Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 1 | /* Berkeley DB interface. |
| 2 | Author: Michael McLay |
| 3 | Hacked: Guido van Rossum |
| 4 | Btree and Recno additions plus sequence methods: David Ely |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 5 | Hacked by Gustavo Niemeyer <niemeyer@conectiva.com> fixing recno |
| 6 | support. |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 7 | |
| 8 | XXX To do: |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 9 | - provide a way to access the various hash functions |
| 10 | - support more open flags |
Guido van Rossum | 675e994 | 1999-09-20 13:28:18 +0000 | [diff] [blame] | 11 | |
| 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 Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 16 | #include "Python.h" |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 17 | #ifdef WITH_THREAD |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 18 | #include "pythread.h" |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 19 | #endif |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 20 | |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 21 | #ifdef HAVE_UNISTD_H |
| 22 | #include <unistd.h> |
| 23 | #endif |
| 24 | |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 25 | #include <sys/types.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <fcntl.h> |
Fred Drake | c9cb847 | 2000-08-31 16:11:07 +0000 | [diff] [blame] | 28 | #ifdef HAVE_DB_185_H |
| 29 | #include <db_185.h> |
| 30 | #else |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 31 | #include <db.h> |
Fred Drake | c9cb847 | 2000-08-31 16:11:07 +0000 | [diff] [blame] | 32 | #endif |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 33 | /* Please don't include internal header files of the Berkeley db package |
| 34 | (it messes up the info required in the Setup file) */ |
| 35 | |
| 36 | typedef struct { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 37 | PyObject_HEAD |
| 38 | DB *di_bsddb; |
| 39 | int di_size; /* -1 means recompute */ |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 40 | int di_type; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 41 | #ifdef WITH_THREAD |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 42 | PyThread_type_lock di_lock; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 43 | #endif |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 44 | } bsddbobject; |
| 45 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 46 | staticforward PyTypeObject Bsddbtype; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 47 | |
| 48 | #define is_bsddbobject(v) ((v)->ob_type == &Bsddbtype) |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 49 | #define check_bsddbobject_open(v, r) if ((v)->di_bsddb == NULL) \ |
Fred Drake | c9f7c26 | 2001-02-19 21:16:00 +0000 | [diff] [blame] | 50 | { PyErr_SetString(BsddbError, \ |
| 51 | "BSDDB object has already been closed"); \ |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 52 | return r; } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 54 | static PyObject *BsddbError; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 56 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 57 | newdbhashobject(char *file, int flags, int mode, |
Fred Drake | c9f7c26 | 2001-02-19 21:16:00 +0000 | [diff] [blame] | 58 | int bsize, int ffactor, int nelem, int cachesize, |
| 59 | int hash, int lorder) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 60 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 61 | bsddbobject *dp; |
| 62 | HASHINFO info; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 64 | if ((dp = PyObject_New(bsddbobject, &Bsddbtype)) == NULL) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 65 | return NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 66 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 67 | info.bsize = bsize; |
| 68 | info.ffactor = ffactor; |
| 69 | info.nelem = nelem; |
| 70 | info.cachesize = cachesize; |
| 71 | info.hash = NULL; /* XXX should derive from hash argument */ |
| 72 | info.lorder = lorder; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | 6beb479 | 1996-09-11 23:22:25 +0000 | [diff] [blame] | 74 | #ifdef O_BINARY |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 75 | flags |= O_BINARY; |
Guido van Rossum | 6beb479 | 1996-09-11 23:22:25 +0000 | [diff] [blame] | 76 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 77 | Py_BEGIN_ALLOW_THREADS |
| 78 | dp->di_bsddb = dbopen(file, flags, mode, DB_HASH, &info); |
| 79 | Py_END_ALLOW_THREADS |
| 80 | if (dp->di_bsddb == NULL) { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 81 | PyErr_SetFromErrno(BsddbError); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 82 | #ifdef WITH_THREAD |
| 83 | dp->di_lock = NULL; |
| 84 | #endif |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 85 | Py_DECREF(dp); |
| 86 | return NULL; |
| 87 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 88 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 89 | dp->di_size = -1; |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 90 | dp->di_type = DB_HASH; |
| 91 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 92 | #ifdef WITH_THREAD |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 93 | dp->di_lock = PyThread_allocate_lock(); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 94 | if (dp->di_lock == NULL) { |
| 95 | PyErr_SetString(BsddbError, "can't allocate lock"); |
| 96 | Py_DECREF(dp); |
| 97 | return NULL; |
| 98 | } |
| 99 | #endif |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 100 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +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 * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 105 | newdbbtobject(char *file, int flags, int mode, |
Fred Drake | c9f7c26 | 2001-02-19 21:16:00 +0000 | [diff] [blame] | 106 | int btflags, int cachesize, int maxkeypage, |
| 107 | int minkeypage, int psize, int lorder) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 108 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 109 | bsddbobject *dp; |
| 110 | BTREEINFO info; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 111 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 112 | if ((dp = PyObject_New(bsddbobject, &Bsddbtype)) == NULL) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 113 | return NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 114 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 115 | info.flags = btflags; |
| 116 | info.cachesize = cachesize; |
| 117 | info.maxkeypage = maxkeypage; |
| 118 | info.minkeypage = minkeypage; |
| 119 | info.psize = psize; |
| 120 | info.lorder = lorder; |
| 121 | info.compare = 0; /* Use default comparison functions, for now..*/ |
| 122 | info.prefix = 0; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 123 | |
Guido van Rossum | 6beb479 | 1996-09-11 23:22:25 +0000 | [diff] [blame] | 124 | #ifdef O_BINARY |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 125 | flags |= O_BINARY; |
Guido van Rossum | 6beb479 | 1996-09-11 23:22:25 +0000 | [diff] [blame] | 126 | #endif |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 127 | Py_BEGIN_ALLOW_THREADS |
| 128 | dp->di_bsddb = dbopen(file, flags, mode, DB_BTREE, &info); |
| 129 | Py_END_ALLOW_THREADS |
| 130 | if (dp->di_bsddb == NULL) { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 131 | PyErr_SetFromErrno(BsddbError); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 132 | #ifdef WITH_THREAD |
| 133 | dp->di_lock = NULL; |
| 134 | #endif |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 135 | Py_DECREF(dp); |
| 136 | return NULL; |
| 137 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 138 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 139 | dp->di_size = -1; |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 140 | dp->di_type = DB_BTREE; |
| 141 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 142 | #ifdef WITH_THREAD |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 143 | dp->di_lock = PyThread_allocate_lock(); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 144 | if (dp->di_lock == NULL) { |
| 145 | PyErr_SetString(BsddbError, "can't allocate lock"); |
| 146 | Py_DECREF(dp); |
| 147 | return NULL; |
| 148 | } |
| 149 | #endif |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 150 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 151 | return (PyObject *)dp; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 154 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 155 | newdbrnobject(char *file, int flags, int mode, |
Fred Drake | c9f7c26 | 2001-02-19 21:16:00 +0000 | [diff] [blame] | 156 | int rnflags, int cachesize, int psize, int lorder, |
| 157 | size_t reclen, u_char bval, char *bfname) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 158 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 159 | bsddbobject *dp; |
| 160 | RECNOINFO info; |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 161 | int fd; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 162 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 163 | if ((dp = PyObject_New(bsddbobject, &Bsddbtype)) == NULL) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 164 | return NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 165 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 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; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 173 | |
Guido van Rossum | 6beb479 | 1996-09-11 23:22:25 +0000 | [diff] [blame] | 174 | #ifdef O_BINARY |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 175 | flags |= O_BINARY; |
Guido van Rossum | 6beb479 | 1996-09-11 23:22:25 +0000 | [diff] [blame] | 176 | #endif |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 177 | /* This is a hack to avoid a dbopen() bug that happens when |
| 178 | * it fails. */ |
| 179 | fd = open(file, flags); |
| 180 | if (fd == -1) { |
| 181 | dp->di_bsddb = NULL; |
| 182 | } |
| 183 | else { |
| 184 | close(fd); |
| 185 | Py_BEGIN_ALLOW_THREADS |
| 186 | dp->di_bsddb = dbopen(file, flags, mode, DB_RECNO, &info); |
| 187 | Py_END_ALLOW_THREADS |
| 188 | } |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 189 | if (dp->di_bsddb == NULL) { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 190 | PyErr_SetFromErrno(BsddbError); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 191 | #ifdef WITH_THREAD |
| 192 | dp->di_lock = NULL; |
| 193 | #endif |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 194 | Py_DECREF(dp); |
| 195 | return NULL; |
| 196 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 197 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 198 | dp->di_size = -1; |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 199 | dp->di_type = DB_RECNO; |
| 200 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 201 | #ifdef WITH_THREAD |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 202 | dp->di_lock = PyThread_allocate_lock(); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 203 | if (dp->di_lock == NULL) { |
| 204 | PyErr_SetString(BsddbError, "can't allocate lock"); |
| 205 | Py_DECREF(dp); |
| 206 | return NULL; |
| 207 | } |
| 208 | #endif |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 209 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 210 | return (PyObject *)dp; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 213 | static void |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 214 | bsddb_dealloc(bsddbobject *dp) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 215 | { |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 216 | #ifdef WITH_THREAD |
| 217 | if (dp->di_lock) { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 218 | PyThread_acquire_lock(dp->di_lock, 0); |
| 219 | PyThread_release_lock(dp->di_lock); |
| 220 | PyThread_free_lock(dp->di_lock); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 221 | dp->di_lock = NULL; |
| 222 | } |
| 223 | #endif |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 224 | if (dp->di_bsddb != NULL) { |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 225 | int status; |
| 226 | Py_BEGIN_ALLOW_THREADS |
| 227 | status = (dp->di_bsddb->close)(dp->di_bsddb); |
| 228 | Py_END_ALLOW_THREADS |
| 229 | if (status != 0) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 230 | fprintf(stderr, |
| 231 | "Python bsddb: close errno %d in dealloc\n", |
| 232 | errno); |
| 233 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 234 | PyObject_Del(dp); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 237 | #ifdef WITH_THREAD |
Fred Drake | c9f7c26 | 2001-02-19 21:16:00 +0000 | [diff] [blame] | 238 | #define BSDDB_BGN_SAVE(_dp) \ |
| 239 | Py_BEGIN_ALLOW_THREADS PyThread_acquire_lock(_dp->di_lock,1); |
| 240 | #define BSDDB_END_SAVE(_dp) \ |
| 241 | PyThread_release_lock(_dp->di_lock); Py_END_ALLOW_THREADS |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 242 | #else |
| 243 | #define BSDDB_BGN_SAVE(_dp) Py_BEGIN_ALLOW_THREADS |
| 244 | #define BSDDB_END_SAVE(_dp) Py_END_ALLOW_THREADS |
| 245 | #endif |
| 246 | |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 247 | static int |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 248 | bsddb_length(bsddbobject *dp) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 249 | { |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 250 | check_bsddbobject_open(dp, -1); |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 251 | if (dp->di_size < 0) { |
| 252 | DBT krec, drec; |
| 253 | int status; |
| 254 | int size = 0; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 255 | BSDDB_BGN_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 256 | for (status = (dp->di_bsddb->seq)(dp->di_bsddb, |
| 257 | &krec, &drec,R_FIRST); |
| 258 | status == 0; |
| 259 | status = (dp->di_bsddb->seq)(dp->di_bsddb, |
| 260 | &krec, &drec, R_NEXT)) |
| 261 | size++; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 262 | BSDDB_END_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 263 | if (status < 0) { |
| 264 | PyErr_SetFromErrno(BsddbError); |
| 265 | return -1; |
| 266 | } |
| 267 | dp->di_size = size; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 268 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 269 | return dp->di_size; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 272 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 273 | bsddb_subscript(bsddbobject *dp, PyObject *key) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 274 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 275 | int status; |
| 276 | DBT krec, drec; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 277 | char *data,buf[4096]; |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 278 | int size; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 279 | PyObject *result; |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 280 | recno_t recno; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 281 | |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 282 | if (dp->di_type == DB_RECNO) { |
| 283 | if (!PyArg_Parse(key, "i", &recno)) { |
| 284 | PyErr_SetString(PyExc_TypeError, |
| 285 | "key type must be integer"); |
| 286 | return NULL; |
| 287 | } |
| 288 | krec.data = &recno; |
| 289 | krec.size = sizeof(recno); |
| 290 | } |
| 291 | else { |
| 292 | if (!PyArg_Parse(key, "s#", &data, &size)) { |
| 293 | PyErr_SetString(PyExc_TypeError, |
| 294 | "key type must be string"); |
| 295 | return NULL; |
| 296 | } |
| 297 | krec.data = data; |
| 298 | krec.size = size; |
| 299 | } |
| 300 | check_bsddbobject_open(dp, NULL); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 301 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 302 | BSDDB_BGN_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 303 | status = (dp->di_bsddb->get)(dp->di_bsddb, &krec, &drec, 0); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 304 | if (status == 0) { |
| 305 | if (drec.size > sizeof(buf)) data = malloc(drec.size); |
| 306 | else data = buf; |
Andrew M. Kuchling | 2e09530 | 2000-12-15 00:59:32 +0000 | [diff] [blame] | 307 | if (data!=NULL) memcpy(data,drec.data,drec.size); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 308 | } |
| 309 | BSDDB_END_SAVE(dp) |
Andrew M. Kuchling | 2e09530 | 2000-12-15 00:59:32 +0000 | [diff] [blame] | 310 | if (data==NULL) return PyErr_NoMemory(); |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 311 | if (status != 0) { |
| 312 | if (status < 0) |
| 313 | PyErr_SetFromErrno(BsddbError); |
| 314 | else |
| 315 | PyErr_SetObject(PyExc_KeyError, key); |
| 316 | return NULL; |
| 317 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 318 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 319 | result = PyString_FromStringAndSize(data, (int)drec.size); |
| 320 | if (data != buf) free(data); |
| 321 | return result; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | static int |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 325 | bsddb_ass_sub(bsddbobject *dp, PyObject *key, PyObject *value) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 326 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 327 | int status; |
| 328 | DBT krec, drec; |
| 329 | char *data; |
| 330 | int size; |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 331 | recno_t recno; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 332 | |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 333 | if (dp->di_type == DB_RECNO) { |
| 334 | if (!PyArg_Parse(key, "i", &recno)) { |
| 335 | PyErr_SetString(PyExc_TypeError, |
| 336 | "bsddb key type must be integer"); |
| 337 | return -1; |
| 338 | } |
| 339 | krec.data = &recno; |
| 340 | krec.size = sizeof(recno); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 341 | } |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 342 | else { |
| 343 | if (!PyArg_Parse(key, "s#", &data, &size)) { |
| 344 | PyErr_SetString(PyExc_TypeError, |
| 345 | "bsddb key type must be string"); |
| 346 | return -1; |
| 347 | } |
| 348 | krec.data = data; |
| 349 | krec.size = size; |
| 350 | } |
| 351 | check_bsddbobject_open(dp, -1); |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 352 | dp->di_size = -1; |
| 353 | if (value == NULL) { |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 354 | BSDDB_BGN_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 355 | status = (dp->di_bsddb->del)(dp->di_bsddb, &krec, 0); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 356 | BSDDB_END_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 357 | } |
| 358 | else { |
| 359 | if (!PyArg_Parse(value, "s#", &data, &size)) { |
| 360 | PyErr_SetString(PyExc_TypeError, |
| 361 | "bsddb value type must be string"); |
| 362 | return -1; |
| 363 | } |
| 364 | drec.data = data; |
| 365 | drec.size = size; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 366 | BSDDB_BGN_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 367 | status = (dp->di_bsddb->put)(dp->di_bsddb, &krec, &drec, 0); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 368 | BSDDB_END_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 369 | } |
| 370 | if (status != 0) { |
| 371 | if (status < 0) |
| 372 | PyErr_SetFromErrno(BsddbError); |
| 373 | else |
| 374 | PyErr_SetObject(PyExc_KeyError, key); |
| 375 | return -1; |
| 376 | } |
| 377 | return 0; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 380 | static PyMappingMethods bsddb_as_mapping = { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 381 | (inquiry)bsddb_length, /*mp_length*/ |
| 382 | (binaryfunc)bsddb_subscript, /*mp_subscript*/ |
| 383 | (objobjargproc)bsddb_ass_sub, /*mp_ass_subscript*/ |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 384 | }; |
| 385 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 386 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 387 | bsddb_close(bsddbobject *dp, PyObject *args) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 388 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 389 | if (!PyArg_NoArgs(args)) |
| 390 | return NULL; |
| 391 | if (dp->di_bsddb != NULL) { |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 392 | int status; |
| 393 | BSDDB_BGN_SAVE(dp) |
| 394 | status = (dp->di_bsddb->close)(dp->di_bsddb); |
| 395 | BSDDB_END_SAVE(dp) |
| 396 | if (status != 0) { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 397 | dp->di_bsddb = NULL; |
| 398 | PyErr_SetFromErrno(BsddbError); |
| 399 | return NULL; |
| 400 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 401 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 402 | dp->di_bsddb = NULL; |
| 403 | Py_INCREF(Py_None); |
| 404 | return Py_None; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 407 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 408 | bsddb_keys(bsddbobject *dp, PyObject *args) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 409 | { |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 410 | PyObject *list, *item=NULL; |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 411 | DBT krec, drec; |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 412 | char *data=NULL,buf[4096]; |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 413 | int status; |
| 414 | int err; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 415 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 416 | if (!PyArg_NoArgs(args)) |
| 417 | return NULL; |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 418 | check_bsddbobject_open(dp, NULL); |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 419 | list = PyList_New(0); |
| 420 | if (list == NULL) |
| 421 | return NULL; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 422 | BSDDB_BGN_SAVE(dp) |
| 423 | status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_FIRST); |
| 424 | if (status == 0) { |
| 425 | if (krec.size > sizeof(buf)) data = malloc(krec.size); |
| 426 | else data = buf; |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 427 | if (data != NULL) memcpy(data,krec.data,krec.size); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 428 | } |
| 429 | BSDDB_END_SAVE(dp) |
Andrew M. Kuchling | 2e09530 | 2000-12-15 00:59:32 +0000 | [diff] [blame] | 430 | if (data==NULL) return PyErr_NoMemory(); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 431 | while (status == 0) { |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 432 | if (dp->di_type == DB_RECNO) |
| 433 | item = PyInt_FromLong(*((int*)data)); |
| 434 | else |
| 435 | item = PyString_FromStringAndSize(data, |
| 436 | (int)krec.size); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 437 | if (data != buf) free(data); |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 438 | if (item == NULL) { |
| 439 | Py_DECREF(list); |
| 440 | return NULL; |
| 441 | } |
| 442 | err = PyList_Append(list, item); |
| 443 | Py_DECREF(item); |
| 444 | if (err != 0) { |
| 445 | Py_DECREF(list); |
| 446 | return NULL; |
| 447 | } |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 448 | BSDDB_BGN_SAVE(dp) |
Fred Drake | c9f7c26 | 2001-02-19 21:16:00 +0000 | [diff] [blame] | 449 | status = (dp->di_bsddb->seq) |
| 450 | (dp->di_bsddb, &krec, &drec, R_NEXT); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 451 | if (status == 0) { |
Fred Drake | c9f7c26 | 2001-02-19 21:16:00 +0000 | [diff] [blame] | 452 | if (krec.size > sizeof(buf)) |
| 453 | data = malloc(krec.size); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 454 | else data = buf; |
Fred Drake | c9f7c26 | 2001-02-19 21:16:00 +0000 | [diff] [blame] | 455 | if (data != NULL) |
| 456 | memcpy(data,krec.data,krec.size); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 457 | } |
| 458 | BSDDB_END_SAVE(dp) |
Fred Drake | c9f7c26 | 2001-02-19 21:16:00 +0000 | [diff] [blame] | 459 | if (data == NULL) return PyErr_NoMemory(); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 460 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 461 | if (status < 0) { |
| 462 | PyErr_SetFromErrno(BsddbError); |
| 463 | Py_DECREF(list); |
| 464 | return NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 465 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 466 | if (dp->di_size < 0) |
| 467 | dp->di_size = PyList_Size(list); /* We just did the work */ |
| 468 | return list; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 471 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 472 | bsddb_has_key(bsddbobject *dp, PyObject *args) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 473 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 474 | DBT krec, drec; |
| 475 | int status; |
| 476 | char *data; |
| 477 | int size; |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 478 | recno_t recno; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 479 | |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 480 | if (dp->di_type == DB_RECNO) { |
| 481 | if (!PyArg_Parse(args, "i", &recno)) { |
| 482 | PyErr_SetString(PyExc_TypeError, |
| 483 | "key type must be integer"); |
| 484 | return NULL; |
| 485 | } |
| 486 | krec.data = &recno; |
| 487 | krec.size = sizeof(recno); |
| 488 | } |
| 489 | else { |
| 490 | if (!PyArg_Parse(args, "s#", &data, &size)) { |
| 491 | PyErr_SetString(PyExc_TypeError, |
| 492 | "key type must be string"); |
| 493 | return NULL; |
| 494 | } |
| 495 | krec.data = data; |
| 496 | krec.size = size; |
| 497 | } |
| 498 | check_bsddbobject_open(dp, NULL); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 499 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 500 | BSDDB_BGN_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 501 | status = (dp->di_bsddb->get)(dp->di_bsddb, &krec, &drec, 0); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 502 | BSDDB_END_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 503 | if (status < 0) { |
| 504 | PyErr_SetFromErrno(BsddbError); |
| 505 | return NULL; |
| 506 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 507 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 508 | return PyInt_FromLong(status == 0); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 511 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 512 | bsddb_set_location(bsddbobject *dp, PyObject *key) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 513 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 514 | int status; |
| 515 | DBT krec, drec; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 516 | char *data,buf[4096]; |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 517 | int size; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 518 | PyObject *result; |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 519 | recno_t recno; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 520 | |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 521 | if (dp->di_type == DB_RECNO) { |
| 522 | if (!PyArg_Parse(key, "i", &recno)) { |
| 523 | PyErr_SetString(PyExc_TypeError, |
| 524 | "key type must be integer"); |
| 525 | return NULL; |
| 526 | } |
| 527 | krec.data = &recno; |
| 528 | krec.size = sizeof(recno); |
| 529 | } |
| 530 | else { |
| 531 | if (!PyArg_Parse(key, "s#", &data, &size)) { |
| 532 | PyErr_SetString(PyExc_TypeError, |
| 533 | "key type must be string"); |
| 534 | return NULL; |
| 535 | } |
| 536 | krec.data = data; |
| 537 | krec.size = size; |
| 538 | } |
| 539 | check_bsddbobject_open(dp, NULL); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 540 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 541 | BSDDB_BGN_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 542 | status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_CURSOR); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 543 | if (status == 0) { |
| 544 | if (drec.size > sizeof(buf)) data = malloc(drec.size); |
| 545 | else data = buf; |
Andrew M. Kuchling | 2e09530 | 2000-12-15 00:59:32 +0000 | [diff] [blame] | 546 | if (data!=NULL) memcpy(data,drec.data,drec.size); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 547 | } |
| 548 | BSDDB_END_SAVE(dp) |
Andrew M. Kuchling | 2e09530 | 2000-12-15 00:59:32 +0000 | [diff] [blame] | 549 | if (data==NULL) return PyErr_NoMemory(); |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 550 | if (status != 0) { |
| 551 | if (status < 0) |
| 552 | PyErr_SetFromErrno(BsddbError); |
| 553 | else |
| 554 | PyErr_SetObject(PyExc_KeyError, key); |
| 555 | return NULL; |
| 556 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 557 | |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 558 | if (dp->di_type == DB_RECNO) |
| 559 | result = Py_BuildValue("is#", *((int*)krec.data), |
| 560 | data, drec.size); |
| 561 | else |
| 562 | result = Py_BuildValue("s#s#", krec.data, krec.size, |
| 563 | data, drec.size); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 564 | if (data != buf) free(data); |
| 565 | return result; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 568 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 569 | bsddb_seq(bsddbobject *dp, PyObject *args, int sequence_request) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 570 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 571 | int status; |
| 572 | DBT krec, drec; |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 573 | char *kdata=NULL,kbuf[4096]; |
| 574 | char *ddata=NULL,dbuf[4096]; |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 575 | PyObject *result; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 576 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 577 | if (!PyArg_NoArgs(args)) |
| 578 | return NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 579 | |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 580 | check_bsddbobject_open(dp, NULL); |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 581 | krec.data = 0; |
| 582 | krec.size = 0; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 583 | |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 584 | BSDDB_BGN_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 585 | status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, |
| 586 | &drec, sequence_request); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 587 | if (status == 0) { |
| 588 | if (krec.size > sizeof(kbuf)) kdata = malloc(krec.size); |
| 589 | else kdata = kbuf; |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 590 | if (kdata != NULL) memcpy(kdata,krec.data,krec.size); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 591 | if (drec.size > sizeof(dbuf)) ddata = malloc(drec.size); |
| 592 | else ddata = dbuf; |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 593 | if (ddata != NULL) memcpy(ddata,drec.data,drec.size); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 594 | } |
| 595 | BSDDB_END_SAVE(dp) |
Andrew M. Kuchling | 2e09530 | 2000-12-15 00:59:32 +0000 | [diff] [blame] | 596 | if (status == 0) { |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 597 | if ((kdata == NULL) || (ddata == NULL)) |
Andrew M. Kuchling | 2e09530 | 2000-12-15 00:59:32 +0000 | [diff] [blame] | 598 | return PyErr_NoMemory(); |
| 599 | } |
| 600 | else { |
| 601 | /* (status != 0) */ |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 602 | if (status < 0) |
| 603 | PyErr_SetFromErrno(BsddbError); |
| 604 | else |
| 605 | PyErr_SetObject(PyExc_KeyError, args); |
| 606 | return NULL; |
| 607 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 608 | |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 609 | |
| 610 | if (dp->di_type == DB_RECNO) |
| 611 | result = Py_BuildValue("is#", *((int*)kdata), |
| 612 | ddata, drec.size); |
| 613 | else |
| 614 | result = Py_BuildValue("s#s#", kdata, krec.size, |
| 615 | ddata, drec.size); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 616 | if (kdata != kbuf) free(kdata); |
| 617 | if (ddata != dbuf) free(ddata); |
| 618 | return result; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 621 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 622 | bsddb_next(bsddbobject *dp, PyObject *key) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 623 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 624 | return bsddb_seq(dp, key, R_NEXT); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 625 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 626 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 627 | bsddb_previous(bsddbobject *dp, PyObject *key) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 628 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 629 | return bsddb_seq(dp, key, R_PREV); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 630 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 631 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 632 | bsddb_first(bsddbobject *dp, PyObject *key) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 633 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 634 | return bsddb_seq(dp, key, R_FIRST); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 635 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 636 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 637 | bsddb_last(bsddbobject *dp, PyObject *key) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 638 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 639 | return bsddb_seq(dp, key, R_LAST); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 640 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 641 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 642 | bsddb_sync(bsddbobject *dp, PyObject *args) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 643 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 644 | int status; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 645 | |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 646 | if (!PyArg_NoArgs(args)) |
| 647 | return NULL; |
Fred Drake | ed5e823 | 2001-02-27 18:56:46 +0000 | [diff] [blame^] | 648 | check_bsddbobject_open(dp, NULL); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 649 | BSDDB_BGN_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 650 | status = (dp->di_bsddb->sync)(dp->di_bsddb, 0); |
Guido van Rossum | 4f199ea | 1998-04-09 20:56:35 +0000 | [diff] [blame] | 651 | BSDDB_END_SAVE(dp) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 652 | if (status != 0) { |
| 653 | PyErr_SetFromErrno(BsddbError); |
| 654 | return NULL; |
| 655 | } |
| 656 | return PyInt_FromLong(status = 0); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 657 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 658 | static PyMethodDef bsddb_methods[] = { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 659 | {"close", (PyCFunction)bsddb_close}, |
| 660 | {"keys", (PyCFunction)bsddb_keys}, |
| 661 | {"has_key", (PyCFunction)bsddb_has_key}, |
| 662 | {"set_location", (PyCFunction)bsddb_set_location}, |
| 663 | {"next", (PyCFunction)bsddb_next}, |
| 664 | {"previous", (PyCFunction)bsddb_previous}, |
| 665 | {"first", (PyCFunction)bsddb_first}, |
| 666 | {"last", (PyCFunction)bsddb_last}, |
| 667 | {"sync", (PyCFunction)bsddb_sync}, |
| 668 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 669 | }; |
| 670 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 671 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 672 | bsddb_getattr(PyObject *dp, char *name) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 673 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 674 | return Py_FindMethod(bsddb_methods, dp, name); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 677 | static PyTypeObject Bsddbtype = { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 678 | PyObject_HEAD_INIT(NULL) |
| 679 | 0, |
| 680 | "bsddb", |
| 681 | sizeof(bsddbobject), |
| 682 | 0, |
| 683 | (destructor)bsddb_dealloc, /*tp_dealloc*/ |
| 684 | 0, /*tp_print*/ |
| 685 | (getattrfunc)bsddb_getattr, /*tp_getattr*/ |
| 686 | 0, /*tp_setattr*/ |
| 687 | 0, /*tp_compare*/ |
| 688 | 0, /*tp_repr*/ |
| 689 | 0, /*tp_as_number*/ |
| 690 | 0, /*tp_as_sequence*/ |
| 691 | &bsddb_as_mapping, /*tp_as_mapping*/ |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 692 | }; |
| 693 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 694 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 695 | bsdhashopen(PyObject *self, PyObject *args) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 696 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 697 | char *file; |
| 698 | char *flag = NULL; |
| 699 | int flags = O_RDONLY; |
| 700 | int mode = 0666; |
| 701 | int bsize = 0; |
| 702 | int ffactor = 0; |
| 703 | int nelem = 0; |
| 704 | int cachesize = 0; |
| 705 | int hash = 0; /* XXX currently ignored */ |
| 706 | int lorder = 0; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 707 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 708 | if (!PyArg_ParseTuple(args, "s|siiiiiii:hashopen", |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 709 | &file, &flag, &mode, |
| 710 | &bsize, &ffactor, &nelem, &cachesize, |
| 711 | &hash, &lorder)) |
| 712 | return NULL; |
| 713 | if (flag != NULL) { |
| 714 | /* XXX need to pass O_EXCL, O_EXLOCK, O_NONBLOCK, O_SHLOCK */ |
| 715 | if (flag[0] == 'r') |
| 716 | flags = O_RDONLY; |
| 717 | else if (flag[0] == 'w') |
| 718 | flags = O_RDWR; |
| 719 | else if (flag[0] == 'c') |
| 720 | flags = O_RDWR|O_CREAT; |
| 721 | else if (flag[0] == 'n') |
| 722 | flags = O_RDWR|O_CREAT|O_TRUNC; |
| 723 | else { |
| 724 | PyErr_SetString(BsddbError, |
| 725 | "Flag should begin with 'r', 'w', 'c' or 'n'"); |
| 726 | return NULL; |
| 727 | } |
| 728 | if (flag[1] == 'l') { |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 729 | #if defined(O_EXLOCK) && defined(O_SHLOCK) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 730 | if (flag[0] == 'r') |
| 731 | flags |= O_SHLOCK; |
| 732 | else |
| 733 | flags |= O_EXLOCK; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 734 | #else |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 735 | PyErr_SetString(BsddbError, |
| 736 | "locking not supported on this platform"); |
| 737 | return NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 738 | #endif |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 739 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 740 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 741 | return newdbhashobject(file, flags, mode, |
| 742 | bsize, ffactor, nelem, cachesize, hash, lorder); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 745 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 746 | bsdbtopen(PyObject *self, PyObject *args) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 747 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 748 | char *file; |
| 749 | char *flag = NULL; |
| 750 | int flags = O_RDONLY; |
| 751 | int mode = 0666; |
| 752 | int cachesize = 0; |
| 753 | int maxkeypage = 0; |
| 754 | int minkeypage = 0; |
| 755 | int btflags = 0; |
| 756 | unsigned int psize = 0; |
| 757 | int lorder = 0; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 758 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 759 | if (!PyArg_ParseTuple(args, "s|siiiiiii:btopen", |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 760 | &file, &flag, &mode, |
| 761 | &btflags, &cachesize, &maxkeypage, &minkeypage, |
| 762 | &psize, &lorder)) |
| 763 | return NULL; |
| 764 | if (flag != NULL) { |
| 765 | /* XXX need to pass O_EXCL, O_EXLOCK, O_NONBLOCK, O_SHLOCK */ |
| 766 | if (flag[0] == 'r') |
| 767 | flags = O_RDONLY; |
| 768 | else if (flag[0] == 'w') |
| 769 | flags = O_RDWR; |
| 770 | else if (flag[0] == 'c') |
| 771 | flags = O_RDWR|O_CREAT; |
| 772 | else if (flag[0] == 'n') |
| 773 | flags = O_RDWR|O_CREAT|O_TRUNC; |
| 774 | else { |
| 775 | PyErr_SetString(BsddbError, |
| 776 | "Flag should begin with 'r', 'w', 'c' or 'n'"); |
| 777 | return NULL; |
| 778 | } |
| 779 | if (flag[1] == 'l') { |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 780 | #if defined(O_EXLOCK) && defined(O_SHLOCK) |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 781 | if (flag[0] == 'r') |
| 782 | flags |= O_SHLOCK; |
| 783 | else |
| 784 | flags |= O_EXLOCK; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 785 | #else |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 786 | PyErr_SetString(BsddbError, |
| 787 | "locking not supported on this platform"); |
| 788 | return NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 789 | #endif |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 790 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 791 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 792 | return newdbbtobject(file, flags, mode, |
| 793 | btflags, cachesize, maxkeypage, minkeypage, |
| 794 | psize, lorder); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 795 | } |
Guido van Rossum | dd96ca7 | 1996-05-23 22:57:54 +0000 | [diff] [blame] | 796 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 797 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 798 | bsdrnopen(PyObject *self, PyObject *args) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 799 | { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 800 | char *file; |
| 801 | char *flag = NULL; |
| 802 | int flags = O_RDONLY; |
| 803 | int mode = 0666; |
| 804 | int cachesize = 0; |
| 805 | int rnflags = 0; |
| 806 | unsigned int psize = 0; |
| 807 | int lorder = 0; |
| 808 | size_t reclen = 0; |
| 809 | char *bval = ""; |
| 810 | char *bfname = NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 811 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 812 | if (!PyArg_ParseTuple(args, "s|siiiiiiss:rnopen", |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 813 | &file, &flag, &mode, |
| 814 | &rnflags, &cachesize, &psize, &lorder, |
| 815 | &reclen, &bval, &bfname)) |
| 816 | return NULL; |
| 817 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 818 | if (flag != NULL) { |
| 819 | /* XXX need to pass O_EXCL, O_EXLOCK, O_NONBLOCK, O_SHLOCK */ |
| 820 | if (flag[0] == 'r') |
| 821 | flags = O_RDONLY; |
| 822 | else if (flag[0] == 'w') |
| 823 | flags = O_RDWR; |
| 824 | else if (flag[0] == 'c') |
| 825 | flags = O_RDWR|O_CREAT; |
| 826 | else if (flag[0] == 'n') |
| 827 | flags = O_RDWR|O_CREAT|O_TRUNC; |
| 828 | else { |
| 829 | PyErr_SetString(BsddbError, |
| 830 | "Flag should begin with 'r', 'w', 'c' or 'n'"); |
| 831 | return NULL; |
| 832 | } |
| 833 | if (flag[1] == 'l') { |
| 834 | #if defined(O_EXLOCK) && defined(O_SHLOCK) |
| 835 | if (flag[0] == 'r') |
| 836 | flags |= O_SHLOCK; |
| 837 | else |
| 838 | flags |= O_EXLOCK; |
| 839 | #else |
| 840 | PyErr_SetString(BsddbError, |
| 841 | "locking not supported on this platform"); |
| 842 | return NULL; |
| 843 | #endif |
| 844 | } |
| 845 | else if (flag[1] != '\0') { |
| 846 | PyErr_SetString(BsddbError, |
| 847 | "Flag char 2 should be 'l' or absent"); |
| 848 | return NULL; |
| 849 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 850 | } |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 851 | return newdbrnobject(file, flags, mode, rnflags, cachesize, |
| 852 | psize, lorder, reclen, bval[0], bfname); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 855 | static PyMethodDef bsddbmodule_methods[] = { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 856 | {"hashopen", (PyCFunction)bsdhashopen, 1}, |
| 857 | {"btopen", (PyCFunction)bsdbtopen, 1}, |
| 858 | {"rnopen", (PyCFunction)bsdrnopen, 1}, |
| 859 | {0, 0}, |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 860 | }; |
| 861 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 862 | DL_EXPORT(void) |
Thomas Wouters | 58d0510 | 2000-07-24 14:43:35 +0000 | [diff] [blame] | 863 | initbsddb(void) { |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 864 | PyObject *m, *d; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 865 | |
Roger E. Masse | d9240d1 | 1997-01-16 22:05:33 +0000 | [diff] [blame] | 866 | Bsddbtype.ob_type = &PyType_Type; |
| 867 | m = Py_InitModule("bsddb", bsddbmodule_methods); |
| 868 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 869 | BsddbError = PyErr_NewException("bsddb.error", NULL, NULL); |
| 870 | if (BsddbError != NULL) |
| 871 | PyDict_SetItemString(d, "error", BsddbError); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 872 | } |