Mercurial > cpython-lru_cache-weakref
annotate Modules/bsddbmodule.c @ 4566:6403c94de50d legacy-trunk
Added safeguard against failure in __del__.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 27 Nov 1996 19:39:58 +0000 |
| parents | 65d2d8e67da3 |
| children | e74b607a5996 |
| rev | line source |
|---|---|
| 3034 | 1 /*********************************************************** |
| 2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, | |
| 3 The Netherlands. | |
| 4 | |
| 5 All Rights Reserved | |
| 6 | |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4295
diff
changeset
|
7 Permission to use, copy, modify, and distribute this software and its |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4295
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
| 3034 | 9 provided that the above copyright notice appear in all copies and that |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4295
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
| 3034 | 11 supporting documentation, and that the names of Stichting Mathematisch |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4295
diff
changeset
|
12 Centrum or CWI or Corporation for National Research Initiatives or |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4295
diff
changeset
|
13 CNRI not be used in advertising or publicity pertaining to |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4295
diff
changeset
|
14 distribution of the software without specific, written prior |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4295
diff
changeset
|
15 permission. |
| 3034 | 16 |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4295
diff
changeset
|
17 While CWI is the initial source for this software, a modified version |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4295
diff
changeset
|
18 is made available by the Corporation for National Research Initiatives |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4295
diff
changeset
|
19 (CNRI) at the Internet address ftp://ftp.python.org. |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4295
diff
changeset
|
20 |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4295
diff
changeset
|
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4295
diff
changeset
|
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4295
diff
changeset
|
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4295
diff
changeset
|
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4295
diff
changeset
|
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4295
diff
changeset
|
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4295
diff
changeset
|
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4295
diff
changeset
|
28 PERFORMANCE OF THIS SOFTWARE. |
| 3034 | 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 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
43 #include "Python.h" |
| 3034 | 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 { | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
53 PyObject_HEAD |
| 3034 | 54 DB *di_bsddb; |
| 55 int di_size; /* -1 means recompute */ | |
| 56 } bsddbobject; | |
| 57 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
58 staticforward PyTypeObject Bsddbtype; |
| 3034 | 59 |
| 60 #define is_bsddbobject(v) ((v)->ob_type == &Bsddbtype) | |
| 61 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
62 static PyObject *BsddbError; |
| 3034 | 63 |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
64 static PyObject * |
| 3034 | 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 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
80 if ((dp = PyObject_NEW(bsddbobject, &Bsddbtype)) == NULL) |
| 3034 | 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 | |
|
4295
008b9fc565bf
Force O_BINARY in flags if defined, for e.g. Microsoft platforms.
Guido van Rossum <guido@python.org>
parents:
4294
diff
changeset
|
90 #ifdef O_BINARY |
|
008b9fc565bf
Force O_BINARY in flags if defined, for e.g. Microsoft platforms.
Guido van Rossum <guido@python.org>
parents:
4294
diff
changeset
|
91 flags |= O_BINARY; |
|
008b9fc565bf
Force O_BINARY in flags if defined, for e.g. Microsoft platforms.
Guido van Rossum <guido@python.org>
parents:
4294
diff
changeset
|
92 #endif |
| 3034 | 93 if ((dp->di_bsddb = dbopen(file, flags, mode, DB_HASH, &info)) == NULL) { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
94 PyErr_SetFromErrno(BsddbError); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
95 Py_DECREF(dp); |
| 3034 | 96 return NULL; |
| 97 } | |
| 98 | |
| 99 dp->di_size = -1; | |
| 100 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
101 return (PyObject *)dp; |
| 3034 | 102 } |
| 103 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
104 static PyObject * |
| 3034 | 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 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
120 if ((dp = PyObject_NEW(bsddbobject, &Bsddbtype)) == NULL) |
| 3034 | 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; | |
|
4295
008b9fc565bf
Force O_BINARY in flags if defined, for e.g. Microsoft platforms.
Guido van Rossum <guido@python.org>
parents:
4294
diff
changeset
|
129 info.compare = 0; /* Use default comparison functions, for now..*/ |
| 3034 | 130 info.prefix = 0; |
| 131 | |
|
4295
008b9fc565bf
Force O_BINARY in flags if defined, for e.g. Microsoft platforms.
Guido van Rossum <guido@python.org>
parents:
4294
diff
changeset
|
132 #ifdef O_BINARY |
|
008b9fc565bf
Force O_BINARY in flags if defined, for e.g. Microsoft platforms.
Guido van Rossum <guido@python.org>
parents:
4294
diff
changeset
|
133 flags |= O_BINARY; |
|
008b9fc565bf
Force O_BINARY in flags if defined, for e.g. Microsoft platforms.
Guido van Rossum <guido@python.org>
parents:
4294
diff
changeset
|
134 #endif |
| 3034 | 135 if ((dp->di_bsddb = dbopen(file, flags, mode, DB_BTREE, &info)) == NULL) { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
136 PyErr_SetFromErrno(BsddbError); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
137 Py_DECREF(dp); |
| 3034 | 138 return NULL; |
| 139 } | |
| 140 | |
| 141 dp->di_size = -1; | |
| 142 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
143 return (PyObject *)dp; |
| 3034 | 144 } |
| 145 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
146 static PyObject * |
| 3034 | 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 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
163 if ((dp = PyObject_NEW(bsddbobject, &Bsddbtype)) == NULL) |
| 3034 | 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 | |
|
4295
008b9fc565bf
Force O_BINARY in flags if defined, for e.g. Microsoft platforms.
Guido van Rossum <guido@python.org>
parents:
4294
diff
changeset
|
174 #ifdef O_BINARY |
|
008b9fc565bf
Force O_BINARY in flags if defined, for e.g. Microsoft platforms.
Guido van Rossum <guido@python.org>
parents:
4294
diff
changeset
|
175 flags |= O_BINARY; |
|
008b9fc565bf
Force O_BINARY in flags if defined, for e.g. Microsoft platforms.
Guido van Rossum <guido@python.org>
parents:
4294
diff
changeset
|
176 #endif |
| 3034 | 177 if ((dp->di_bsddb = dbopen(file, flags, mode, DB_RECNO, &info)) == NULL) { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
178 PyErr_SetFromErrno(BsddbError); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
179 Py_DECREF(dp); |
| 3034 | 180 return NULL; |
| 181 } | |
| 182 | |
| 183 dp->di_size = -1; | |
| 184 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
185 return (PyObject *)dp; |
| 3034 | 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, | |
| 196 "Python bsddb: close errno %s in dealloc\n", errno); | |
| 197 } | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
198 PyMem_DEL(dp); |
| 3034 | 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) { | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
214 PyErr_SetFromErrno(BsddbError); |
| 3034 | 215 return -1; |
| 216 } | |
| 217 dp->di_size = size; | |
| 218 } | |
| 219 return dp->di_size; | |
| 220 } | |
| 221 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
222 static PyObject * |
| 3034 | 223 bsddb_subscript(dp, key) |
| 224 bsddbobject *dp; | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
225 PyObject *key; |
| 3034 | 226 { |
| 227 int status; | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
228 PyObject *v; |
| 3034 | 229 DBT krec, drec; |
| 230 char *data; | |
| 231 int size; | |
| 232 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
233 if (!PyArg_Parse(key, "s#", &data, &size)) |
| 3034 | 234 return NULL; |
| 235 krec.data = data; | |
| 236 krec.size = size; | |
| 237 | |
| 238 status = (dp->di_bsddb->get)(dp->di_bsddb, &krec, &drec, 0); | |
| 239 if (status != 0) { | |
| 240 if (status < 0) | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
241 PyErr_SetFromErrno(BsddbError); |
| 3034 | 242 else |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
243 PyErr_SetObject(PyExc_KeyError, key); |
| 3034 | 244 return NULL; |
| 245 } | |
| 246 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
247 return PyString_FromStringAndSize((char *)drec.data, (int)drec.size); |
| 3034 | 248 } |
| 249 | |
| 250 static int | |
| 251 bsddb_ass_sub(dp, key, value) | |
| 252 bsddbobject *dp; | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
253 PyObject *key, *value; |
| 3034 | 254 { |
| 255 int status; | |
| 256 DBT krec, drec; | |
| 257 char *data; | |
| 258 int size; | |
| 259 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
260 if (!PyArg_Parse(key, "s#", &data, &size)) { |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
261 PyErr_SetString(PyExc_TypeError, "bsddb key type must be string"); |
| 3034 | 262 return -1; |
| 263 } | |
| 264 krec.data = data; | |
| 265 krec.size = size; | |
| 266 dp->di_size = -1; | |
| 267 if (value == NULL) { | |
| 268 status = (dp->di_bsddb->del)(dp->di_bsddb, &krec, 0); | |
| 269 } | |
| 270 else { | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
271 if (!PyArg_Parse(value, "s#", &data, &size)) { |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
272 PyErr_SetString(PyExc_TypeError, "bsddb value type must be string"); |
| 3034 | 273 return -1; |
| 274 } | |
| 275 drec.data = data; | |
| 276 drec.size = size; | |
| 277 status = (dp->di_bsddb->put)(dp->di_bsddb, &krec, &drec, 0); | |
| 278 } | |
| 279 if (status != 0) { | |
| 280 if (status < 0) | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
281 PyErr_SetFromErrno(BsddbError); |
| 3034 | 282 else |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
283 PyErr_SetObject(PyExc_KeyError, key); |
| 3034 | 284 return -1; |
| 285 } | |
| 286 return 0; | |
| 287 } | |
| 288 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
289 static PyMappingMethods bsddb_as_mapping = { |
| 3034 | 290 (inquiry)bsddb_length, /*mp_length*/ |
| 291 (binaryfunc)bsddb_subscript, /*mp_subscript*/ | |
| 292 (objobjargproc)bsddb_ass_sub, /*mp_ass_subscript*/ | |
| 293 }; | |
| 294 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
295 static PyObject * |
| 3034 | 296 bsddb_close(dp, args) |
| 297 bsddbobject *dp; | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
298 PyObject *args; |
| 3034 | 299 { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
300 if (!PyArg_NoArgs(args)) |
| 3034 | 301 return NULL; |
| 302 if (dp->di_bsddb != NULL) { | |
| 303 if ((dp->di_bsddb->close)(dp->di_bsddb) != 0) { | |
| 304 dp->di_bsddb = NULL; | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
305 PyErr_SetFromErrno(BsddbError); |
| 3034 | 306 return NULL; |
| 307 } | |
| 308 } | |
| 309 dp->di_bsddb = NULL; | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
310 Py_INCREF(Py_None); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
311 return Py_None; |
| 3034 | 312 } |
| 313 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
314 static PyObject * |
| 3034 | 315 bsddb_keys(dp, args) |
| 316 bsddbobject *dp; | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
317 PyObject *args; |
| 3034 | 318 { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
319 PyObject *list, *item; |
| 3034 | 320 DBT krec, drec; |
| 321 int status; | |
| 322 int err; | |
| 323 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
324 if (!PyArg_NoArgs(args)) |
| 3034 | 325 return NULL; |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
326 list = PyList_New(0); |
| 3034 | 327 if (list == NULL) |
| 328 return NULL; | |
| 329 for (status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_FIRST); | |
| 330 status == 0; | |
| 331 status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_NEXT)) { | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
332 item = PyString_FromStringAndSize((char *)krec.data, (int)krec.size); |
| 3034 | 333 if (item == NULL) { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
334 Py_DECREF(list); |
| 3034 | 335 return NULL; |
| 336 } | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
337 err = PyList_Append(list, item); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
338 Py_DECREF(item); |
| 3034 | 339 if (err != 0) { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
340 Py_DECREF(list); |
| 3034 | 341 return NULL; |
| 342 } | |
| 343 } | |
| 344 if (status < 0) { | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
345 PyErr_SetFromErrno(BsddbError); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
346 Py_DECREF(list); |
| 3034 | 347 return NULL; |
| 348 } | |
| 349 if (dp->di_size < 0) | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
350 dp->di_size = PyList_Size(list); /* We just did the work for this... */ |
| 3034 | 351 return list; |
| 352 } | |
| 353 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
354 static PyObject * |
| 3034 | 355 bsddb_has_key(dp, args) |
| 356 bsddbobject *dp; | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
357 PyObject *args; |
| 3034 | 358 { |
| 359 DBT krec, drec; | |
| 360 int status; | |
| 361 char *data; | |
| 362 int size; | |
| 363 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
364 if (!PyArg_Parse(args, "s#", &data, &size)) |
| 3034 | 365 return NULL; |
| 366 krec.data = data; | |
| 367 krec.size = size; | |
| 368 | |
| 369 status = (dp->di_bsddb->get)(dp->di_bsddb, &krec, &drec, 0); | |
| 370 if (status < 0) { | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
371 PyErr_SetFromErrno(BsddbError); |
| 3034 | 372 return NULL; |
| 373 } | |
| 374 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
375 return PyInt_FromLong(status == 0); |
| 3034 | 376 } |
| 377 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
378 static PyObject * |
| 3034 | 379 bsddb_set_location(dp, key) |
| 380 bsddbobject *dp; | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
381 PyObject *key; |
| 3034 | 382 { |
| 383 int status; | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
384 PyObject *v; |
| 3034 | 385 DBT krec, drec; |
| 386 char *data; | |
| 387 int size; | |
| 388 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
389 if (!PyArg_Parse(key, "s#", &data, &size)) |
| 3034 | 390 return NULL; |
| 391 krec.data = data; | |
| 392 krec.size = size; | |
| 393 | |
| 394 status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_CURSOR); | |
| 395 if (status != 0) { | |
| 396 if (status < 0) | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
397 PyErr_SetFromErrno(BsddbError); |
| 3034 | 398 else |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
399 PyErr_SetObject(PyExc_KeyError, key); |
| 3034 | 400 return NULL; |
| 401 } | |
| 402 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
403 return Py_BuildValue("s#s#", krec.data, krec.size, drec.data, drec.size); |
| 3034 | 404 } |
| 405 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
406 static PyObject * |
| 3034 | 407 bsddb_seq(dp, args, sequence_request) |
| 408 bsddbobject *dp; | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
409 PyObject *args; |
| 3034 | 410 int sequence_request; |
| 411 { | |
| 412 int status; | |
| 413 DBT krec, drec; | |
| 414 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
415 if (!PyArg_NoArgs(args)) |
| 3034 | 416 return NULL; |
| 417 | |
| 418 krec.data = 0; | |
| 419 krec.size = 0; | |
| 420 | |
| 421 status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, sequence_request); | |
| 422 if (status != 0) { | |
| 423 if (status < 0) | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
424 PyErr_SetFromErrno(BsddbError); |
| 3034 | 425 else |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
426 PyErr_SetObject(PyExc_KeyError, args); |
| 3034 | 427 return NULL; |
| 428 } | |
| 429 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
430 return Py_BuildValue("s#s#", krec.data, krec.size, drec.data, drec.size); |
| 3034 | 431 } |
| 432 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
433 static PyObject * |
| 3034 | 434 bsddb_next(dp, key) |
| 435 bsddbobject *dp; | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
436 PyObject *key; |
| 3034 | 437 { |
| 438 return bsddb_seq(dp, key, R_NEXT); | |
| 439 } | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
440 static PyObject * |
| 3034 | 441 bsddb_previous(dp, key) |
| 442 bsddbobject *dp; | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
443 PyObject *key; |
| 3034 | 444 { |
| 445 return bsddb_seq(dp, key, R_PREV); | |
| 446 } | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
447 static PyObject * |
| 3034 | 448 bsddb_first(dp, key) |
| 449 bsddbobject *dp; | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
450 PyObject *key; |
| 3034 | 451 { |
| 452 return bsddb_seq(dp, key, R_FIRST); | |
| 453 } | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
454 static PyObject * |
| 3034 | 455 bsddb_last(dp, key) |
| 456 bsddbobject *dp; | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
457 PyObject *key; |
| 3034 | 458 { |
| 459 return bsddb_seq(dp, key, R_LAST); | |
| 460 } | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
461 static PyObject * |
| 3034 | 462 bsddb_sync(dp, args) |
| 463 bsddbobject *dp; | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
464 PyObject *args; |
| 3034 | 465 { |
| 466 int status; | |
| 467 | |
| 468 status = (dp->di_bsddb->sync)(dp->di_bsddb, 0); | |
| 469 if (status != 0) { | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
470 PyErr_SetFromErrno(BsddbError); |
| 3034 | 471 return NULL; |
| 472 } | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
473 return PyInt_FromLong(status = 0); |
| 3034 | 474 } |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
475 static PyMethodDef bsddb_methods[] = { |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
476 {"close", (PyCFunction)bsddb_close}, |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
477 {"keys", (PyCFunction)bsddb_keys}, |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
478 {"has_key", (PyCFunction)bsddb_has_key}, |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
479 {"set_location", (PyCFunction)bsddb_set_location}, |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
480 {"next", (PyCFunction)bsddb_next}, |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
481 {"previous", (PyCFunction)bsddb_previous}, |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
482 {"first", (PyCFunction)bsddb_first}, |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
483 {"last", (PyCFunction)bsddb_last}, |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
484 {"sync", (PyCFunction)bsddb_sync}, |
| 3034 | 485 {NULL, NULL} /* sentinel */ |
| 486 }; | |
| 487 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
488 static PyObject * |
| 3034 | 489 bsddb_getattr(dp, name) |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
490 PyObject *dp; |
| 3034 | 491 char *name; |
| 492 { | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
493 return Py_FindMethod(bsddb_methods, dp, name); |
| 3034 | 494 } |
| 495 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
496 static PyTypeObject Bsddbtype = { |
|
4294
520ddcb3d5d6
Dynamically initialize ob_type field, for Windows DLL
Guido van Rossum <guido@python.org>
parents:
3803
diff
changeset
|
497 PyObject_HEAD_INIT(NULL) |
| 3034 | 498 0, |
| 499 "bsddb", | |
| 500 sizeof(bsddbobject), | |
| 501 0, | |
| 502 (destructor)bsddb_dealloc, /*tp_dealloc*/ | |
| 503 0, /*tp_print*/ | |
| 504 (getattrfunc)bsddb_getattr, /*tp_getattr*/ | |
| 505 0, /*tp_setattr*/ | |
| 506 0, /*tp_compare*/ | |
| 507 0, /*tp_repr*/ | |
| 508 0, /*tp_as_number*/ | |
| 509 0, /*tp_as_sequence*/ | |
| 510 &bsddb_as_mapping, /*tp_as_mapping*/ | |
| 511 }; | |
| 512 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
513 static PyObject * |
| 3034 | 514 bsdhashopen(self, args) |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
515 PyObject *self; |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
516 PyObject *args; |
| 3034 | 517 { |
| 518 char *file; | |
| 519 char *flag = NULL; | |
| 520 int flags = O_RDONLY; | |
| 521 int mode = 0666; | |
| 522 int bsize = 0; | |
| 523 int ffactor = 0; | |
| 524 int nelem = 0; | |
| 525 int cachesize = 0; | |
| 526 int hash = 0; /* XXX currently ignored */ | |
| 527 int lorder = 0; | |
| 528 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
529 if (!PyArg_ParseTuple(args, "s|siiiiiii", |
| 3034 | 530 &file, &flag, &mode, |
| 531 &bsize, &ffactor, &nelem, &cachesize, &hash, &lorder)) | |
| 532 return NULL; | |
| 533 if (flag != NULL) { | |
| 534 /* XXX need a way to pass O_EXCL, O_EXLOCK, O_NONBLOCK, O_SHLOCK */ | |
| 535 if (flag[0] == 'r') | |
| 536 flags = O_RDONLY; | |
| 537 else if (flag[0] == 'w') | |
| 538 flags = O_RDWR; | |
| 539 else if (flag[0] == 'c') | |
| 540 flags = O_RDWR|O_CREAT; | |
| 541 else if (flag[0] == 'n') | |
| 542 flags = O_RDWR|O_CREAT|O_TRUNC; | |
| 543 else { | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
544 PyErr_SetString(BsddbError, |
| 3034 | 545 "Flag should begin with 'r', 'w', 'c' or 'n'"); |
| 546 return NULL; | |
| 547 } | |
| 548 if (flag[1] == 'l') { | |
| 549 #if defined(O_EXLOCK) && defined(O_SHLOCK) | |
| 550 if (flag[0] == 'r') | |
| 551 flags |= O_SHLOCK; | |
| 552 else | |
| 553 flags |= O_EXLOCK; | |
| 554 #else | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
555 PyErr_SetString(BsddbError, "locking not supported on this platform"); |
| 3034 | 556 return NULL; |
| 557 #endif | |
| 558 } | |
| 559 } | |
| 560 return newdbhashobject(file, flags, mode, | |
| 561 bsize, ffactor, nelem, cachesize, hash, lorder); | |
| 562 } | |
| 563 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
564 static PyObject * |
| 3034 | 565 bsdbtopen(self, args) |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
566 PyObject *self; |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
567 PyObject *args; |
| 3034 | 568 { |
| 569 char *file; | |
| 570 char *flag = NULL; | |
| 571 int flags = O_RDONLY; | |
| 572 int mode = 0666; | |
| 573 int cachesize = 0; | |
|
3600
7c5711a6a475
Be more careful about default args.
Guido van Rossum <guido@python.org>
parents:
3034
diff
changeset
|
574 int maxkeypage = 0; |
|
7c5711a6a475
Be more careful about default args.
Guido van Rossum <guido@python.org>
parents:
3034
diff
changeset
|
575 int minkeypage = 0; |
|
7c5711a6a475
Be more careful about default args.
Guido van Rossum <guido@python.org>
parents:
3034
diff
changeset
|
576 int btflags = 0; |
|
7c5711a6a475
Be more careful about default args.
Guido van Rossum <guido@python.org>
parents:
3034
diff
changeset
|
577 unsigned int psize = 0; |
|
7c5711a6a475
Be more careful about default args.
Guido van Rossum <guido@python.org>
parents:
3034
diff
changeset
|
578 int lorder = 0; |
| 3034 | 579 |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
580 if (!PyArg_ParseTuple(args, "s|siiiiiii", |
|
3600
7c5711a6a475
Be more careful about default args.
Guido van Rossum <guido@python.org>
parents:
3034
diff
changeset
|
581 &file, &flag, &mode, |
|
7c5711a6a475
Be more careful about default args.
Guido van Rossum <guido@python.org>
parents:
3034
diff
changeset
|
582 &btflags, &cachesize, &maxkeypage, &minkeypage, |
|
7c5711a6a475
Be more careful about default args.
Guido van Rossum <guido@python.org>
parents:
3034
diff
changeset
|
583 &psize, &lorder)) |
| 3034 | 584 return NULL; |
| 585 if (flag != NULL) { | |
| 586 /* XXX need a way to pass O_EXCL, O_EXLOCK, O_NONBLOCK, O_SHLOCK */ | |
| 587 if (flag[0] == 'r') | |
| 588 flags = O_RDONLY; | |
| 589 else if (flag[0] == 'w') | |
| 590 flags = O_RDWR; | |
| 591 else if (flag[0] == 'c') | |
| 592 flags = O_RDWR|O_CREAT; | |
| 593 else if (flag[0] == 'n') | |
| 594 flags = O_RDWR|O_CREAT|O_TRUNC; | |
| 595 else { | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
596 PyErr_SetString(BsddbError, |
| 3034 | 597 "Flag should begin with 'r', 'w', 'c' or 'n'"); |
| 598 return NULL; | |
| 599 } | |
| 600 if (flag[1] == 'l') { | |
| 601 #if defined(O_EXLOCK) && defined(O_SHLOCK) | |
| 602 if (flag[0] == 'r') | |
| 603 flags |= O_SHLOCK; | |
| 604 else | |
| 605 flags |= O_EXLOCK; | |
| 606 #else | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
607 PyErr_SetString(BsddbError, "locking not supported on this platform"); |
| 3034 | 608 return NULL; |
| 609 #endif | |
| 610 } | |
| 611 } | |
| 612 return newdbbtobject(file, flags, mode, | |
|
3600
7c5711a6a475
Be more careful about default args.
Guido van Rossum <guido@python.org>
parents:
3034
diff
changeset
|
613 btflags, cachesize, maxkeypage, minkeypage, |
|
7c5711a6a475
Be more careful about default args.
Guido van Rossum <guido@python.org>
parents:
3034
diff
changeset
|
614 psize, lorder); |
| 3034 | 615 } |
|
3600
7c5711a6a475
Be more careful about default args.
Guido van Rossum <guido@python.org>
parents:
3034
diff
changeset
|
616 |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
617 static PyObject * |
| 3034 | 618 bsdrnopen(self, args) |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
619 PyObject *self; |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
620 PyObject *args; |
| 3034 | 621 { |
| 622 char *file; | |
| 623 char *flag = NULL; | |
| 624 int flags = O_RDONLY; | |
| 625 int mode = 0666; | |
| 626 int cachesize = 0; | |
|
3600
7c5711a6a475
Be more careful about default args.
Guido van Rossum <guido@python.org>
parents:
3034
diff
changeset
|
627 int rnflags = 0; |
|
7c5711a6a475
Be more careful about default args.
Guido van Rossum <guido@python.org>
parents:
3034
diff
changeset
|
628 unsigned int psize = 0; |
|
7c5711a6a475
Be more careful about default args.
Guido van Rossum <guido@python.org>
parents:
3034
diff
changeset
|
629 int lorder = 0; |
|
7c5711a6a475
Be more careful about default args.
Guido van Rossum <guido@python.org>
parents:
3034
diff
changeset
|
630 size_t reclen = 0; |
|
7c5711a6a475
Be more careful about default args.
Guido van Rossum <guido@python.org>
parents:
3034
diff
changeset
|
631 char *bval = ""; |
|
7c5711a6a475
Be more careful about default args.
Guido van Rossum <guido@python.org>
parents:
3034
diff
changeset
|
632 char *bfname = NULL; |
| 3034 | 633 |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
634 if (!PyArg_ParseTuple(args, "s|siiiiiiss", |
|
3600
7c5711a6a475
Be more careful about default args.
Guido van Rossum <guido@python.org>
parents:
3034
diff
changeset
|
635 &file, &flag, &mode, |
|
7c5711a6a475
Be more careful about default args.
Guido van Rossum <guido@python.org>
parents:
3034
diff
changeset
|
636 &rnflags, &cachesize, &psize, &lorder, |
|
7c5711a6a475
Be more careful about default args.
Guido van Rossum <guido@python.org>
parents:
3034
diff
changeset
|
637 &reclen, &bval, &bfname)) |
| 3034 | 638 return NULL; |
| 639 if (flag != NULL) { | |
| 640 /* XXX need a way to pass O_EXCL, O_EXLOCK, O_NONBLOCK, O_SHLOCK */ | |
| 641 if (flag[0] == 'r') | |
| 642 flags = O_RDONLY; | |
| 643 else if (flag[0] == 'w') | |
| 644 flags = O_RDWR; | |
| 645 else if (flag[0] == 'c') | |
| 646 flags = O_RDWR|O_CREAT; | |
| 647 else if (flag[0] == 'n') | |
| 648 flags = O_RDWR|O_CREAT|O_TRUNC; | |
| 649 else { | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
650 PyErr_SetString(BsddbError, |
| 3034 | 651 "Flag should begin with 'r', 'w', 'c' or 'n'"); |
| 652 return NULL; | |
| 653 } | |
| 654 if (flag[1] == 'l') { | |
| 655 #if defined(O_EXLOCK) && defined(O_SHLOCK) | |
| 656 if (flag[0] == 'r') | |
| 657 flags |= O_SHLOCK; | |
| 658 else | |
| 659 flags |= O_EXLOCK; | |
| 660 #else | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
661 PyErr_SetString(BsddbError, "locking not supported on this platform"); |
| 3034 | 662 return NULL; |
| 663 #endif | |
| 664 } | |
|
3600
7c5711a6a475
Be more careful about default args.
Guido van Rossum <guido@python.org>
parents:
3034
diff
changeset
|
665 else if (flag[1] != '\0') { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
666 PyErr_SetString(BsddbError, |
|
3600
7c5711a6a475
Be more careful about default args.
Guido van Rossum <guido@python.org>
parents:
3034
diff
changeset
|
667 "Flag char 2 should be 'l' or absent"); |
|
7c5711a6a475
Be more careful about default args.
Guido van Rossum <guido@python.org>
parents:
3034
diff
changeset
|
668 return NULL; |
|
7c5711a6a475
Be more careful about default args.
Guido van Rossum <guido@python.org>
parents:
3034
diff
changeset
|
669 } |
| 3034 | 670 } |
| 671 return newdbrnobject(file, flags, mode, | |
| 672 rnflags, cachesize, psize, lorder, bval[0], bfname); | |
| 673 } | |
| 674 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
675 static PyMethodDef bsddbmodule_methods[] = { |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
676 {"hashopen", (PyCFunction)bsdhashopen, 1}, |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
677 {"btopen", (PyCFunction)bsdbtopen, 1}, |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
678 {"rnopen", (PyCFunction)bsdrnopen, 1}, |
| 3034 | 679 {0, 0}, |
| 680 }; | |
| 681 | |
| 682 void | |
| 683 initbsddb() { | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
684 PyObject *m, *d; |
| 3034 | 685 |
|
4294
520ddcb3d5d6
Dynamically initialize ob_type field, for Windows DLL
Guido van Rossum <guido@python.org>
parents:
3803
diff
changeset
|
686 Bsddbtype.ob_type = &PyType_Type; |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
687 m = Py_InitModule("bsddb", bsddbmodule_methods); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
688 d = PyModule_GetDict(m); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
689 BsddbError = PyString_FromString("bsddb.error"); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
690 if (BsddbError == NULL || PyDict_SetItemString(d, "error", BsddbError)) |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3600
diff
changeset
|
691 Py_FatalError("can't define bsddb.error"); |
| 3034 | 692 } |
