blob: 35a22b993ddec8847f73f4ea8a1d0be41bb43d1a [file] [log] [blame]
Guido van Rossum1100dca1995-08-30 23:43:03 +00001
2/* Berkeley DB interface.
3 Author: Michael McLay
4 Hacked: Guido van Rossum
5 Btree and Recno additions plus sequence methods: David Ely
6
7 XXX To do:
8 - provide interface to the B-tree and record libraries too
9 - provide a way to access the various hash functions
10 - support more open flags
Guido van Rossum675e9941999-09-20 13:28:18 +000011
12 The windows port of the Berkeley DB code is hard to find on the web:
13 www.nightmare.com/software.html
14*/
Guido van Rossum1100dca1995-08-30 23:43:03 +000015
Guido van Rossumdfe8ad91996-07-24 00:51:20 +000016#include "Python.h"
Guido van Rossum4f199ea1998-04-09 20:56:35 +000017#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +000018#include "pythread.h"
Guido van Rossum4f199ea1998-04-09 20:56:35 +000019#endif
Guido van Rossum1100dca1995-08-30 23:43:03 +000020
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
Fred Drakec9cb8472000-08-31 16:11:07 +000024#ifdef HAVE_DB_185_H
25#include <db_185.h>
26#else
Guido van Rossum1100dca1995-08-30 23:43:03 +000027#include <db.h>
Fred Drakec9cb8472000-08-31 16:11:07 +000028#endif
Guido van Rossum1100dca1995-08-30 23:43:03 +000029/* Please don't include internal header files of the Berkeley db package
30 (it messes up the info required in the Setup file) */
31
32typedef struct {
Roger E. Massed9240d11997-01-16 22:05:33 +000033 PyObject_HEAD
34 DB *di_bsddb;
35 int di_size; /* -1 means recompute */
Guido van Rossum4f199ea1998-04-09 20:56:35 +000036#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +000037 PyThread_type_lock di_lock;
Guido van Rossum4f199ea1998-04-09 20:56:35 +000038#endif
Guido van Rossum1100dca1995-08-30 23:43:03 +000039} bsddbobject;
40
Guido van Rossumdfe8ad91996-07-24 00:51:20 +000041staticforward PyTypeObject Bsddbtype;
Guido van Rossum1100dca1995-08-30 23:43:03 +000042
43#define is_bsddbobject(v) ((v)->ob_type == &Bsddbtype)
Guido van Rossum77eecfa1997-07-17 22:56:01 +000044#define check_bsddbobject_open(v) if ((v)->di_bsddb == NULL) \
45 { PyErr_SetString(BsddbError, "BSDDB object has already been closed"); \
46 return NULL; }
Guido van Rossum1100dca1995-08-30 23:43:03 +000047
Guido van Rossumdfe8ad91996-07-24 00:51:20 +000048static PyObject *BsddbError;
Guido van Rossum1100dca1995-08-30 23:43:03 +000049
Guido van Rossumdfe8ad91996-07-24 00:51:20 +000050static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +000051newdbhashobject(char *file, int flags, int mode,
52 int bsize, int ffactor, int nelem, int cachesize, int hash, int lorder)
Guido van Rossum1100dca1995-08-30 23:43:03 +000053{
Roger E. Massed9240d11997-01-16 22:05:33 +000054 bsddbobject *dp;
55 HASHINFO info;
Guido van Rossum1100dca1995-08-30 23:43:03 +000056
Guido van Rossumb18618d2000-05-03 23:44:39 +000057 if ((dp = PyObject_New(bsddbobject, &Bsddbtype)) == NULL)
Roger E. Massed9240d11997-01-16 22:05:33 +000058 return NULL;
Guido van Rossum1100dca1995-08-30 23:43:03 +000059
Roger E. Massed9240d11997-01-16 22:05:33 +000060 info.bsize = bsize;
61 info.ffactor = ffactor;
62 info.nelem = nelem;
63 info.cachesize = cachesize;
64 info.hash = NULL; /* XXX should derive from hash argument */
65 info.lorder = lorder;
Guido van Rossum1100dca1995-08-30 23:43:03 +000066
Guido van Rossum6beb4791996-09-11 23:22:25 +000067#ifdef O_BINARY
Roger E. Massed9240d11997-01-16 22:05:33 +000068 flags |= O_BINARY;
Guido van Rossum6beb4791996-09-11 23:22:25 +000069#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +000070 Py_BEGIN_ALLOW_THREADS
71 dp->di_bsddb = dbopen(file, flags, mode, DB_HASH, &info);
72 Py_END_ALLOW_THREADS
73 if (dp->di_bsddb == NULL) {
Roger E. Massed9240d11997-01-16 22:05:33 +000074 PyErr_SetFromErrno(BsddbError);
Guido van Rossum4f199ea1998-04-09 20:56:35 +000075#ifdef WITH_THREAD
76 dp->di_lock = NULL;
77#endif
Roger E. Massed9240d11997-01-16 22:05:33 +000078 Py_DECREF(dp);
79 return NULL;
80 }
Guido van Rossum1100dca1995-08-30 23:43:03 +000081
Roger E. Massed9240d11997-01-16 22:05:33 +000082 dp->di_size = -1;
Guido van Rossum4f199ea1998-04-09 20:56:35 +000083#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +000084 dp->di_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +000085 if (dp->di_lock == NULL) {
86 PyErr_SetString(BsddbError, "can't allocate lock");
87 Py_DECREF(dp);
88 return NULL;
89 }
90#endif
Guido van Rossum1100dca1995-08-30 23:43:03 +000091
Roger E. Massed9240d11997-01-16 22:05:33 +000092 return (PyObject *)dp;
Guido van Rossum1100dca1995-08-30 23:43:03 +000093}
94
Guido van Rossumdfe8ad91996-07-24 00:51:20 +000095static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +000096newdbbtobject(char *file, int flags, int mode,
97 int btflags, int cachesize, int maxkeypage, int minkeypage, int psize, int lorder)
Guido van Rossum1100dca1995-08-30 23:43:03 +000098{
Roger E. Massed9240d11997-01-16 22:05:33 +000099 bsddbobject *dp;
100 BTREEINFO info;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000101
Guido van Rossumb18618d2000-05-03 23:44:39 +0000102 if ((dp = PyObject_New(bsddbobject, &Bsddbtype)) == NULL)
Roger E. Massed9240d11997-01-16 22:05:33 +0000103 return NULL;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000104
Roger E. Massed9240d11997-01-16 22:05:33 +0000105 info.flags = btflags;
106 info.cachesize = cachesize;
107 info.maxkeypage = maxkeypage;
108 info.minkeypage = minkeypage;
109 info.psize = psize;
110 info.lorder = lorder;
111 info.compare = 0; /* Use default comparison functions, for now..*/
112 info.prefix = 0;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000113
Guido van Rossum6beb4791996-09-11 23:22:25 +0000114#ifdef O_BINARY
Roger E. Massed9240d11997-01-16 22:05:33 +0000115 flags |= O_BINARY;
Guido van Rossum6beb4791996-09-11 23:22:25 +0000116#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000117 Py_BEGIN_ALLOW_THREADS
118 dp->di_bsddb = dbopen(file, flags, mode, DB_BTREE, &info);
119 Py_END_ALLOW_THREADS
120 if (dp->di_bsddb == NULL) {
Roger E. Massed9240d11997-01-16 22:05:33 +0000121 PyErr_SetFromErrno(BsddbError);
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000122#ifdef WITH_THREAD
123 dp->di_lock = NULL;
124#endif
Roger E. Massed9240d11997-01-16 22:05:33 +0000125 Py_DECREF(dp);
126 return NULL;
127 }
Guido van Rossum1100dca1995-08-30 23:43:03 +0000128
Roger E. Massed9240d11997-01-16 22:05:33 +0000129 dp->di_size = -1;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000130#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000131 dp->di_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000132 if (dp->di_lock == NULL) {
133 PyErr_SetString(BsddbError, "can't allocate lock");
134 Py_DECREF(dp);
135 return NULL;
136 }
137#endif
Guido van Rossum1100dca1995-08-30 23:43:03 +0000138
Roger E. Massed9240d11997-01-16 22:05:33 +0000139 return (PyObject *)dp;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000140}
141
Guido van Rossumdfe8ad91996-07-24 00:51:20 +0000142static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000143newdbrnobject(char *file, int flags, int mode,
144 int rnflags, int cachesize, int psize, int lorder, size_t reclen, u_char bval, char *bfname)
Guido van Rossum1100dca1995-08-30 23:43:03 +0000145{
Roger E. Massed9240d11997-01-16 22:05:33 +0000146 bsddbobject *dp;
147 RECNOINFO info;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000148
Guido van Rossumb18618d2000-05-03 23:44:39 +0000149 if ((dp = PyObject_New(bsddbobject, &Bsddbtype)) == NULL)
Roger E. Massed9240d11997-01-16 22:05:33 +0000150 return NULL;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000151
Roger E. Massed9240d11997-01-16 22:05:33 +0000152 info.flags = rnflags;
153 info.cachesize = cachesize;
154 info.psize = psize;
155 info.lorder = lorder;
156 info.reclen = reclen;
157 info.bval = bval;
158 info.bfname = bfname;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000159
Guido van Rossum6beb4791996-09-11 23:22:25 +0000160#ifdef O_BINARY
Roger E. Massed9240d11997-01-16 22:05:33 +0000161 flags |= O_BINARY;
Guido van Rossum6beb4791996-09-11 23:22:25 +0000162#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000163 Py_BEGIN_ALLOW_THREADS
164 dp->di_bsddb = dbopen(file, flags, mode, DB_RECNO, &info);
165 Py_END_ALLOW_THREADS
166 if (dp->di_bsddb == NULL) {
Roger E. Massed9240d11997-01-16 22:05:33 +0000167 PyErr_SetFromErrno(BsddbError);
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000168#ifdef WITH_THREAD
169 dp->di_lock = NULL;
170#endif
Roger E. Massed9240d11997-01-16 22:05:33 +0000171 Py_DECREF(dp);
172 return NULL;
173 }
Guido van Rossum1100dca1995-08-30 23:43:03 +0000174
Roger E. Massed9240d11997-01-16 22:05:33 +0000175 dp->di_size = -1;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000176#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000177 dp->di_lock = PyThread_allocate_lock();
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000178 if (dp->di_lock == NULL) {
179 PyErr_SetString(BsddbError, "can't allocate lock");
180 Py_DECREF(dp);
181 return NULL;
182 }
183#endif
Guido van Rossum1100dca1995-08-30 23:43:03 +0000184
Roger E. Massed9240d11997-01-16 22:05:33 +0000185 return (PyObject *)dp;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000186}
187
Guido van Rossum1100dca1995-08-30 23:43:03 +0000188static void
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000189bsddb_dealloc(bsddbobject *dp)
Guido van Rossum1100dca1995-08-30 23:43:03 +0000190{
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000191#ifdef WITH_THREAD
192 if (dp->di_lock) {
Guido van Rossum65d5b571998-12-21 19:32:43 +0000193 PyThread_acquire_lock(dp->di_lock, 0);
194 PyThread_release_lock(dp->di_lock);
195 PyThread_free_lock(dp->di_lock);
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000196 dp->di_lock = NULL;
197 }
198#endif
Roger E. Massed9240d11997-01-16 22:05:33 +0000199 if (dp->di_bsddb != NULL) {
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000200 int status;
201 Py_BEGIN_ALLOW_THREADS
202 status = (dp->di_bsddb->close)(dp->di_bsddb);
203 Py_END_ALLOW_THREADS
204 if (status != 0)
Roger E. Massed9240d11997-01-16 22:05:33 +0000205 fprintf(stderr,
206 "Python bsddb: close errno %d in dealloc\n",
207 errno);
208 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000209 PyObject_Del(dp);
Guido van Rossum1100dca1995-08-30 23:43:03 +0000210}
211
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000212#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000213#define BSDDB_BGN_SAVE(_dp) Py_BEGIN_ALLOW_THREADS PyThread_acquire_lock(_dp->di_lock,1);
214#define BSDDB_END_SAVE(_dp) PyThread_release_lock(_dp->di_lock); Py_END_ALLOW_THREADS
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000215#else
216#define BSDDB_BGN_SAVE(_dp) Py_BEGIN_ALLOW_THREADS
217#define BSDDB_END_SAVE(_dp) Py_END_ALLOW_THREADS
218#endif
219
Guido van Rossum1100dca1995-08-30 23:43:03 +0000220static int
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000221bsddb_length(bsddbobject *dp)
Guido van Rossum1100dca1995-08-30 23:43:03 +0000222{
Guido van Rossum77eecfa1997-07-17 22:56:01 +0000223 if (dp->di_bsddb == NULL) {
224 PyErr_SetString(BsddbError, "BSDDB object has already been closed");
225 return -1;
226 }
Roger E. Massed9240d11997-01-16 22:05:33 +0000227 if (dp->di_size < 0) {
228 DBT krec, drec;
229 int status;
230 int size = 0;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000231 BSDDB_BGN_SAVE(dp)
Roger E. Massed9240d11997-01-16 22:05:33 +0000232 for (status = (dp->di_bsddb->seq)(dp->di_bsddb,
233 &krec, &drec,R_FIRST);
234 status == 0;
235 status = (dp->di_bsddb->seq)(dp->di_bsddb,
236 &krec, &drec, R_NEXT))
237 size++;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000238 BSDDB_END_SAVE(dp)
Roger E. Massed9240d11997-01-16 22:05:33 +0000239 if (status < 0) {
240 PyErr_SetFromErrno(BsddbError);
241 return -1;
242 }
243 dp->di_size = size;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000244 }
Roger E. Massed9240d11997-01-16 22:05:33 +0000245 return dp->di_size;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000246}
247
Guido van Rossumdfe8ad91996-07-24 00:51:20 +0000248static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000249bsddb_subscript(bsddbobject *dp, PyObject *key)
Guido van Rossum1100dca1995-08-30 23:43:03 +0000250{
Roger E. Massed9240d11997-01-16 22:05:33 +0000251 int status;
252 DBT krec, drec;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000253 char *data,buf[4096];
Roger E. Massed9240d11997-01-16 22:05:33 +0000254 int size;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000255 PyObject *result;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000256
Roger E. Massed9240d11997-01-16 22:05:33 +0000257 if (!PyArg_Parse(key, "s#", &data, &size))
258 return NULL;
Guido van Rossum77eecfa1997-07-17 22:56:01 +0000259 check_bsddbobject_open(dp);
260
Roger E. Massed9240d11997-01-16 22:05:33 +0000261 krec.data = data;
262 krec.size = size;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000263
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000264 BSDDB_BGN_SAVE(dp)
Roger E. Massed9240d11997-01-16 22:05:33 +0000265 status = (dp->di_bsddb->get)(dp->di_bsddb, &krec, &drec, 0);
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000266 if (status == 0) {
267 if (drec.size > sizeof(buf)) data = malloc(drec.size);
268 else data = buf;
Andrew M. Kuchling2e095302000-12-15 00:59:32 +0000269 if (data!=NULL) memcpy(data,drec.data,drec.size);
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000270 }
271 BSDDB_END_SAVE(dp)
Andrew M. Kuchling2e095302000-12-15 00:59:32 +0000272 if (data==NULL) return PyErr_NoMemory();
Roger E. Massed9240d11997-01-16 22:05:33 +0000273 if (status != 0) {
274 if (status < 0)
275 PyErr_SetFromErrno(BsddbError);
276 else
277 PyErr_SetObject(PyExc_KeyError, key);
278 return NULL;
279 }
Guido van Rossum1100dca1995-08-30 23:43:03 +0000280
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000281 result = PyString_FromStringAndSize(data, (int)drec.size);
282 if (data != buf) free(data);
283 return result;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000284}
285
286static int
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000287bsddb_ass_sub(bsddbobject *dp, PyObject *key, PyObject *value)
Guido van Rossum1100dca1995-08-30 23:43:03 +0000288{
Roger E. Massed9240d11997-01-16 22:05:33 +0000289 int status;
290 DBT krec, drec;
291 char *data;
292 int size;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000293
Roger E. Massed9240d11997-01-16 22:05:33 +0000294 if (!PyArg_Parse(key, "s#", &data, &size)) {
295 PyErr_SetString(PyExc_TypeError,
296 "bsddb key type must be string");
297 return -1;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000298 }
Guido van Rossum77eecfa1997-07-17 22:56:01 +0000299 if (dp->di_bsddb == NULL) {
300 PyErr_SetString(BsddbError, "BSDDB object has already been closed");
301 return -1;
302 }
Roger E. Massed9240d11997-01-16 22:05:33 +0000303 krec.data = data;
304 krec.size = size;
305 dp->di_size = -1;
306 if (value == NULL) {
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000307 BSDDB_BGN_SAVE(dp)
Roger E. Massed9240d11997-01-16 22:05:33 +0000308 status = (dp->di_bsddb->del)(dp->di_bsddb, &krec, 0);
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000309 BSDDB_END_SAVE(dp)
Roger E. Massed9240d11997-01-16 22:05:33 +0000310 }
311 else {
312 if (!PyArg_Parse(value, "s#", &data, &size)) {
313 PyErr_SetString(PyExc_TypeError,
314 "bsddb value type must be string");
315 return -1;
316 }
317 drec.data = data;
318 drec.size = size;
319#if 0
320 /* For RECNO, put fails with 'No space left on device'
321 after a few short records are added?? Looks fine
322 to this point... linked with 1.85 on Solaris Intel
323 Roger E. Masse 1/16/97
324 */
325 printf("before put data: '%s', size: %d\n",
326 drec.data, drec.size);
327 printf("before put key= '%s', size= %d\n",
328 krec.data, krec.size);
329#endif
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000330 BSDDB_BGN_SAVE(dp)
Roger E. Massed9240d11997-01-16 22:05:33 +0000331 status = (dp->di_bsddb->put)(dp->di_bsddb, &krec, &drec, 0);
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000332 BSDDB_END_SAVE(dp)
Roger E. Massed9240d11997-01-16 22:05:33 +0000333 }
334 if (status != 0) {
335 if (status < 0)
336 PyErr_SetFromErrno(BsddbError);
337 else
338 PyErr_SetObject(PyExc_KeyError, key);
339 return -1;
340 }
341 return 0;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000342}
343
Guido van Rossumdfe8ad91996-07-24 00:51:20 +0000344static PyMappingMethods bsddb_as_mapping = {
Roger E. Massed9240d11997-01-16 22:05:33 +0000345 (inquiry)bsddb_length, /*mp_length*/
346 (binaryfunc)bsddb_subscript, /*mp_subscript*/
347 (objobjargproc)bsddb_ass_sub, /*mp_ass_subscript*/
Guido van Rossum1100dca1995-08-30 23:43:03 +0000348};
349
Guido van Rossumdfe8ad91996-07-24 00:51:20 +0000350static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000351bsddb_close(bsddbobject *dp, PyObject *args)
Guido van Rossum1100dca1995-08-30 23:43:03 +0000352{
Roger E. Massed9240d11997-01-16 22:05:33 +0000353 if (!PyArg_NoArgs(args))
354 return NULL;
355 if (dp->di_bsddb != NULL) {
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000356 int status;
357 BSDDB_BGN_SAVE(dp)
358 status = (dp->di_bsddb->close)(dp->di_bsddb);
359 BSDDB_END_SAVE(dp)
360 if (status != 0) {
Roger E. Massed9240d11997-01-16 22:05:33 +0000361 dp->di_bsddb = NULL;
362 PyErr_SetFromErrno(BsddbError);
363 return NULL;
364 }
Guido van Rossum1100dca1995-08-30 23:43:03 +0000365 }
Roger E. Massed9240d11997-01-16 22:05:33 +0000366 dp->di_bsddb = NULL;
367 Py_INCREF(Py_None);
368 return Py_None;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000369}
370
Guido van Rossumdfe8ad91996-07-24 00:51:20 +0000371static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000372bsddb_keys(bsddbobject *dp, PyObject *args)
Guido van Rossum1100dca1995-08-30 23:43:03 +0000373{
Roger E. Massed9240d11997-01-16 22:05:33 +0000374 PyObject *list, *item;
375 DBT krec, drec;
Guido van Rossum730806d1998-04-10 22:27:42 +0000376 char *data=NULL,buf[4096];
Roger E. Massed9240d11997-01-16 22:05:33 +0000377 int status;
378 int err;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000379
Roger E. Massed9240d11997-01-16 22:05:33 +0000380 if (!PyArg_NoArgs(args))
381 return NULL;
Guido van Rossum77eecfa1997-07-17 22:56:01 +0000382 check_bsddbobject_open(dp);
Roger E. Massed9240d11997-01-16 22:05:33 +0000383 list = PyList_New(0);
384 if (list == NULL)
385 return NULL;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000386 BSDDB_BGN_SAVE(dp)
387 status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_FIRST);
388 if (status == 0) {
389 if (krec.size > sizeof(buf)) data = malloc(krec.size);
390 else data = buf;
Andrew M. Kuchling2e095302000-12-15 00:59:32 +0000391 if (data!=NULL) memcpy(data,krec.data,krec.size);
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000392 }
393 BSDDB_END_SAVE(dp)
Andrew M. Kuchling2e095302000-12-15 00:59:32 +0000394 if (data==NULL) return PyErr_NoMemory();
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000395 while (status == 0) {
396 item = PyString_FromStringAndSize(data, (int)krec.size);
397 if (data != buf) free(data);
Roger E. Massed9240d11997-01-16 22:05:33 +0000398 if (item == NULL) {
399 Py_DECREF(list);
400 return NULL;
401 }
402 err = PyList_Append(list, item);
403 Py_DECREF(item);
404 if (err != 0) {
405 Py_DECREF(list);
406 return NULL;
407 }
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000408 BSDDB_BGN_SAVE(dp)
409 status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_NEXT);
410 if (status == 0) {
411 if (krec.size > sizeof(buf)) data = malloc(krec.size);
412 else data = buf;
Andrew M. Kuchling2e095302000-12-15 00:59:32 +0000413 if (data!=NULL) memcpy(data,krec.data,krec.size);
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000414 }
415 BSDDB_END_SAVE(dp)
Andrew M. Kuchling2e095302000-12-15 00:59:32 +0000416 if (data==NULL) return PyErr_NoMemory();
Guido van Rossum1100dca1995-08-30 23:43:03 +0000417 }
Roger E. Massed9240d11997-01-16 22:05:33 +0000418 if (status < 0) {
419 PyErr_SetFromErrno(BsddbError);
420 Py_DECREF(list);
421 return NULL;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000422 }
Roger E. Massed9240d11997-01-16 22:05:33 +0000423 if (dp->di_size < 0)
424 dp->di_size = PyList_Size(list); /* We just did the work */
425 return list;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000426}
427
Guido van Rossumdfe8ad91996-07-24 00:51:20 +0000428static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000429bsddb_has_key(bsddbobject *dp, PyObject *args)
Guido van Rossum1100dca1995-08-30 23:43:03 +0000430{
Roger E. Massed9240d11997-01-16 22:05:33 +0000431 DBT krec, drec;
432 int status;
433 char *data;
434 int size;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000435
Roger E. Massed9240d11997-01-16 22:05:33 +0000436 if (!PyArg_Parse(args, "s#", &data, &size))
437 return NULL;
Guido van Rossum77eecfa1997-07-17 22:56:01 +0000438 check_bsddbobject_open(dp);
Roger E. Massed9240d11997-01-16 22:05:33 +0000439 krec.data = data;
440 krec.size = size;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000441
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000442 BSDDB_BGN_SAVE(dp)
Roger E. Massed9240d11997-01-16 22:05:33 +0000443 status = (dp->di_bsddb->get)(dp->di_bsddb, &krec, &drec, 0);
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000444 BSDDB_END_SAVE(dp)
Roger E. Massed9240d11997-01-16 22:05:33 +0000445 if (status < 0) {
446 PyErr_SetFromErrno(BsddbError);
447 return NULL;
448 }
Guido van Rossum1100dca1995-08-30 23:43:03 +0000449
Roger E. Massed9240d11997-01-16 22:05:33 +0000450 return PyInt_FromLong(status == 0);
Guido van Rossum1100dca1995-08-30 23:43:03 +0000451}
452
Guido van Rossumdfe8ad91996-07-24 00:51:20 +0000453static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000454bsddb_set_location(bsddbobject *dp, PyObject *key)
Guido van Rossum1100dca1995-08-30 23:43:03 +0000455{
Roger E. Massed9240d11997-01-16 22:05:33 +0000456 int status;
457 DBT krec, drec;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000458 char *data,buf[4096];
Roger E. Massed9240d11997-01-16 22:05:33 +0000459 int size;
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000460 PyObject *result;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000461
Roger E. Massed9240d11997-01-16 22:05:33 +0000462 if (!PyArg_Parse(key, "s#", &data, &size))
463 return NULL;
Guido van Rossum77eecfa1997-07-17 22:56:01 +0000464 check_bsddbobject_open(dp);
Roger E. Massed9240d11997-01-16 22:05:33 +0000465 krec.data = data;
466 krec.size = size;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000467
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000468 BSDDB_BGN_SAVE(dp)
Roger E. Massed9240d11997-01-16 22:05:33 +0000469 status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_CURSOR);
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000470 if (status == 0) {
471 if (drec.size > sizeof(buf)) data = malloc(drec.size);
472 else data = buf;
Andrew M. Kuchling2e095302000-12-15 00:59:32 +0000473 if (data!=NULL) memcpy(data,drec.data,drec.size);
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000474 }
475 BSDDB_END_SAVE(dp)
Andrew M. Kuchling2e095302000-12-15 00:59:32 +0000476 if (data==NULL) return PyErr_NoMemory();
Roger E. Massed9240d11997-01-16 22:05:33 +0000477 if (status != 0) {
478 if (status < 0)
479 PyErr_SetFromErrno(BsddbError);
480 else
481 PyErr_SetObject(PyExc_KeyError, key);
482 return NULL;
483 }
Guido van Rossum1100dca1995-08-30 23:43:03 +0000484
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000485 result = Py_BuildValue("s#s#", krec.data, krec.size, data, drec.size);
486 if (data != buf) free(data);
487 return result;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000488}
489
Guido van Rossumdfe8ad91996-07-24 00:51:20 +0000490static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000491bsddb_seq(bsddbobject *dp, PyObject *args, int sequence_request)
Guido van Rossum1100dca1995-08-30 23:43:03 +0000492{
Roger E. Massed9240d11997-01-16 22:05:33 +0000493 int status;
494 DBT krec, drec;
Guido van Rossum730806d1998-04-10 22:27:42 +0000495 char *kdata=NULL,kbuf[4096];
496 char *ddata=NULL,dbuf[4096];
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000497 PyObject *result;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000498
Roger E. Massed9240d11997-01-16 22:05:33 +0000499 if (!PyArg_NoArgs(args))
500 return NULL;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000501
Guido van Rossum77eecfa1997-07-17 22:56:01 +0000502 check_bsddbobject_open(dp);
Roger E. Massed9240d11997-01-16 22:05:33 +0000503 krec.data = 0;
504 krec.size = 0;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000505
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000506 BSDDB_BGN_SAVE(dp)
Roger E. Massed9240d11997-01-16 22:05:33 +0000507 status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec,
508 &drec, sequence_request);
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000509 if (status == 0) {
510 if (krec.size > sizeof(kbuf)) kdata = malloc(krec.size);
511 else kdata = kbuf;
Andrew M. Kuchling2e095302000-12-15 00:59:32 +0000512 if (kdata!=NULL) memcpy(kdata,krec.data,krec.size);
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000513 if (drec.size > sizeof(dbuf)) ddata = malloc(drec.size);
514 else ddata = dbuf;
Andrew M. Kuchling2e095302000-12-15 00:59:32 +0000515 if (ddata!=NULL) memcpy(ddata,drec.data,drec.size);
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000516 }
517 BSDDB_END_SAVE(dp)
Andrew M. Kuchling2e095302000-12-15 00:59:32 +0000518 if (status == 0) {
519 if ((kdata==NULL) || (ddata==NULL))
520 return PyErr_NoMemory();
521 }
522 else {
523 /* (status != 0) */
Roger E. Massed9240d11997-01-16 22:05:33 +0000524 if (status < 0)
525 PyErr_SetFromErrno(BsddbError);
526 else
527 PyErr_SetObject(PyExc_KeyError, args);
528 return NULL;
529 }
Guido van Rossum1100dca1995-08-30 23:43:03 +0000530
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000531 result = Py_BuildValue("s#s#", kdata, krec.size, ddata, drec.size);
532 if (kdata != kbuf) free(kdata);
533 if (ddata != dbuf) free(ddata);
534 return result;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000535}
536
Guido van Rossumdfe8ad91996-07-24 00:51:20 +0000537static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000538bsddb_next(bsddbobject *dp, PyObject *key)
Guido van Rossum1100dca1995-08-30 23:43:03 +0000539{
Roger E. Massed9240d11997-01-16 22:05:33 +0000540 return bsddb_seq(dp, key, R_NEXT);
Guido van Rossum1100dca1995-08-30 23:43:03 +0000541}
Guido van Rossumdfe8ad91996-07-24 00:51:20 +0000542static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000543bsddb_previous(bsddbobject *dp, PyObject *key)
Guido van Rossum1100dca1995-08-30 23:43:03 +0000544{
Roger E. Massed9240d11997-01-16 22:05:33 +0000545 return bsddb_seq(dp, key, R_PREV);
Guido van Rossum1100dca1995-08-30 23:43:03 +0000546}
Guido van Rossumdfe8ad91996-07-24 00:51:20 +0000547static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000548bsddb_first(bsddbobject *dp, PyObject *key)
Guido van Rossum1100dca1995-08-30 23:43:03 +0000549{
Roger E. Massed9240d11997-01-16 22:05:33 +0000550 return bsddb_seq(dp, key, R_FIRST);
Guido van Rossum1100dca1995-08-30 23:43:03 +0000551}
Guido van Rossumdfe8ad91996-07-24 00:51:20 +0000552static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000553bsddb_last(bsddbobject *dp, PyObject *key)
Guido van Rossum1100dca1995-08-30 23:43:03 +0000554{
Roger E. Massed9240d11997-01-16 22:05:33 +0000555 return bsddb_seq(dp, key, R_LAST);
Guido van Rossum1100dca1995-08-30 23:43:03 +0000556}
Guido van Rossumdfe8ad91996-07-24 00:51:20 +0000557static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000558bsddb_sync(bsddbobject *dp, PyObject *args)
Guido van Rossum1100dca1995-08-30 23:43:03 +0000559{
Roger E. Massed9240d11997-01-16 22:05:33 +0000560 int status;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000561
Guido van Rossum77eecfa1997-07-17 22:56:01 +0000562 if (!PyArg_NoArgs(args))
563 return NULL;
564 check_bsddbobject_open(dp);
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000565 BSDDB_BGN_SAVE(dp)
Roger E. Massed9240d11997-01-16 22:05:33 +0000566 status = (dp->di_bsddb->sync)(dp->di_bsddb, 0);
Guido van Rossum4f199ea1998-04-09 20:56:35 +0000567 BSDDB_END_SAVE(dp)
Roger E. Massed9240d11997-01-16 22:05:33 +0000568 if (status != 0) {
569 PyErr_SetFromErrno(BsddbError);
570 return NULL;
571 }
572 return PyInt_FromLong(status = 0);
Guido van Rossum1100dca1995-08-30 23:43:03 +0000573}
Guido van Rossumdfe8ad91996-07-24 00:51:20 +0000574static PyMethodDef bsddb_methods[] = {
Roger E. Massed9240d11997-01-16 22:05:33 +0000575 {"close", (PyCFunction)bsddb_close},
576 {"keys", (PyCFunction)bsddb_keys},
577 {"has_key", (PyCFunction)bsddb_has_key},
578 {"set_location", (PyCFunction)bsddb_set_location},
579 {"next", (PyCFunction)bsddb_next},
580 {"previous", (PyCFunction)bsddb_previous},
581 {"first", (PyCFunction)bsddb_first},
582 {"last", (PyCFunction)bsddb_last},
583 {"sync", (PyCFunction)bsddb_sync},
584 {NULL, NULL} /* sentinel */
Guido van Rossum1100dca1995-08-30 23:43:03 +0000585};
586
Guido van Rossumdfe8ad91996-07-24 00:51:20 +0000587static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000588bsddb_getattr(PyObject *dp, char *name)
Guido van Rossum1100dca1995-08-30 23:43:03 +0000589{
Roger E. Massed9240d11997-01-16 22:05:33 +0000590 return Py_FindMethod(bsddb_methods, dp, name);
Guido van Rossum1100dca1995-08-30 23:43:03 +0000591}
592
Guido van Rossumdfe8ad91996-07-24 00:51:20 +0000593static PyTypeObject Bsddbtype = {
Roger E. Massed9240d11997-01-16 22:05:33 +0000594 PyObject_HEAD_INIT(NULL)
595 0,
596 "bsddb",
597 sizeof(bsddbobject),
598 0,
599 (destructor)bsddb_dealloc, /*tp_dealloc*/
600 0, /*tp_print*/
601 (getattrfunc)bsddb_getattr, /*tp_getattr*/
602 0, /*tp_setattr*/
603 0, /*tp_compare*/
604 0, /*tp_repr*/
605 0, /*tp_as_number*/
606 0, /*tp_as_sequence*/
607 &bsddb_as_mapping, /*tp_as_mapping*/
Guido van Rossum1100dca1995-08-30 23:43:03 +0000608};
609
Guido van Rossumdfe8ad91996-07-24 00:51:20 +0000610static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000611bsdhashopen(PyObject *self, PyObject *args)
Guido van Rossum1100dca1995-08-30 23:43:03 +0000612{
Roger E. Massed9240d11997-01-16 22:05:33 +0000613 char *file;
614 char *flag = NULL;
615 int flags = O_RDONLY;
616 int mode = 0666;
617 int bsize = 0;
618 int ffactor = 0;
619 int nelem = 0;
620 int cachesize = 0;
621 int hash = 0; /* XXX currently ignored */
622 int lorder = 0;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000623
Guido van Rossum43713e52000-02-29 13:59:29 +0000624 if (!PyArg_ParseTuple(args, "s|siiiiiii:hashopen",
Roger E. Massed9240d11997-01-16 22:05:33 +0000625 &file, &flag, &mode,
626 &bsize, &ffactor, &nelem, &cachesize,
627 &hash, &lorder))
628 return NULL;
629 if (flag != NULL) {
630 /* XXX need to pass O_EXCL, O_EXLOCK, O_NONBLOCK, O_SHLOCK */
631 if (flag[0] == 'r')
632 flags = O_RDONLY;
633 else if (flag[0] == 'w')
634 flags = O_RDWR;
635 else if (flag[0] == 'c')
636 flags = O_RDWR|O_CREAT;
637 else if (flag[0] == 'n')
638 flags = O_RDWR|O_CREAT|O_TRUNC;
639 else {
640 PyErr_SetString(BsddbError,
641 "Flag should begin with 'r', 'w', 'c' or 'n'");
642 return NULL;
643 }
644 if (flag[1] == 'l') {
Guido van Rossum1100dca1995-08-30 23:43:03 +0000645#if defined(O_EXLOCK) && defined(O_SHLOCK)
Roger E. Massed9240d11997-01-16 22:05:33 +0000646 if (flag[0] == 'r')
647 flags |= O_SHLOCK;
648 else
649 flags |= O_EXLOCK;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000650#else
Roger E. Massed9240d11997-01-16 22:05:33 +0000651 PyErr_SetString(BsddbError,
652 "locking not supported on this platform");
653 return NULL;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000654#endif
Roger E. Massed9240d11997-01-16 22:05:33 +0000655 }
Guido van Rossum1100dca1995-08-30 23:43:03 +0000656 }
Roger E. Massed9240d11997-01-16 22:05:33 +0000657 return newdbhashobject(file, flags, mode,
658 bsize, ffactor, nelem, cachesize, hash, lorder);
Guido van Rossum1100dca1995-08-30 23:43:03 +0000659}
660
Guido van Rossumdfe8ad91996-07-24 00:51:20 +0000661static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000662bsdbtopen(PyObject *self, PyObject *args)
Guido van Rossum1100dca1995-08-30 23:43:03 +0000663{
Roger E. Massed9240d11997-01-16 22:05:33 +0000664 char *file;
665 char *flag = NULL;
666 int flags = O_RDONLY;
667 int mode = 0666;
668 int cachesize = 0;
669 int maxkeypage = 0;
670 int minkeypage = 0;
671 int btflags = 0;
672 unsigned int psize = 0;
673 int lorder = 0;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000674
Guido van Rossum43713e52000-02-29 13:59:29 +0000675 if (!PyArg_ParseTuple(args, "s|siiiiiii:btopen",
Roger E. Massed9240d11997-01-16 22:05:33 +0000676 &file, &flag, &mode,
677 &btflags, &cachesize, &maxkeypage, &minkeypage,
678 &psize, &lorder))
679 return NULL;
680 if (flag != NULL) {
681 /* XXX need to pass O_EXCL, O_EXLOCK, O_NONBLOCK, O_SHLOCK */
682 if (flag[0] == 'r')
683 flags = O_RDONLY;
684 else if (flag[0] == 'w')
685 flags = O_RDWR;
686 else if (flag[0] == 'c')
687 flags = O_RDWR|O_CREAT;
688 else if (flag[0] == 'n')
689 flags = O_RDWR|O_CREAT|O_TRUNC;
690 else {
691 PyErr_SetString(BsddbError,
692 "Flag should begin with 'r', 'w', 'c' or 'n'");
693 return NULL;
694 }
695 if (flag[1] == 'l') {
Guido van Rossum1100dca1995-08-30 23:43:03 +0000696#if defined(O_EXLOCK) && defined(O_SHLOCK)
Roger E. Massed9240d11997-01-16 22:05:33 +0000697 if (flag[0] == 'r')
698 flags |= O_SHLOCK;
699 else
700 flags |= O_EXLOCK;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000701#else
Roger E. Massed9240d11997-01-16 22:05:33 +0000702 PyErr_SetString(BsddbError,
703 "locking not supported on this platform");
704 return NULL;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000705#endif
Roger E. Massed9240d11997-01-16 22:05:33 +0000706 }
Guido van Rossum1100dca1995-08-30 23:43:03 +0000707 }
Roger E. Massed9240d11997-01-16 22:05:33 +0000708 return newdbbtobject(file, flags, mode,
709 btflags, cachesize, maxkeypage, minkeypage,
710 psize, lorder);
Guido van Rossum1100dca1995-08-30 23:43:03 +0000711}
Guido van Rossumdd96ca71996-05-23 22:57:54 +0000712
Guido van Rossumdfe8ad91996-07-24 00:51:20 +0000713static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000714bsdrnopen(PyObject *self, PyObject *args)
Guido van Rossum1100dca1995-08-30 23:43:03 +0000715{
Roger E. Massed9240d11997-01-16 22:05:33 +0000716 char *file;
717 char *flag = NULL;
718 int flags = O_RDONLY;
719 int mode = 0666;
720 int cachesize = 0;
721 int rnflags = 0;
722 unsigned int psize = 0;
723 int lorder = 0;
724 size_t reclen = 0;
725 char *bval = "";
726 char *bfname = NULL;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000727
Guido van Rossum43713e52000-02-29 13:59:29 +0000728 if (!PyArg_ParseTuple(args, "s|siiiiiiss:rnopen",
Roger E. Massed9240d11997-01-16 22:05:33 +0000729 &file, &flag, &mode,
730 &rnflags, &cachesize, &psize, &lorder,
731 &reclen, &bval, &bfname))
732 return NULL;
733
734# if 0
735 printf("file: %s\n", file);
736 printf("flag: %s\n", flag);
737 printf("mode: %d\n", mode);
738 printf("rnflags: 0x%x\n", rnflags);
739 printf("cachesize: %d\n", cachesize);
740 printf("psize: %d\n", psize);
741 printf("lorder: %d\n", 0);
742 printf("reclen: %d\n", reclen);
743 printf("bval: %c\n", bval[0]);
744 printf("bfname %s\n", bfname);
Guido van Rossum1100dca1995-08-30 23:43:03 +0000745#endif
Roger E. Massed9240d11997-01-16 22:05:33 +0000746
747 if (flag != NULL) {
748 /* XXX need to pass O_EXCL, O_EXLOCK, O_NONBLOCK, O_SHLOCK */
749 if (flag[0] == 'r')
750 flags = O_RDONLY;
751 else if (flag[0] == 'w')
752 flags = O_RDWR;
753 else if (flag[0] == 'c')
754 flags = O_RDWR|O_CREAT;
755 else if (flag[0] == 'n')
756 flags = O_RDWR|O_CREAT|O_TRUNC;
757 else {
758 PyErr_SetString(BsddbError,
759 "Flag should begin with 'r', 'w', 'c' or 'n'");
760 return NULL;
761 }
762 if (flag[1] == 'l') {
763#if defined(O_EXLOCK) && defined(O_SHLOCK)
764 if (flag[0] == 'r')
765 flags |= O_SHLOCK;
766 else
767 flags |= O_EXLOCK;
768#else
769 PyErr_SetString(BsddbError,
770 "locking not supported on this platform");
771 return NULL;
772#endif
773 }
774 else if (flag[1] != '\0') {
775 PyErr_SetString(BsddbError,
776 "Flag char 2 should be 'l' or absent");
777 return NULL;
778 }
Guido van Rossum1100dca1995-08-30 23:43:03 +0000779 }
Roger E. Massed9240d11997-01-16 22:05:33 +0000780 return newdbrnobject(file, flags, mode, rnflags, cachesize,
781 psize, lorder, reclen, bval[0], bfname);
Guido van Rossum1100dca1995-08-30 23:43:03 +0000782}
783
Guido van Rossumdfe8ad91996-07-24 00:51:20 +0000784static PyMethodDef bsddbmodule_methods[] = {
Roger E. Massed9240d11997-01-16 22:05:33 +0000785 {"hashopen", (PyCFunction)bsdhashopen, 1},
786 {"btopen", (PyCFunction)bsdbtopen, 1},
787 {"rnopen", (PyCFunction)bsdrnopen, 1},
788 {0, 0},
Guido van Rossum1100dca1995-08-30 23:43:03 +0000789};
790
Guido van Rossum3886bb61998-12-04 18:50:17 +0000791DL_EXPORT(void)
Thomas Wouters58d05102000-07-24 14:43:35 +0000792initbsddb(void) {
Roger E. Massed9240d11997-01-16 22:05:33 +0000793 PyObject *m, *d;
Guido van Rossum1100dca1995-08-30 23:43:03 +0000794
Roger E. Massed9240d11997-01-16 22:05:33 +0000795 Bsddbtype.ob_type = &PyType_Type;
796 m = Py_InitModule("bsddb", bsddbmodule_methods);
797 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000798 BsddbError = PyErr_NewException("bsddb.error", NULL, NULL);
799 if (BsddbError != NULL)
800 PyDict_SetItemString(d, "error", BsddbError);
Guido van Rossum1100dca1995-08-30 23:43:03 +0000801}