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