Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1 | /*---------------------------------------------------------------------- |
| 2 | Copyright (c) 1999-2001, Digital Creations, Fredericksburg, VA, USA |
| 3 | and Andrew Kuchling. All rights reserved. |
| 4 | |
| 5 | Redistribution and use in source and binary forms, with or without |
| 6 | modification, are permitted provided that the following conditions are |
| 7 | met: |
| 8 | |
| 9 | o Redistributions of source code must retain the above copyright |
| 10 | notice, this list of conditions, and the disclaimer that follows. |
| 11 | |
| 12 | o Redistributions in binary form must reproduce the above copyright |
| 13 | notice, this list of conditions, and the following disclaimer in |
| 14 | the documentation and/or other materials provided with the |
| 15 | distribution. |
| 16 | |
| 17 | o Neither the name of Digital Creations nor the names of its |
| 18 | contributors may be used to endorse or promote products derived |
| 19 | from this software without specific prior written permission. |
| 20 | |
| 21 | THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS AND CONTRIBUTORS *AS |
| 22 | IS* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 23 | TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| 24 | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL |
| 25 | CREATIONS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 26 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 27 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 28 | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 29 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR |
| 30 | TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 31 | USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 32 | DAMAGE. |
| 33 | ------------------------------------------------------------------------*/ |
| 34 | |
| 35 | |
| 36 | /* |
| 37 | * Handwritten code to wrap version 3.x of the Berkeley DB library, |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 38 | * written to replace a SWIG-generated file. It has since been updated |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 39 | * to compile with Berkeley DB versions 3.2 through 4.2. |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 40 | * |
| 41 | * This module was started by Andrew Kuchling to remove the dependency |
Gregory P. Smith | f805785 | 2007-09-09 20:25:00 +0000 | [diff] [blame] | 42 | * on SWIG in a package by Gregory P. Smith who based his work on a |
| 43 | * similar package by Robin Dunn <robin@alldunn.com> which wrapped |
| 44 | * Berkeley DB 2.7.x. |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 45 | * |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 46 | * Development of this module then returned full circle back to Robin Dunn |
| 47 | * who worked on behalf of Digital Creations to complete the wrapping of |
| 48 | * the DB 3.x API and to build a solid unit test suite. Robin has |
| 49 | * since gone onto other projects (wxPython). |
| 50 | * |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 51 | * Gregory P. Smith <greg@krypto.org> was once again the maintainer. |
| 52 | * |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 53 | * Since January 2008, new maintainer is Jesus Cea <jcea@jcea.es>. |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 54 | * Jesus Cea licenses this code to PSF under a Contributor Agreement. |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 55 | * |
| 56 | * Use the pybsddb-users@lists.sf.net mailing list for all questions. |
Barry Warsaw | c74e4a5 | 2003-04-24 14:28:08 +0000 | [diff] [blame] | 57 | * Things can change faster than the header of this file is updated. This |
| 58 | * file is shared with the PyBSDDB project at SourceForge: |
| 59 | * |
| 60 | * http://pybsddb.sf.net |
| 61 | * |
| 62 | * This file should remain backward compatible with Python 2.1, but see PEP |
| 63 | * 291 for the most current backward compatibility requirements: |
| 64 | * |
| 65 | * http://www.python.org/peps/pep-0291.html |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 66 | * |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 67 | * This module contains 6 types: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 68 | * |
| 69 | * DB (Database) |
| 70 | * DBCursor (Database Cursor) |
| 71 | * DBEnv (database environment) |
| 72 | * DBTxn (An explicit database transaction) |
| 73 | * DBLock (A lock handle) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 74 | * DBSequence (Sequence) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 75 | * |
| 76 | */ |
| 77 | |
| 78 | /* --------------------------------------------------------------------- */ |
| 79 | |
| 80 | /* |
| 81 | * Portions of this module, associated unit tests and build scripts are the |
| 82 | * result of a contract with The Written Word (http://thewrittenword.com/) |
| 83 | * Many thanks go out to them for causing me to raise the bar on quality and |
| 84 | * functionality, resulting in a better bsddb3 package for all of us to use. |
| 85 | * |
| 86 | * --Robin |
| 87 | */ |
| 88 | |
| 89 | /* --------------------------------------------------------------------- */ |
| 90 | |
Gregory P. Smith | a703a21 | 2003-11-03 01:04:41 +0000 | [diff] [blame] | 91 | #include <stddef.h> /* for offsetof() */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 92 | #include <Python.h> |
Gregory P. Smith | 3925053 | 2007-10-09 06:02:21 +0000 | [diff] [blame] | 93 | |
| 94 | #define COMPILING_BSDDB_C |
| 95 | #include "bsddb.h" |
| 96 | #undef COMPILING_BSDDB_C |
| 97 | |
| 98 | static char *rcs_id = "$Id$"; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 99 | |
| 100 | /* --------------------------------------------------------------------- */ |
| 101 | /* Various macro definitions */ |
| 102 | |
Gregory P. Smith | 7f5b6f4 | 2006-04-08 07:10:51 +0000 | [diff] [blame] | 103 | #if (PY_VERSION_HEX < 0x02050000) |
Neal Norwitz | 09a29fa | 2006-06-12 02:05:55 +0000 | [diff] [blame] | 104 | typedef int Py_ssize_t; |
Gregory P. Smith | 7f5b6f4 | 2006-04-08 07:10:51 +0000 | [diff] [blame] | 105 | #endif |
| 106 | |
Gregory P. Smith | 572226c | 2008-05-26 19:03:35 +0000 | [diff] [blame] | 107 | #if (PY_VERSION_HEX < 0x02060000) /* really: before python trunk r63675 */ |
| 108 | /* This code now uses PyBytes* API function names instead of PyString*. |
| 109 | * These #defines map to their equivalent on earlier python versions. */ |
| 110 | #define PyBytes_FromStringAndSize PyString_FromStringAndSize |
| 111 | #define PyBytes_FromString PyString_FromString |
| 112 | #define PyBytes_AsStringAndSize PyString_AsStringAndSize |
| 113 | #define PyBytes_Check PyString_Check |
| 114 | #define PyBytes_GET_SIZE PyString_GET_SIZE |
| 115 | #define PyBytes_AS_STRING PyString_AS_STRING |
| 116 | #endif |
| 117 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 118 | #if (PY_VERSION_HEX >= 0x03000000) |
| 119 | #define NUMBER_Check PyLong_Check |
| 120 | #define NUMBER_AsLong PyLong_AsLong |
| 121 | #define NUMBER_FromLong PyLong_FromLong |
| 122 | #else |
| 123 | #define NUMBER_Check PyInt_Check |
| 124 | #define NUMBER_AsLong PyInt_AsLong |
| 125 | #define NUMBER_FromLong PyInt_FromLong |
| 126 | #endif |
| 127 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 128 | #ifdef WITH_THREAD |
| 129 | |
| 130 | /* These are for when calling Python --> C */ |
| 131 | #define MYDB_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS; |
| 132 | #define MYDB_END_ALLOW_THREADS Py_END_ALLOW_THREADS; |
| 133 | |
Mark Hammond | a69d409 | 2003-04-22 23:13:27 +0000 | [diff] [blame] | 134 | /* For 2.3, use the PyGILState_ calls */ |
| 135 | #if (PY_VERSION_HEX >= 0x02030000) |
| 136 | #define MYDB_USE_GILSTATE |
| 137 | #endif |
| 138 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 139 | /* and these are for calling C --> Python */ |
Mark Hammond | a69d409 | 2003-04-22 23:13:27 +0000 | [diff] [blame] | 140 | #if defined(MYDB_USE_GILSTATE) |
| 141 | #define MYDB_BEGIN_BLOCK_THREADS \ |
| 142 | PyGILState_STATE __savestate = PyGILState_Ensure(); |
| 143 | #define MYDB_END_BLOCK_THREADS \ |
| 144 | PyGILState_Release(__savestate); |
| 145 | #else /* MYDB_USE_GILSTATE */ |
| 146 | /* Pre GILState API - do it the long old way */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 147 | static PyInterpreterState* _db_interpreterState = NULL; |
| 148 | #define MYDB_BEGIN_BLOCK_THREADS { \ |
| 149 | PyThreadState* prevState; \ |
| 150 | PyThreadState* newState; \ |
| 151 | PyEval_AcquireLock(); \ |
| 152 | newState = PyThreadState_New(_db_interpreterState); \ |
| 153 | prevState = PyThreadState_Swap(newState); |
| 154 | |
| 155 | #define MYDB_END_BLOCK_THREADS \ |
| 156 | newState = PyThreadState_Swap(prevState); \ |
| 157 | PyThreadState_Clear(newState); \ |
| 158 | PyEval_ReleaseLock(); \ |
| 159 | PyThreadState_Delete(newState); \ |
| 160 | } |
Mark Hammond | a69d409 | 2003-04-22 23:13:27 +0000 | [diff] [blame] | 161 | #endif /* MYDB_USE_GILSTATE */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 162 | |
| 163 | #else |
Mark Hammond | a69d409 | 2003-04-22 23:13:27 +0000 | [diff] [blame] | 164 | /* Compiled without threads - avoid all this cruft */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 165 | #define MYDB_BEGIN_ALLOW_THREADS |
| 166 | #define MYDB_END_ALLOW_THREADS |
| 167 | #define MYDB_BEGIN_BLOCK_THREADS |
| 168 | #define MYDB_END_BLOCK_THREADS |
| 169 | |
| 170 | #endif |
| 171 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 172 | /* Should DB_INCOMPLETE be turned into a warning or an exception? */ |
| 173 | #define INCOMPLETE_IS_WARNING 1 |
| 174 | |
| 175 | /* --------------------------------------------------------------------- */ |
| 176 | /* Exceptions */ |
| 177 | |
| 178 | static PyObject* DBError; /* Base class, all others derive from this */ |
Gregory P. Smith | e276717 | 2003-11-02 08:06:29 +0000 | [diff] [blame] | 179 | static PyObject* DBCursorClosedError; /* raised when trying to use a closed cursor object */ |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 180 | static PyObject* DBKeyEmptyError; /* DB_KEYEMPTY: also derives from KeyError */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 181 | static PyObject* DBKeyExistError; /* DB_KEYEXIST */ |
| 182 | static PyObject* DBLockDeadlockError; /* DB_LOCK_DEADLOCK */ |
| 183 | static PyObject* DBLockNotGrantedError; /* DB_LOCK_NOTGRANTED */ |
| 184 | static PyObject* DBNotFoundError; /* DB_NOTFOUND: also derives from KeyError */ |
| 185 | static PyObject* DBOldVersionError; /* DB_OLD_VERSION */ |
| 186 | static PyObject* DBRunRecoveryError; /* DB_RUNRECOVERY */ |
| 187 | static PyObject* DBVerifyBadError; /* DB_VERIFY_BAD */ |
| 188 | static PyObject* DBNoServerError; /* DB_NOSERVER */ |
| 189 | static PyObject* DBNoServerHomeError; /* DB_NOSERVER_HOME */ |
| 190 | static PyObject* DBNoServerIDError; /* DB_NOSERVER_ID */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 191 | static PyObject* DBPageNotFoundError; /* DB_PAGE_NOTFOUND */ |
| 192 | static PyObject* DBSecondaryBadError; /* DB_SECONDARY_BAD */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 193 | |
| 194 | #if !INCOMPLETE_IS_WARNING |
| 195 | static PyObject* DBIncompleteError; /* DB_INCOMPLETE */ |
| 196 | #endif |
| 197 | |
| 198 | static PyObject* DBInvalidArgError; /* EINVAL */ |
| 199 | static PyObject* DBAccessError; /* EACCES */ |
| 200 | static PyObject* DBNoSpaceError; /* ENOSPC */ |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 201 | static PyObject* DBNoMemoryError; /* DB_BUFFER_SMALL (ENOMEM when < 4.3) */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 202 | static PyObject* DBAgainError; /* EAGAIN */ |
| 203 | static PyObject* DBBusyError; /* EBUSY */ |
| 204 | static PyObject* DBFileExistsError; /* EEXIST */ |
| 205 | static PyObject* DBNoSuchFileError; /* ENOENT */ |
| 206 | static PyObject* DBPermissionsError; /* EPERM */ |
| 207 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 208 | #if (DBVER >= 42) |
| 209 | static PyObject* DBRepHandleDeadError; /* DB_REP_HANDLE_DEAD */ |
| 210 | #endif |
| 211 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 212 | static PyObject* DBRepUnavailError; /* DB_REP_UNAVAIL */ |
| 213 | |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 214 | #if (DBVER < 43) |
| 215 | #define DB_BUFFER_SMALL ENOMEM |
| 216 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 217 | |
Matthias Klose | 54cc539 | 2010-03-15 12:46:18 +0000 | [diff] [blame] | 218 | #if (DBVER < 48) |
| 219 | #define DB_GID_SIZE DB_XIDDATASIZE |
| 220 | #endif |
| 221 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 222 | |
| 223 | /* --------------------------------------------------------------------- */ |
| 224 | /* Structure definitions */ |
| 225 | |
Gregory P. Smith | 3925053 | 2007-10-09 06:02:21 +0000 | [diff] [blame] | 226 | #if PYTHON_API_VERSION < 1010 |
| 227 | #error "Python 2.1 or later required" |
Gregory P. Smith | a703a21 | 2003-11-03 01:04:41 +0000 | [diff] [blame] | 228 | #endif |
| 229 | |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 230 | |
Gregory P. Smith | 3925053 | 2007-10-09 06:02:21 +0000 | [diff] [blame] | 231 | /* Defaults for moduleFlags in DBEnvObject and DBObject. */ |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 232 | #define DEFAULT_GET_RETURNS_NONE 1 |
Gregory P. Smith | a703a21 | 2003-11-03 01:04:41 +0000 | [diff] [blame] | 233 | #define DEFAULT_CURSOR_SET_RETURNS_NONE 1 /* 0 in pybsddb < 4.2, python < 2.4 */ |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 234 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 235 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 236 | /* See comment in Python 2.6 "object.h" */ |
| 237 | #ifndef staticforward |
| 238 | #define staticforward static |
| 239 | #endif |
| 240 | #ifndef statichere |
| 241 | #define statichere static |
| 242 | #endif |
| 243 | |
| 244 | staticforward PyTypeObject DB_Type, DBCursor_Type, DBEnv_Type, DBTxn_Type, |
| 245 | DBLock_Type; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 246 | #if (DBVER >= 43) |
| 247 | staticforward PyTypeObject DBSequence_Type; |
| 248 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 249 | |
Martin v. Löwis | 83c9201 | 2008-04-24 13:17:24 +0000 | [diff] [blame] | 250 | #ifndef Py_TYPE |
Gregory P. Smith | fc00669 | 2007-11-05 09:06:28 +0000 | [diff] [blame] | 251 | /* for compatibility with Python 2.5 and earlier */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 252 | #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type) |
Gregory P. Smith | fc00669 | 2007-11-05 09:06:28 +0000 | [diff] [blame] | 253 | #endif |
| 254 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 255 | #define DBObject_Check(v) (Py_TYPE(v) == &DB_Type) |
| 256 | #define DBCursorObject_Check(v) (Py_TYPE(v) == &DBCursor_Type) |
| 257 | #define DBEnvObject_Check(v) (Py_TYPE(v) == &DBEnv_Type) |
| 258 | #define DBTxnObject_Check(v) (Py_TYPE(v) == &DBTxn_Type) |
| 259 | #define DBLockObject_Check(v) (Py_TYPE(v) == &DBLock_Type) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 260 | #if (DBVER >= 43) |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 261 | #define DBSequenceObject_Check(v) (Py_TYPE(v) == &DBSequence_Type) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 262 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 263 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 264 | #if (DBVER < 46) |
| 265 | #define _DBC_close(dbc) dbc->c_close(dbc) |
| 266 | #define _DBC_count(dbc,a,b) dbc->c_count(dbc,a,b) |
| 267 | #define _DBC_del(dbc,a) dbc->c_del(dbc,a) |
| 268 | #define _DBC_dup(dbc,a,b) dbc->c_dup(dbc,a,b) |
| 269 | #define _DBC_get(dbc,a,b,c) dbc->c_get(dbc,a,b,c) |
| 270 | #define _DBC_pget(dbc,a,b,c,d) dbc->c_pget(dbc,a,b,c,d) |
| 271 | #define _DBC_put(dbc,a,b,c) dbc->c_put(dbc,a,b,c) |
| 272 | #else |
| 273 | #define _DBC_close(dbc) dbc->close(dbc) |
| 274 | #define _DBC_count(dbc,a,b) dbc->count(dbc,a,b) |
| 275 | #define _DBC_del(dbc,a) dbc->del(dbc,a) |
| 276 | #define _DBC_dup(dbc,a,b) dbc->dup(dbc,a,b) |
| 277 | #define _DBC_get(dbc,a,b,c) dbc->get(dbc,a,b,c) |
| 278 | #define _DBC_pget(dbc,a,b,c,d) dbc->pget(dbc,a,b,c,d) |
| 279 | #define _DBC_put(dbc,a,b,c) dbc->put(dbc,a,b,c) |
| 280 | #endif |
| 281 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 282 | |
| 283 | /* --------------------------------------------------------------------- */ |
| 284 | /* Utility macros and functions */ |
| 285 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 286 | #define INSERT_IN_DOUBLE_LINKED_LIST(backlink,object) \ |
| 287 | { \ |
| 288 | object->sibling_next=backlink; \ |
| 289 | object->sibling_prev_p=&(backlink); \ |
| 290 | backlink=object; \ |
| 291 | if (object->sibling_next) { \ |
| 292 | object->sibling_next->sibling_prev_p=&(object->sibling_next); \ |
| 293 | } \ |
| 294 | } |
| 295 | |
| 296 | #define EXTRACT_FROM_DOUBLE_LINKED_LIST(object) \ |
| 297 | { \ |
| 298 | if (object->sibling_next) { \ |
| 299 | object->sibling_next->sibling_prev_p=object->sibling_prev_p; \ |
| 300 | } \ |
| 301 | *(object->sibling_prev_p)=object->sibling_next; \ |
| 302 | } |
| 303 | |
| 304 | #define EXTRACT_FROM_DOUBLE_LINKED_LIST_MAYBE_NULL(object) \ |
| 305 | { \ |
| 306 | if (object->sibling_next) { \ |
| 307 | object->sibling_next->sibling_prev_p=object->sibling_prev_p; \ |
| 308 | } \ |
| 309 | if (object->sibling_prev_p) { \ |
| 310 | *(object->sibling_prev_p)=object->sibling_next; \ |
| 311 | } \ |
| 312 | } |
| 313 | |
| 314 | #define INSERT_IN_DOUBLE_LINKED_LIST_TXN(backlink,object) \ |
| 315 | { \ |
| 316 | object->sibling_next_txn=backlink; \ |
| 317 | object->sibling_prev_p_txn=&(backlink); \ |
| 318 | backlink=object; \ |
| 319 | if (object->sibling_next_txn) { \ |
| 320 | object->sibling_next_txn->sibling_prev_p_txn= \ |
| 321 | &(object->sibling_next_txn); \ |
| 322 | } \ |
| 323 | } |
| 324 | |
| 325 | #define EXTRACT_FROM_DOUBLE_LINKED_LIST_TXN(object) \ |
| 326 | { \ |
| 327 | if (object->sibling_next_txn) { \ |
| 328 | object->sibling_next_txn->sibling_prev_p_txn= \ |
| 329 | object->sibling_prev_p_txn; \ |
| 330 | } \ |
| 331 | *(object->sibling_prev_p_txn)=object->sibling_next_txn; \ |
| 332 | } |
| 333 | |
| 334 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 335 | #define RETURN_IF_ERR() \ |
| 336 | if (makeDBError(err)) { \ |
| 337 | return NULL; \ |
| 338 | } |
| 339 | |
| 340 | #define RETURN_NONE() Py_INCREF(Py_None); return Py_None; |
| 341 | |
Gregory P. Smith | e276717 | 2003-11-02 08:06:29 +0000 | [diff] [blame] | 342 | #define _CHECK_OBJECT_NOT_CLOSED(nonNull, pyErrObj, name) \ |
| 343 | if ((nonNull) == NULL) { \ |
| 344 | PyObject *errTuple = NULL; \ |
| 345 | errTuple = Py_BuildValue("(is)", 0, #name " object has been closed"); \ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 346 | if (errTuple) { \ |
| 347 | PyErr_SetObject((pyErrObj), errTuple); \ |
| 348 | Py_DECREF(errTuple); \ |
| 349 | } \ |
Gregory P. Smith | e276717 | 2003-11-02 08:06:29 +0000 | [diff] [blame] | 350 | return NULL; \ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Gregory P. Smith | e276717 | 2003-11-02 08:06:29 +0000 | [diff] [blame] | 353 | #define CHECK_DB_NOT_CLOSED(dbobj) \ |
| 354 | _CHECK_OBJECT_NOT_CLOSED(dbobj->db, DBError, DB) |
| 355 | |
| 356 | #define CHECK_ENV_NOT_CLOSED(env) \ |
| 357 | _CHECK_OBJECT_NOT_CLOSED(env->db_env, DBError, DBEnv) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 358 | |
| 359 | #define CHECK_CURSOR_NOT_CLOSED(curs) \ |
Gregory P. Smith | e276717 | 2003-11-02 08:06:29 +0000 | [diff] [blame] | 360 | _CHECK_OBJECT_NOT_CLOSED(curs->dbc, DBCursorClosedError, DBCursor) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 361 | |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 362 | #if (DBVER >= 43) |
| 363 | #define CHECK_SEQUENCE_NOT_CLOSED(curs) \ |
| 364 | _CHECK_OBJECT_NOT_CLOSED(curs->sequence, DBError, DBSequence) |
| 365 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 366 | |
| 367 | #define CHECK_DBFLAG(mydb, flag) (((mydb)->flags & (flag)) || \ |
| 368 | (((mydb)->myenvobj != NULL) && ((mydb)->myenvobj->flags & (flag)))) |
| 369 | |
| 370 | #define CLEAR_DBT(dbt) (memset(&(dbt), 0, sizeof(dbt))) |
| 371 | |
| 372 | #define FREE_DBT(dbt) if ((dbt.flags & (DB_DBT_MALLOC|DB_DBT_REALLOC)) && \ |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 373 | dbt.data != NULL) { free(dbt.data); dbt.data = NULL; } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 374 | |
| 375 | |
| 376 | static int makeDBError(int err); |
| 377 | |
| 378 | |
| 379 | /* Return the access method type of the DBObject */ |
| 380 | static int _DB_get_type(DBObject* self) |
| 381 | { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 382 | DBTYPE type; |
| 383 | int err; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 384 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 385 | err = self->db->get_type(self->db, &type); |
| 386 | if (makeDBError(err)) { |
| 387 | return -1; |
| 388 | } |
| 389 | return type; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | |
| 393 | /* Create a DBT structure (containing key and data values) from Python |
| 394 | strings. Returns 1 on success, 0 on an error. */ |
| 395 | static int make_dbt(PyObject* obj, DBT* dbt) |
| 396 | { |
| 397 | CLEAR_DBT(*dbt); |
| 398 | if (obj == Py_None) { |
| 399 | /* no need to do anything, the structure has already been zeroed */ |
| 400 | } |
| 401 | else if (!PyArg_Parse(obj, "s#", &dbt->data, &dbt->size)) { |
| 402 | PyErr_SetString(PyExc_TypeError, |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 403 | #if (PY_VERSION_HEX < 0x03000000) |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 404 | "Data values must be of type string or None."); |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 405 | #else |
| 406 | "Data values must be of type bytes or None."); |
| 407 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 408 | return 0; |
| 409 | } |
| 410 | return 1; |
| 411 | } |
| 412 | |
| 413 | |
| 414 | /* Recno and Queue DBs can have integer keys. This function figures out |
| 415 | what's been given, verifies that it's allowed, and then makes the DBT. |
| 416 | |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 417 | Caller MUST call FREE_DBT(key) when done. */ |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 418 | static int |
| 419 | make_key_dbt(DBObject* self, PyObject* keyobj, DBT* key, int* pflags) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 420 | { |
| 421 | db_recno_t recno; |
| 422 | int type; |
| 423 | |
| 424 | CLEAR_DBT(*key); |
Gustavo Niemeyer | f073b75 | 2004-01-20 15:24:29 +0000 | [diff] [blame] | 425 | if (keyobj == Py_None) { |
Gustavo Niemeyer | 024f2de | 2004-01-20 15:14:55 +0000 | [diff] [blame] | 426 | type = _DB_get_type(self); |
Gustavo Niemeyer | 8974f72 | 2004-01-20 15:20:03 +0000 | [diff] [blame] | 427 | if (type == -1) |
| 428 | return 0; |
Gustavo Niemeyer | 024f2de | 2004-01-20 15:14:55 +0000 | [diff] [blame] | 429 | if (type == DB_RECNO || type == DB_QUEUE) { |
| 430 | PyErr_SetString( |
| 431 | PyExc_TypeError, |
| 432 | "None keys not allowed for Recno and Queue DB's"); |
| 433 | return 0; |
| 434 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 435 | /* no need to do anything, the structure has already been zeroed */ |
| 436 | } |
| 437 | |
Christian Heimes | 593daf5 | 2008-05-26 12:51:38 +0000 | [diff] [blame] | 438 | else if (PyBytes_Check(keyobj)) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 439 | /* verify access method type */ |
| 440 | type = _DB_get_type(self); |
| 441 | if (type == -1) |
| 442 | return 0; |
| 443 | if (type == DB_RECNO || type == DB_QUEUE) { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 444 | PyErr_SetString( |
| 445 | PyExc_TypeError, |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 446 | #if (PY_VERSION_HEX < 0x03000000) |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 447 | "String keys not allowed for Recno and Queue DB's"); |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 448 | #else |
| 449 | "Bytes keys not allowed for Recno and Queue DB's"); |
| 450 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 451 | return 0; |
| 452 | } |
| 453 | |
Gregory P. Smith | 10bed54 | 2007-10-09 06:50:43 +0000 | [diff] [blame] | 454 | /* |
| 455 | * NOTE(gps): I don't like doing a data copy here, it seems |
| 456 | * wasteful. But without a clean way to tell FREE_DBT if it |
| 457 | * should free key->data or not we have to. Other places in |
| 458 | * the code check for DB_THREAD and forceably set DBT_MALLOC |
| 459 | * when we otherwise would leave flags 0 to indicate that. |
| 460 | */ |
Christian Heimes | 593daf5 | 2008-05-26 12:51:38 +0000 | [diff] [blame] | 461 | key->data = malloc(PyBytes_GET_SIZE(keyobj)); |
Gregory P. Smith | 10bed54 | 2007-10-09 06:50:43 +0000 | [diff] [blame] | 462 | if (key->data == NULL) { |
| 463 | PyErr_SetString(PyExc_MemoryError, "Key memory allocation failed"); |
| 464 | return 0; |
| 465 | } |
Christian Heimes | 593daf5 | 2008-05-26 12:51:38 +0000 | [diff] [blame] | 466 | memcpy(key->data, PyBytes_AS_STRING(keyobj), |
| 467 | PyBytes_GET_SIZE(keyobj)); |
Gregory P. Smith | 10bed54 | 2007-10-09 06:50:43 +0000 | [diff] [blame] | 468 | key->flags = DB_DBT_REALLOC; |
Christian Heimes | 593daf5 | 2008-05-26 12:51:38 +0000 | [diff] [blame] | 469 | key->size = PyBytes_GET_SIZE(keyobj); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 472 | else if (NUMBER_Check(keyobj)) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 473 | /* verify access method type */ |
| 474 | type = _DB_get_type(self); |
| 475 | if (type == -1) |
| 476 | return 0; |
| 477 | if (type == DB_BTREE && pflags != NULL) { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 478 | /* if BTREE then an Integer key is allowed with the |
| 479 | * DB_SET_RECNO flag */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 480 | *pflags |= DB_SET_RECNO; |
| 481 | } |
| 482 | else if (type != DB_RECNO && type != DB_QUEUE) { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 483 | PyErr_SetString( |
| 484 | PyExc_TypeError, |
| 485 | "Integer keys only allowed for Recno and Queue DB's"); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 486 | return 0; |
| 487 | } |
| 488 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 489 | /* Make a key out of the requested recno, use allocated space so DB |
| 490 | * will be able to realloc room for the real key if needed. */ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 491 | recno = NUMBER_AsLong(keyobj); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 492 | key->data = malloc(sizeof(db_recno_t)); |
| 493 | if (key->data == NULL) { |
| 494 | PyErr_SetString(PyExc_MemoryError, "Key memory allocation failed"); |
| 495 | return 0; |
| 496 | } |
| 497 | key->ulen = key->size = sizeof(db_recno_t); |
| 498 | memcpy(key->data, &recno, sizeof(db_recno_t)); |
| 499 | key->flags = DB_DBT_REALLOC; |
| 500 | } |
| 501 | else { |
| 502 | PyErr_Format(PyExc_TypeError, |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 503 | #if (PY_VERSION_HEX < 0x03000000) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 504 | "String or Integer object expected for key, %s found", |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 505 | #else |
| 506 | "Bytes or Integer object expected for key, %s found", |
| 507 | #endif |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 508 | Py_TYPE(keyobj)->tp_name); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | return 1; |
| 513 | } |
| 514 | |
| 515 | |
| 516 | /* Add partial record access to an existing DBT data struct. |
| 517 | If dlen and doff are set, then the DB_DBT_PARTIAL flag will be set |
| 518 | and the data storage/retrieval will be done using dlen and doff. */ |
| 519 | static int add_partial_dbt(DBT* d, int dlen, int doff) { |
| 520 | /* if neither were set we do nothing (-1 is the default value) */ |
| 521 | if ((dlen == -1) && (doff == -1)) { |
| 522 | return 1; |
| 523 | } |
| 524 | |
| 525 | if ((dlen < 0) || (doff < 0)) { |
| 526 | PyErr_SetString(PyExc_TypeError, "dlen and doff must both be >= 0"); |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | d->flags = d->flags | DB_DBT_PARTIAL; |
| 531 | d->dlen = (unsigned int) dlen; |
| 532 | d->doff = (unsigned int) doff; |
| 533 | return 1; |
| 534 | } |
| 535 | |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 536 | /* a safe strcpy() without the zeroing behaviour and semantics of strncpy. */ |
| 537 | /* TODO: make this use the native libc strlcpy() when available (BSD) */ |
| 538 | unsigned int our_strlcpy(char* dest, const char* src, unsigned int n) |
| 539 | { |
| 540 | unsigned int srclen, copylen; |
| 541 | |
| 542 | srclen = strlen(src); |
| 543 | if (n <= 0) |
| 544 | return srclen; |
| 545 | copylen = (srclen > n-1) ? n-1 : srclen; |
| 546 | /* populate dest[0] thru dest[copylen-1] */ |
| 547 | memcpy(dest, src, copylen); |
| 548 | /* guarantee null termination */ |
| 549 | dest[copylen] = 0; |
| 550 | |
| 551 | return srclen; |
| 552 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 553 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 554 | /* Callback used to save away more information about errors from the DB |
| 555 | * library. */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 556 | static char _db_errmsg[1024]; |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 557 | #if (DBVER <= 42) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 558 | static void _db_errorCallback(const char* prefix, char* msg) |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 559 | #else |
| 560 | static void _db_errorCallback(const DB_ENV *db_env, |
| 561 | const char* prefix, const char* msg) |
| 562 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 563 | { |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 564 | our_strlcpy(_db_errmsg, msg, sizeof(_db_errmsg)); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 568 | /* |
| 569 | ** We need these functions because some results |
| 570 | ** are undefined if pointer is NULL. Some other |
| 571 | ** give None instead of "". |
| 572 | ** |
| 573 | ** This functions are static and will be |
| 574 | ** -I hope- inlined. |
| 575 | */ |
| 576 | static const char *DummyString = "This string is a simple placeholder"; |
| 577 | static PyObject *Build_PyString(const char *p,int s) |
| 578 | { |
| 579 | if (!p) { |
| 580 | p=DummyString; |
| 581 | assert(s==0); |
| 582 | } |
Christian Heimes | 593daf5 | 2008-05-26 12:51:38 +0000 | [diff] [blame] | 583 | return PyBytes_FromStringAndSize(p,s); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | static PyObject *BuildValue_S(const void *p,int s) |
| 587 | { |
| 588 | if (!p) { |
| 589 | p=DummyString; |
| 590 | assert(s==0); |
| 591 | } |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 592 | return PyBytes_FromStringAndSize(p, s); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | static PyObject *BuildValue_SS(const void *p1,int s1,const void *p2,int s2) |
| 596 | { |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 597 | PyObject *a, *b, *r; |
| 598 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 599 | if (!p1) { |
| 600 | p1=DummyString; |
| 601 | assert(s1==0); |
| 602 | } |
| 603 | if (!p2) { |
| 604 | p2=DummyString; |
| 605 | assert(s2==0); |
| 606 | } |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 607 | |
| 608 | if (!(a = PyBytes_FromStringAndSize(p1, s1))) { |
| 609 | return NULL; |
| 610 | } |
| 611 | if (!(b = PyBytes_FromStringAndSize(p2, s2))) { |
| 612 | Py_DECREF(a); |
| 613 | return NULL; |
| 614 | } |
| 615 | |
| 616 | #if (PY_VERSION_HEX >= 0x02040000) |
| 617 | r = PyTuple_Pack(2, a, b) ; |
| 618 | #else |
| 619 | r = Py_BuildValue("OO", a, b); |
| 620 | #endif |
| 621 | Py_DECREF(a); |
| 622 | Py_DECREF(b); |
| 623 | return r; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | static PyObject *BuildValue_IS(int i,const void *p,int s) |
| 627 | { |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 628 | PyObject *a, *r; |
| 629 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 630 | if (!p) { |
| 631 | p=DummyString; |
| 632 | assert(s==0); |
| 633 | } |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 634 | |
| 635 | if (!(a = PyBytes_FromStringAndSize(p, s))) { |
| 636 | return NULL; |
| 637 | } |
| 638 | |
| 639 | r = Py_BuildValue("iO", i, a); |
| 640 | Py_DECREF(a); |
| 641 | return r; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 644 | static PyObject *BuildValue_LS(long l,const void *p,int s) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 645 | { |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 646 | PyObject *a, *r; |
| 647 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 648 | if (!p) { |
| 649 | p=DummyString; |
| 650 | assert(s==0); |
| 651 | } |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 652 | |
| 653 | if (!(a = PyBytes_FromStringAndSize(p, s))) { |
| 654 | return NULL; |
| 655 | } |
| 656 | |
| 657 | r = Py_BuildValue("lO", l, a); |
| 658 | Py_DECREF(a); |
| 659 | return r; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | |
| 663 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 664 | /* make a nice exception object to raise for errors. */ |
| 665 | static int makeDBError(int err) |
| 666 | { |
| 667 | char errTxt[2048]; /* really big, just in case... */ |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 668 | PyObject *errObj = NULL; |
| 669 | PyObject *errTuple = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 670 | int exceptionRaised = 0; |
Neal Norwitz | dce937f | 2006-07-23 08:01:43 +0000 | [diff] [blame] | 671 | unsigned int bytes_left; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 672 | |
| 673 | switch (err) { |
| 674 | case 0: /* successful, no error */ break; |
| 675 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 676 | #if (DBVER < 41) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 677 | case DB_INCOMPLETE: |
| 678 | #if INCOMPLETE_IS_WARNING |
Neal Norwitz | dce937f | 2006-07-23 08:01:43 +0000 | [diff] [blame] | 679 | bytes_left = our_strlcpy(errTxt, db_strerror(err), sizeof(errTxt)); |
| 680 | /* Ensure that bytes_left never goes negative */ |
| 681 | if (_db_errmsg[0] && bytes_left < (sizeof(errTxt) - 4)) { |
| 682 | bytes_left = sizeof(errTxt) - bytes_left - 4 - 1; |
| 683 | assert(bytes_left >= 0); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 684 | strcat(errTxt, " -- "); |
Neal Norwitz | dce937f | 2006-07-23 08:01:43 +0000 | [diff] [blame] | 685 | strncat(errTxt, _db_errmsg, bytes_left); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 686 | } |
Neal Norwitz | dce937f | 2006-07-23 08:01:43 +0000 | [diff] [blame] | 687 | _db_errmsg[0] = 0; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 688 | exceptionRaised = PyErr_Warn(PyExc_RuntimeWarning, errTxt); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 689 | |
| 690 | #else /* do an exception instead */ |
| 691 | errObj = DBIncompleteError; |
| 692 | #endif |
| 693 | break; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 694 | #endif /* DBVER < 41 */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 695 | |
| 696 | case DB_KEYEMPTY: errObj = DBKeyEmptyError; break; |
| 697 | case DB_KEYEXIST: errObj = DBKeyExistError; break; |
| 698 | case DB_LOCK_DEADLOCK: errObj = DBLockDeadlockError; break; |
| 699 | case DB_LOCK_NOTGRANTED: errObj = DBLockNotGrantedError; break; |
| 700 | case DB_NOTFOUND: errObj = DBNotFoundError; break; |
| 701 | case DB_OLD_VERSION: errObj = DBOldVersionError; break; |
| 702 | case DB_RUNRECOVERY: errObj = DBRunRecoveryError; break; |
| 703 | case DB_VERIFY_BAD: errObj = DBVerifyBadError; break; |
| 704 | case DB_NOSERVER: errObj = DBNoServerError; break; |
| 705 | case DB_NOSERVER_HOME: errObj = DBNoServerHomeError; break; |
| 706 | case DB_NOSERVER_ID: errObj = DBNoServerIDError; break; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 707 | case DB_PAGE_NOTFOUND: errObj = DBPageNotFoundError; break; |
| 708 | case DB_SECONDARY_BAD: errObj = DBSecondaryBadError; break; |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 709 | case DB_BUFFER_SMALL: errObj = DBNoMemoryError; break; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 710 | |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 711 | #if (DBVER >= 43) |
| 712 | /* ENOMEM and DB_BUFFER_SMALL were one and the same until 4.3 */ |
| 713 | case ENOMEM: errObj = PyExc_MemoryError; break; |
| 714 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 715 | case EINVAL: errObj = DBInvalidArgError; break; |
| 716 | case EACCES: errObj = DBAccessError; break; |
| 717 | case ENOSPC: errObj = DBNoSpaceError; break; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 718 | case EAGAIN: errObj = DBAgainError; break; |
| 719 | case EBUSY : errObj = DBBusyError; break; |
| 720 | case EEXIST: errObj = DBFileExistsError; break; |
| 721 | case ENOENT: errObj = DBNoSuchFileError; break; |
| 722 | case EPERM : errObj = DBPermissionsError; break; |
| 723 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 724 | #if (DBVER >= 42) |
| 725 | case DB_REP_HANDLE_DEAD : errObj = DBRepHandleDeadError; break; |
| 726 | #endif |
| 727 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 728 | case DB_REP_UNAVAIL : errObj = DBRepUnavailError; break; |
| 729 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 730 | default: errObj = DBError; break; |
| 731 | } |
| 732 | |
| 733 | if (errObj != NULL) { |
Neal Norwitz | dce937f | 2006-07-23 08:01:43 +0000 | [diff] [blame] | 734 | bytes_left = our_strlcpy(errTxt, db_strerror(err), sizeof(errTxt)); |
| 735 | /* Ensure that bytes_left never goes negative */ |
| 736 | if (_db_errmsg[0] && bytes_left < (sizeof(errTxt) - 4)) { |
| 737 | bytes_left = sizeof(errTxt) - bytes_left - 4 - 1; |
| 738 | assert(bytes_left >= 0); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 739 | strcat(errTxt, " -- "); |
Neal Norwitz | dce937f | 2006-07-23 08:01:43 +0000 | [diff] [blame] | 740 | strncat(errTxt, _db_errmsg, bytes_left); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 741 | } |
Neal Norwitz | dce937f | 2006-07-23 08:01:43 +0000 | [diff] [blame] | 742 | _db_errmsg[0] = 0; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 743 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 744 | errTuple = Py_BuildValue("(is)", err, errTxt); |
| 745 | if (errTuple == NULL) { |
| 746 | Py_DECREF(errObj); |
| 747 | return !0; |
| 748 | } |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 749 | PyErr_SetObject(errObj, errTuple); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 750 | Py_DECREF(errTuple); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | return ((errObj != NULL) || exceptionRaised); |
| 754 | } |
| 755 | |
| 756 | |
| 757 | |
| 758 | /* set a type exception */ |
| 759 | static void makeTypeError(char* expected, PyObject* found) |
| 760 | { |
| 761 | PyErr_Format(PyExc_TypeError, "Expected %s argument, %s found.", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 762 | expected, Py_TYPE(found)->tp_name); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | |
| 766 | /* verify that an obj is either None or a DBTxn, and set the txn pointer */ |
| 767 | static int checkTxnObj(PyObject* txnobj, DB_TXN** txn) |
| 768 | { |
| 769 | if (txnobj == Py_None || txnobj == NULL) { |
| 770 | *txn = NULL; |
| 771 | return 1; |
| 772 | } |
| 773 | if (DBTxnObject_Check(txnobj)) { |
| 774 | *txn = ((DBTxnObject*)txnobj)->txn; |
| 775 | return 1; |
| 776 | } |
| 777 | else |
| 778 | makeTypeError("DBTxn", txnobj); |
| 779 | return 0; |
| 780 | } |
| 781 | |
| 782 | |
| 783 | /* Delete a key from a database |
| 784 | Returns 0 on success, -1 on an error. */ |
| 785 | static int _DB_delete(DBObject* self, DB_TXN *txn, DBT *key, int flags) |
| 786 | { |
| 787 | int err; |
| 788 | |
| 789 | MYDB_BEGIN_ALLOW_THREADS; |
| 790 | err = self->db->del(self->db, txn, key, 0); |
| 791 | MYDB_END_ALLOW_THREADS; |
| 792 | if (makeDBError(err)) { |
| 793 | return -1; |
| 794 | } |
| 795 | self->haveStat = 0; |
| 796 | return 0; |
| 797 | } |
| 798 | |
| 799 | |
| 800 | /* Store a key into a database |
| 801 | Returns 0 on success, -1 on an error. */ |
| 802 | static int _DB_put(DBObject* self, DB_TXN *txn, DBT *key, DBT *data, int flags) |
| 803 | { |
| 804 | int err; |
| 805 | |
| 806 | MYDB_BEGIN_ALLOW_THREADS; |
| 807 | err = self->db->put(self->db, txn, key, data, flags); |
| 808 | MYDB_END_ALLOW_THREADS; |
| 809 | if (makeDBError(err)) { |
| 810 | return -1; |
| 811 | } |
| 812 | self->haveStat = 0; |
| 813 | return 0; |
| 814 | } |
| 815 | |
| 816 | /* Get a key/data pair from a cursor */ |
| 817 | static PyObject* _DBCursor_get(DBCursorObject* self, int extra_flags, |
| 818 | PyObject *args, PyObject *kwargs, char *format) |
| 819 | { |
| 820 | int err; |
| 821 | PyObject* retval = NULL; |
| 822 | DBT key, data; |
| 823 | int dlen = -1; |
| 824 | int doff = -1; |
| 825 | int flags = 0; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 826 | static char* kwnames[] = { "flags", "dlen", "doff", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 827 | |
| 828 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, format, kwnames, |
| 829 | &flags, &dlen, &doff)) |
| 830 | return NULL; |
| 831 | |
| 832 | CHECK_CURSOR_NOT_CLOSED(self); |
| 833 | |
| 834 | flags |= extra_flags; |
| 835 | CLEAR_DBT(key); |
| 836 | CLEAR_DBT(data); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 837 | if (!add_partial_dbt(&data, dlen, doff)) |
| 838 | return NULL; |
| 839 | |
| 840 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 841 | err = _DBC_get(self->dbc, &key, &data, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 842 | MYDB_END_ALLOW_THREADS; |
| 843 | |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 844 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 845 | && self->mydb->moduleFlags.getReturnsNone) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 846 | Py_INCREF(Py_None); |
| 847 | retval = Py_None; |
| 848 | } |
| 849 | else if (makeDBError(err)) { |
| 850 | retval = NULL; |
| 851 | } |
| 852 | else { /* otherwise, success! */ |
| 853 | |
| 854 | /* if Recno or Queue, return the key as an Int */ |
| 855 | switch (_DB_get_type(self->mydb)) { |
| 856 | case -1: |
| 857 | retval = NULL; |
| 858 | break; |
| 859 | |
| 860 | case DB_RECNO: |
| 861 | case DB_QUEUE: |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 862 | retval = BuildValue_IS(*((db_recno_t*)key.data), data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 863 | break; |
| 864 | case DB_HASH: |
| 865 | case DB_BTREE: |
| 866 | default: |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 867 | retval = BuildValue_SS(key.data, key.size, data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 868 | break; |
| 869 | } |
| 870 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 871 | return retval; |
| 872 | } |
| 873 | |
| 874 | |
| 875 | /* add an integer to a dictionary using the given name as a key */ |
| 876 | static void _addIntToDict(PyObject* dict, char *name, int value) |
| 877 | { |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 878 | PyObject* v = NUMBER_FromLong((long) value); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 879 | if (!v || PyDict_SetItemString(dict, name, v)) |
| 880 | PyErr_Clear(); |
| 881 | |
| 882 | Py_XDECREF(v); |
| 883 | } |
Kristján Valur Jónsson | bd77c03 | 2007-04-26 15:24:54 +0000 | [diff] [blame] | 884 | |
| 885 | /* The same, when the value is a time_t */ |
| 886 | static void _addTimeTToDict(PyObject* dict, char *name, time_t value) |
| 887 | { |
| 888 | PyObject* v; |
| 889 | /* if the value fits in regular int, use that. */ |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 890 | #ifdef PY_LONG_LONG |
Kristján Valur Jónsson | bd77c03 | 2007-04-26 15:24:54 +0000 | [diff] [blame] | 891 | if (sizeof(time_t) > sizeof(long)) |
| 892 | v = PyLong_FromLongLong((PY_LONG_LONG) value); |
| 893 | else |
| 894 | #endif |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 895 | v = NUMBER_FromLong((long) value); |
Kristján Valur Jónsson | bd77c03 | 2007-04-26 15:24:54 +0000 | [diff] [blame] | 896 | if (!v || PyDict_SetItemString(dict, name, v)) |
| 897 | PyErr_Clear(); |
| 898 | |
| 899 | Py_XDECREF(v); |
| 900 | } |
| 901 | |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 902 | #if (DBVER >= 43) |
| 903 | /* add an db_seq_t to a dictionary using the given name as a key */ |
| 904 | static void _addDb_seq_tToDict(PyObject* dict, char *name, db_seq_t value) |
| 905 | { |
| 906 | PyObject* v = PyLong_FromLongLong(value); |
| 907 | if (!v || PyDict_SetItemString(dict, name, v)) |
| 908 | PyErr_Clear(); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 909 | |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 910 | Py_XDECREF(v); |
| 911 | } |
| 912 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 913 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 914 | static void _addDB_lsnToDict(PyObject* dict, char *name, DB_LSN value) |
| 915 | { |
| 916 | PyObject *v = Py_BuildValue("(ll)",value.file,value.offset); |
| 917 | if (!v || PyDict_SetItemString(dict, name, v)) |
| 918 | PyErr_Clear(); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 919 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 920 | Py_XDECREF(v); |
| 921 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 922 | |
| 923 | /* --------------------------------------------------------------------- */ |
| 924 | /* Allocators and deallocators */ |
| 925 | |
| 926 | static DBObject* |
| 927 | newDBObject(DBEnvObject* arg, int flags) |
| 928 | { |
| 929 | DBObject* self; |
| 930 | DB_ENV* db_env = NULL; |
| 931 | int err; |
| 932 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 933 | self = PyObject_New(DBObject, &DB_Type); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 934 | if (self == NULL) |
| 935 | return NULL; |
| 936 | |
| 937 | self->haveStat = 0; |
| 938 | self->flags = 0; |
| 939 | self->setflags = 0; |
| 940 | self->myenvobj = NULL; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 941 | self->db = NULL; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 942 | self->children_cursors = NULL; |
| 943 | #if (DBVER >=43) |
| 944 | self->children_sequences = NULL; |
| 945 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 946 | self->associateCallback = NULL; |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 947 | self->btCompareCallback = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 948 | self->primaryDBType = 0; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 949 | Py_INCREF(Py_None); |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 950 | self->private_obj = Py_None; |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 951 | self->in_weakreflist = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 952 | |
| 953 | /* keep a reference to our python DBEnv object */ |
| 954 | if (arg) { |
| 955 | Py_INCREF(arg); |
| 956 | self->myenvobj = arg; |
| 957 | db_env = arg->db_env; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 958 | INSERT_IN_DOUBLE_LINKED_LIST(self->myenvobj->children_dbs,self); |
| 959 | } else { |
| 960 | self->sibling_prev_p=NULL; |
| 961 | self->sibling_next=NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 962 | } |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 963 | self->txn=NULL; |
| 964 | self->sibling_prev_p_txn=NULL; |
| 965 | self->sibling_next_txn=NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 966 | |
| 967 | if (self->myenvobj) |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 968 | self->moduleFlags = self->myenvobj->moduleFlags; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 969 | else |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 970 | self->moduleFlags.getReturnsNone = DEFAULT_GET_RETURNS_NONE; |
| 971 | self->moduleFlags.cursorSetReturnsNone = DEFAULT_CURSOR_SET_RETURNS_NONE; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 972 | |
| 973 | MYDB_BEGIN_ALLOW_THREADS; |
| 974 | err = db_create(&self->db, db_env, flags); |
Neal Norwitz | dce937f | 2006-07-23 08:01:43 +0000 | [diff] [blame] | 975 | if (self->db != NULL) { |
| 976 | self->db->set_errcall(self->db, _db_errorCallback); |
Neal Norwitz | dce937f | 2006-07-23 08:01:43 +0000 | [diff] [blame] | 977 | self->db->app_private = (void*)self; |
Neal Norwitz | dce937f | 2006-07-23 08:01:43 +0000 | [diff] [blame] | 978 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 979 | MYDB_END_ALLOW_THREADS; |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 980 | /* TODO add a weakref(self) to the self->myenvobj->open_child_weakrefs |
| 981 | * list so that a DBEnv can refuse to close without aborting any open |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 982 | * DBTxns and closing any open DBs first. */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 983 | if (makeDBError(err)) { |
| 984 | if (self->myenvobj) { |
| 985 | Py_DECREF(self->myenvobj); |
| 986 | self->myenvobj = NULL; |
| 987 | } |
Gregory P. Smith | 664782e | 2008-05-17 06:12:02 +0000 | [diff] [blame] | 988 | Py_DECREF(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 989 | self = NULL; |
| 990 | } |
| 991 | return self; |
| 992 | } |
| 993 | |
| 994 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 995 | /* Forward declaration */ |
Jesus Cea | 5cd5f12 | 2008-09-23 18:54:08 +0000 | [diff] [blame] | 996 | static PyObject *DB_close_internal(DBObject* self, int flags, int do_not_close); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 997 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 998 | static void |
| 999 | DB_dealloc(DBObject* self) |
| 1000 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1001 | PyObject *dummy; |
| 1002 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1003 | if (self->db != NULL) { |
Jesus Cea | 5cd5f12 | 2008-09-23 18:54:08 +0000 | [diff] [blame] | 1004 | dummy=DB_close_internal(self, 0, 0); |
| 1005 | /* |
| 1006 | ** Raising exceptions while doing |
| 1007 | ** garbage collection is a fatal error. |
| 1008 | */ |
| 1009 | if (dummy) |
| 1010 | Py_DECREF(dummy); |
| 1011 | else |
| 1012 | PyErr_Clear(); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1013 | } |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 1014 | if (self->in_weakreflist != NULL) { |
| 1015 | PyObject_ClearWeakRefs((PyObject *) self); |
| 1016 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1017 | if (self->myenvobj) { |
| 1018 | Py_DECREF(self->myenvobj); |
| 1019 | self->myenvobj = NULL; |
| 1020 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1021 | if (self->associateCallback != NULL) { |
| 1022 | Py_DECREF(self->associateCallback); |
| 1023 | self->associateCallback = NULL; |
| 1024 | } |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 1025 | if (self->btCompareCallback != NULL) { |
| 1026 | Py_DECREF(self->btCompareCallback); |
| 1027 | self->btCompareCallback = NULL; |
| 1028 | } |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 1029 | Py_DECREF(self->private_obj); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1030 | PyObject_Del(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1033 | static DBCursorObject* |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1034 | newDBCursorObject(DBC* dbc, DBTxnObject *txn, DBObject* db) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1035 | { |
Neal Norwitz | b4a5581 | 2004-07-09 23:30:57 +0000 | [diff] [blame] | 1036 | DBCursorObject* self = PyObject_New(DBCursorObject, &DBCursor_Type); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1037 | if (self == NULL) |
| 1038 | return NULL; |
| 1039 | |
| 1040 | self->dbc = dbc; |
| 1041 | self->mydb = db; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1042 | |
| 1043 | INSERT_IN_DOUBLE_LINKED_LIST(self->mydb->children_cursors,self); |
| 1044 | if (txn && ((PyObject *)txn!=Py_None)) { |
| 1045 | INSERT_IN_DOUBLE_LINKED_LIST_TXN(txn->children_cursors,self); |
| 1046 | self->txn=txn; |
| 1047 | } else { |
| 1048 | self->txn=NULL; |
| 1049 | } |
| 1050 | |
Gregory P. Smith | a703a21 | 2003-11-03 01:04:41 +0000 | [diff] [blame] | 1051 | self->in_weakreflist = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1052 | Py_INCREF(self->mydb); |
| 1053 | return self; |
| 1054 | } |
| 1055 | |
| 1056 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1057 | /* Forward declaration */ |
| 1058 | static PyObject *DBC_close_internal(DBCursorObject* self); |
| 1059 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1060 | static void |
| 1061 | DBCursor_dealloc(DBCursorObject* self) |
| 1062 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1063 | PyObject *dummy; |
Gregory P. Smith | a703a21 | 2003-11-03 01:04:41 +0000 | [diff] [blame] | 1064 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1065 | if (self->dbc != NULL) { |
Jesus Cea | 5cd5f12 | 2008-09-23 18:54:08 +0000 | [diff] [blame] | 1066 | dummy=DBC_close_internal(self); |
| 1067 | /* |
| 1068 | ** Raising exceptions while doing |
| 1069 | ** garbage collection is a fatal error. |
| 1070 | */ |
| 1071 | if (dummy) |
| 1072 | Py_DECREF(dummy); |
| 1073 | else |
| 1074 | PyErr_Clear(); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1075 | } |
Gregory P. Smith | a703a21 | 2003-11-03 01:04:41 +0000 | [diff] [blame] | 1076 | if (self->in_weakreflist != NULL) { |
| 1077 | PyObject_ClearWeakRefs((PyObject *) self); |
| 1078 | } |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1079 | Py_DECREF(self->mydb); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1080 | PyObject_Del(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | |
| 1084 | static DBEnvObject* |
| 1085 | newDBEnvObject(int flags) |
| 1086 | { |
| 1087 | int err; |
Neal Norwitz | b4a5581 | 2004-07-09 23:30:57 +0000 | [diff] [blame] | 1088 | DBEnvObject* self = PyObject_New(DBEnvObject, &DBEnv_Type); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1089 | if (self == NULL) |
| 1090 | return NULL; |
| 1091 | |
Jesus Cea | 5cd5f12 | 2008-09-23 18:54:08 +0000 | [diff] [blame] | 1092 | self->db_env = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1093 | self->closed = 1; |
| 1094 | self->flags = flags; |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 1095 | self->moduleFlags.getReturnsNone = DEFAULT_GET_RETURNS_NONE; |
| 1096 | self->moduleFlags.cursorSetReturnsNone = DEFAULT_CURSOR_SET_RETURNS_NONE; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1097 | self->children_dbs = NULL; |
| 1098 | self->children_txns = NULL; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1099 | Py_INCREF(Py_None); |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 1100 | self->private_obj = Py_None; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1101 | Py_INCREF(Py_None); |
| 1102 | self->rep_transport = Py_None; |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 1103 | self->in_weakreflist = NULL; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1104 | self->event_notifyCallback = NULL; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1105 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1106 | MYDB_BEGIN_ALLOW_THREADS; |
| 1107 | err = db_env_create(&self->db_env, flags); |
| 1108 | MYDB_END_ALLOW_THREADS; |
| 1109 | if (makeDBError(err)) { |
Gregory P. Smith | 664782e | 2008-05-17 06:12:02 +0000 | [diff] [blame] | 1110 | Py_DECREF(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1111 | self = NULL; |
| 1112 | } |
| 1113 | else { |
| 1114 | self->db_env->set_errcall(self->db_env, _db_errorCallback); |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 1115 | self->db_env->app_private = self; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1116 | } |
| 1117 | return self; |
| 1118 | } |
| 1119 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1120 | /* Forward declaration */ |
| 1121 | static PyObject *DBEnv_close_internal(DBEnvObject* self, int flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1122 | |
| 1123 | static void |
| 1124 | DBEnv_dealloc(DBEnvObject* self) |
| 1125 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1126 | PyObject *dummy; |
| 1127 | |
Jesus Cea | ac25fab | 2008-09-03 17:50:32 +0000 | [diff] [blame] | 1128 | if (self->db_env) { |
Jesus Cea | 5cd5f12 | 2008-09-23 18:54:08 +0000 | [diff] [blame] | 1129 | dummy=DBEnv_close_internal(self, 0); |
| 1130 | /* |
| 1131 | ** Raising exceptions while doing |
| 1132 | ** garbage collection is a fatal error. |
| 1133 | */ |
| 1134 | if (dummy) |
| 1135 | Py_DECREF(dummy); |
| 1136 | else |
| 1137 | PyErr_Clear(); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1140 | Py_XDECREF(self->event_notifyCallback); |
| 1141 | self->event_notifyCallback = NULL; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1142 | |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 1143 | if (self->in_weakreflist != NULL) { |
| 1144 | PyObject_ClearWeakRefs((PyObject *) self); |
| 1145 | } |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 1146 | Py_DECREF(self->private_obj); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1147 | Py_DECREF(self->rep_transport); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1148 | PyObject_Del(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | |
| 1152 | static DBTxnObject* |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1153 | newDBTxnObject(DBEnvObject* myenv, DBTxnObject *parent, DB_TXN *txn, int flags) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1154 | { |
| 1155 | int err; |
Gregory P. Smith | 664782e | 2008-05-17 06:12:02 +0000 | [diff] [blame] | 1156 | DB_TXN *parent_txn = NULL; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1157 | |
Neal Norwitz | b4a5581 | 2004-07-09 23:30:57 +0000 | [diff] [blame] | 1158 | DBTxnObject* self = PyObject_New(DBTxnObject, &DBTxn_Type); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1159 | if (self == NULL) |
| 1160 | return NULL; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1161 | |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 1162 | self->in_weakreflist = NULL; |
Gregory P. Smith | 664782e | 2008-05-17 06:12:02 +0000 | [diff] [blame] | 1163 | self->children_txns = NULL; |
| 1164 | self->children_dbs = NULL; |
| 1165 | self->children_cursors = NULL; |
| 1166 | self->children_sequences = NULL; |
| 1167 | self->flag_prepare = 0; |
| 1168 | self->parent_txn = NULL; |
| 1169 | self->env = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1170 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1171 | if (parent && ((PyObject *)parent!=Py_None)) { |
Gregory P. Smith | 664782e | 2008-05-17 06:12:02 +0000 | [diff] [blame] | 1172 | parent_txn = parent->txn; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1173 | } |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1174 | |
| 1175 | if (txn) { |
Gregory P. Smith | 664782e | 2008-05-17 06:12:02 +0000 | [diff] [blame] | 1176 | self->txn = txn; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1177 | } else { |
| 1178 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1179 | err = myenv->db_env->txn_begin(myenv->db_env, parent_txn, &(self->txn), flags); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1180 | MYDB_END_ALLOW_THREADS; |
| 1181 | |
| 1182 | if (makeDBError(err)) { |
Gregory P. Smith | 664782e | 2008-05-17 06:12:02 +0000 | [diff] [blame] | 1183 | Py_DECREF(self); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1184 | return NULL; |
| 1185 | } |
| 1186 | } |
| 1187 | |
Gregory P. Smith | 664782e | 2008-05-17 06:12:02 +0000 | [diff] [blame] | 1188 | /* Can't use 'parent' because could be 'parent==Py_None' */ |
| 1189 | if (parent_txn) { |
| 1190 | self->parent_txn = parent; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1191 | Py_INCREF(parent); |
| 1192 | self->env = NULL; |
Gregory P. Smith | 664782e | 2008-05-17 06:12:02 +0000 | [diff] [blame] | 1193 | INSERT_IN_DOUBLE_LINKED_LIST(parent->children_txns, self); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1194 | } else { |
Gregory P. Smith | 664782e | 2008-05-17 06:12:02 +0000 | [diff] [blame] | 1195 | self->parent_txn = NULL; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1196 | Py_INCREF(myenv); |
| 1197 | self->env = myenv; |
Gregory P. Smith | 664782e | 2008-05-17 06:12:02 +0000 | [diff] [blame] | 1198 | INSERT_IN_DOUBLE_LINKED_LIST(myenv->children_txns, self); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1201 | return self; |
| 1202 | } |
| 1203 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1204 | /* Forward declaration */ |
| 1205 | static PyObject * |
| 1206 | DBTxn_abort_discard_internal(DBTxnObject* self, int discard); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1207 | |
| 1208 | static void |
| 1209 | DBTxn_dealloc(DBTxnObject* self) |
| 1210 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1211 | PyObject *dummy; |
| 1212 | |
| 1213 | if (self->txn) { |
| 1214 | int flag_prepare = self->flag_prepare; |
Jesus Cea | 5cd5f12 | 2008-09-23 18:54:08 +0000 | [diff] [blame] | 1215 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1216 | dummy=DBTxn_abort_discard_internal(self,0); |
Jesus Cea | 5cd5f12 | 2008-09-23 18:54:08 +0000 | [diff] [blame] | 1217 | /* |
| 1218 | ** Raising exceptions while doing |
| 1219 | ** garbage collection is a fatal error. |
| 1220 | */ |
| 1221 | if (dummy) |
| 1222 | Py_DECREF(dummy); |
| 1223 | else |
| 1224 | PyErr_Clear(); |
| 1225 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1226 | if (!flag_prepare) { |
| 1227 | PyErr_Warn(PyExc_RuntimeWarning, |
| 1228 | "DBTxn aborted in destructor. No prior commit() or abort()."); |
| 1229 | } |
| 1230 | } |
| 1231 | |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 1232 | if (self->in_weakreflist != NULL) { |
| 1233 | PyObject_ClearWeakRefs((PyObject *) self); |
| 1234 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1235 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1236 | if (self->env) { |
| 1237 | Py_DECREF(self->env); |
| 1238 | } else { |
| 1239 | Py_DECREF(self->parent_txn); |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 1240 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1241 | PyObject_Del(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | |
| 1245 | static DBLockObject* |
| 1246 | newDBLockObject(DBEnvObject* myenv, u_int32_t locker, DBT* obj, |
| 1247 | db_lockmode_t lock_mode, int flags) |
| 1248 | { |
| 1249 | int err; |
Neal Norwitz | b4a5581 | 2004-07-09 23:30:57 +0000 | [diff] [blame] | 1250 | DBLockObject* self = PyObject_New(DBLockObject, &DBLock_Type); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1251 | if (self == NULL) |
| 1252 | return NULL; |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 1253 | self->in_weakreflist = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1254 | |
| 1255 | MYDB_BEGIN_ALLOW_THREADS; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1256 | err = myenv->db_env->lock_get(myenv->db_env, locker, flags, obj, lock_mode, |
| 1257 | &self->lock); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1258 | MYDB_END_ALLOW_THREADS; |
| 1259 | if (makeDBError(err)) { |
Gregory P. Smith | 664782e | 2008-05-17 06:12:02 +0000 | [diff] [blame] | 1260 | Py_DECREF(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1261 | self = NULL; |
| 1262 | } |
| 1263 | |
| 1264 | return self; |
| 1265 | } |
| 1266 | |
| 1267 | |
| 1268 | static void |
| 1269 | DBLock_dealloc(DBLockObject* self) |
| 1270 | { |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 1271 | if (self->in_weakreflist != NULL) { |
| 1272 | PyObject_ClearWeakRefs((PyObject *) self); |
| 1273 | } |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 1274 | /* TODO: is this lock held? should we release it? */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1275 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1276 | PyObject_Del(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 1280 | #if (DBVER >= 43) |
| 1281 | static DBSequenceObject* |
| 1282 | newDBSequenceObject(DBObject* mydb, int flags) |
| 1283 | { |
| 1284 | int err; |
| 1285 | DBSequenceObject* self = PyObject_New(DBSequenceObject, &DBSequence_Type); |
| 1286 | if (self == NULL) |
| 1287 | return NULL; |
| 1288 | Py_INCREF(mydb); |
| 1289 | self->mydb = mydb; |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 1290 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1291 | INSERT_IN_DOUBLE_LINKED_LIST(self->mydb->children_sequences,self); |
Gregory P. Smith | 664782e | 2008-05-17 06:12:02 +0000 | [diff] [blame] | 1292 | self->txn = NULL; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1293 | |
| 1294 | self->in_weakreflist = NULL; |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 1295 | |
| 1296 | MYDB_BEGIN_ALLOW_THREADS; |
| 1297 | err = db_sequence_create(&self->sequence, self->mydb->db, flags); |
| 1298 | MYDB_END_ALLOW_THREADS; |
| 1299 | if (makeDBError(err)) { |
Gregory P. Smith | 664782e | 2008-05-17 06:12:02 +0000 | [diff] [blame] | 1300 | Py_DECREF(self); |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 1301 | self = NULL; |
| 1302 | } |
| 1303 | |
| 1304 | return self; |
| 1305 | } |
| 1306 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1307 | /* Forward declaration */ |
| 1308 | static PyObject |
| 1309 | *DBSequence_close_internal(DBSequenceObject* self, int flags, int do_not_close); |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 1310 | |
| 1311 | static void |
| 1312 | DBSequence_dealloc(DBSequenceObject* self) |
| 1313 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1314 | PyObject *dummy; |
| 1315 | |
| 1316 | if (self->sequence != NULL) { |
| 1317 | dummy=DBSequence_close_internal(self,0,0); |
Jesus Cea | 5cd5f12 | 2008-09-23 18:54:08 +0000 | [diff] [blame] | 1318 | /* |
| 1319 | ** Raising exceptions while doing |
| 1320 | ** garbage collection is a fatal error. |
| 1321 | */ |
| 1322 | if (dummy) |
| 1323 | Py_DECREF(dummy); |
| 1324 | else |
| 1325 | PyErr_Clear(); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 1328 | if (self->in_weakreflist != NULL) { |
| 1329 | PyObject_ClearWeakRefs((PyObject *) self); |
| 1330 | } |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 1331 | |
| 1332 | Py_DECREF(self->mydb); |
| 1333 | PyObject_Del(self); |
| 1334 | } |
| 1335 | #endif |
| 1336 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1337 | /* --------------------------------------------------------------------- */ |
| 1338 | /* DB methods */ |
| 1339 | |
| 1340 | static PyObject* |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 1341 | DB_append(DBObject* self, PyObject* args, PyObject* kwargs) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1342 | { |
| 1343 | PyObject* txnobj = NULL; |
| 1344 | PyObject* dataobj; |
| 1345 | db_recno_t recno; |
| 1346 | DBT key, data; |
| 1347 | DB_TXN *txn = NULL; |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 1348 | static char* kwnames[] = { "data", "txn", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1349 | |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 1350 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:append", kwnames, |
| 1351 | &dataobj, &txnobj)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1352 | return NULL; |
| 1353 | |
| 1354 | CHECK_DB_NOT_CLOSED(self); |
| 1355 | |
| 1356 | /* make a dummy key out of a recno */ |
| 1357 | recno = 0; |
| 1358 | CLEAR_DBT(key); |
| 1359 | key.data = &recno; |
| 1360 | key.size = sizeof(recno); |
| 1361 | key.ulen = key.size; |
| 1362 | key.flags = DB_DBT_USERMEM; |
| 1363 | |
| 1364 | if (!make_dbt(dataobj, &data)) return NULL; |
| 1365 | if (!checkTxnObj(txnobj, &txn)) return NULL; |
| 1366 | |
| 1367 | if (-1 == _DB_put(self, txn, &key, &data, DB_APPEND)) |
| 1368 | return NULL; |
| 1369 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1370 | return NUMBER_FromLong(recno); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1374 | static int |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1375 | _db_associateCallback(DB* db, const DBT* priKey, const DBT* priData, |
| 1376 | DBT* secKey) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1377 | { |
| 1378 | int retval = DB_DONOTINDEX; |
| 1379 | DBObject* secondaryDB = (DBObject*)db->app_private; |
| 1380 | PyObject* callback = secondaryDB->associateCallback; |
| 1381 | int type = secondaryDB->primaryDBType; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1382 | PyObject* args; |
Thomas Wouters | 89ba381 | 2006-03-07 14:14:51 +0000 | [diff] [blame] | 1383 | PyObject* result = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1384 | |
| 1385 | |
| 1386 | if (callback != NULL) { |
| 1387 | MYDB_BEGIN_BLOCK_THREADS; |
| 1388 | |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 1389 | if (type == DB_RECNO || type == DB_QUEUE) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1390 | args = BuildValue_LS(*((db_recno_t*)priKey->data), priData->data, priData->size); |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 1391 | else |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1392 | args = BuildValue_SS(priKey->data, priKey->size, priData->data, priData->size); |
Thomas Wouters | 098f694 | 2006-03-07 14:13:17 +0000 | [diff] [blame] | 1393 | if (args != NULL) { |
Thomas Wouters | 098f694 | 2006-03-07 14:13:17 +0000 | [diff] [blame] | 1394 | result = PyEval_CallObject(callback, args); |
| 1395 | } |
| 1396 | if (args == NULL || result == NULL) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1397 | PyErr_Print(); |
| 1398 | } |
| 1399 | else if (result == Py_None) { |
| 1400 | retval = DB_DONOTINDEX; |
| 1401 | } |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1402 | else if (NUMBER_Check(result)) { |
| 1403 | retval = NUMBER_AsLong(result); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1404 | } |
Christian Heimes | 593daf5 | 2008-05-26 12:51:38 +0000 | [diff] [blame] | 1405 | else if (PyBytes_Check(result)) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1406 | char* data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1407 | Py_ssize_t size; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1408 | |
| 1409 | CLEAR_DBT(*secKey); |
Christian Heimes | 593daf5 | 2008-05-26 12:51:38 +0000 | [diff] [blame] | 1410 | PyBytes_AsStringAndSize(result, &data, &size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1411 | secKey->flags = DB_DBT_APPMALLOC; /* DB will free */ |
| 1412 | secKey->data = malloc(size); /* TODO, check this */ |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1413 | if (secKey->data) { |
| 1414 | memcpy(secKey->data, data, size); |
| 1415 | secKey->size = size; |
| 1416 | retval = 0; |
| 1417 | } |
| 1418 | else { |
| 1419 | PyErr_SetString(PyExc_MemoryError, |
| 1420 | "malloc failed in _db_associateCallback"); |
| 1421 | PyErr_Print(); |
| 1422 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1423 | } |
| 1424 | else { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1425 | PyErr_SetString( |
| 1426 | PyExc_TypeError, |
| 1427 | "DB associate callback should return DB_DONOTINDEX or string."); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1428 | PyErr_Print(); |
| 1429 | } |
| 1430 | |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 1431 | Py_XDECREF(args); |
| 1432 | Py_XDECREF(result); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1433 | |
| 1434 | MYDB_END_BLOCK_THREADS; |
| 1435 | } |
| 1436 | return retval; |
| 1437 | } |
| 1438 | |
| 1439 | |
| 1440 | static PyObject* |
| 1441 | DB_associate(DBObject* self, PyObject* args, PyObject* kwargs) |
| 1442 | { |
| 1443 | int err, flags=0; |
| 1444 | DBObject* secondaryDB; |
| 1445 | PyObject* callback; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1446 | #if (DBVER >= 41) |
| 1447 | PyObject *txnobj = NULL; |
| 1448 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1449 | static char* kwnames[] = {"secondaryDB", "callback", "flags", "txn", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 1450 | NULL}; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1451 | #else |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1452 | static char* kwnames[] = {"secondaryDB", "callback", "flags", NULL}; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1453 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1454 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1455 | #if (DBVER >= 41) |
| 1456 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|iO:associate", kwnames, |
| 1457 | &secondaryDB, &callback, &flags, |
| 1458 | &txnobj)) { |
| 1459 | #else |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1460 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:associate", kwnames, |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1461 | &secondaryDB, &callback, &flags)) { |
| 1462 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1463 | return NULL; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | #if (DBVER >= 41) |
| 1467 | if (!checkTxnObj(txnobj, &txn)) return NULL; |
| 1468 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1469 | |
| 1470 | CHECK_DB_NOT_CLOSED(self); |
| 1471 | if (!DBObject_Check(secondaryDB)) { |
| 1472 | makeTypeError("DB", (PyObject*)secondaryDB); |
| 1473 | return NULL; |
| 1474 | } |
Gregory P. Smith | 91116b6 | 2005-06-06 10:28:06 +0000 | [diff] [blame] | 1475 | CHECK_DB_NOT_CLOSED(secondaryDB); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1476 | if (callback == Py_None) { |
| 1477 | callback = NULL; |
| 1478 | } |
| 1479 | else if (!PyCallable_Check(callback)) { |
| 1480 | makeTypeError("Callable", callback); |
| 1481 | return NULL; |
| 1482 | } |
| 1483 | |
| 1484 | /* Save a reference to the callback in the secondary DB. */ |
Gregory P. Smith | 692ca9a | 2005-06-06 09:55:06 +0000 | [diff] [blame] | 1485 | Py_XDECREF(secondaryDB->associateCallback); |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 1486 | Py_XINCREF(callback); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1487 | secondaryDB->associateCallback = callback; |
| 1488 | secondaryDB->primaryDBType = _DB_get_type(self); |
| 1489 | |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 1490 | /* PyEval_InitThreads is called here due to a quirk in python 1.5 |
| 1491 | * - 2.2.1 (at least) according to Russell Williamson <merel@wt.net>: |
| 1492 | * The global interepreter lock is not initialized until the first |
| 1493 | * thread is created using thread.start_new_thread() or fork() is |
| 1494 | * called. that would cause the ALLOW_THREADS here to segfault due |
| 1495 | * to a null pointer reference if no threads or child processes |
| 1496 | * have been created. This works around that and is a no-op if |
| 1497 | * threads have already been initialized. |
| 1498 | * (see pybsddb-users mailing list post on 2002-08-07) |
| 1499 | */ |
Gregory P. Smith | aa71f5f | 2003-01-17 07:56:16 +0000 | [diff] [blame] | 1500 | #ifdef WITH_THREAD |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 1501 | PyEval_InitThreads(); |
Gregory P. Smith | aa71f5f | 2003-01-17 07:56:16 +0000 | [diff] [blame] | 1502 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1503 | MYDB_BEGIN_ALLOW_THREADS; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1504 | #if (DBVER >= 41) |
| 1505 | err = self->db->associate(self->db, |
| 1506 | txn, |
| 1507 | secondaryDB->db, |
| 1508 | _db_associateCallback, |
| 1509 | flags); |
| 1510 | #else |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1511 | err = self->db->associate(self->db, |
| 1512 | secondaryDB->db, |
| 1513 | _db_associateCallback, |
| 1514 | flags); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1515 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1516 | MYDB_END_ALLOW_THREADS; |
| 1517 | |
| 1518 | if (err) { |
Gregory P. Smith | 692ca9a | 2005-06-06 09:55:06 +0000 | [diff] [blame] | 1519 | Py_XDECREF(secondaryDB->associateCallback); |
| 1520 | secondaryDB->associateCallback = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1521 | secondaryDB->primaryDBType = 0; |
| 1522 | } |
| 1523 | |
| 1524 | RETURN_IF_ERR(); |
| 1525 | RETURN_NONE(); |
| 1526 | } |
| 1527 | |
| 1528 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1529 | static PyObject* |
Jesus Cea | 5cd5f12 | 2008-09-23 18:54:08 +0000 | [diff] [blame] | 1530 | DB_close_internal(DBObject* self, int flags, int do_not_close) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1531 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1532 | PyObject *dummy; |
Jesus Cea | 5cd5f12 | 2008-09-23 18:54:08 +0000 | [diff] [blame] | 1533 | int err = 0; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1534 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1535 | if (self->db != NULL) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1536 | /* Can be NULL if db is not in an environment */ |
| 1537 | EXTRACT_FROM_DOUBLE_LINKED_LIST_MAYBE_NULL(self); |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 1538 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1539 | if (self->txn) { |
| 1540 | EXTRACT_FROM_DOUBLE_LINKED_LIST_TXN(self); |
| 1541 | self->txn=NULL; |
| 1542 | } |
| 1543 | |
| 1544 | while(self->children_cursors) { |
| 1545 | dummy=DBC_close_internal(self->children_cursors); |
| 1546 | Py_XDECREF(dummy); |
| 1547 | } |
| 1548 | |
| 1549 | #if (DBVER >= 43) |
| 1550 | while(self->children_sequences) { |
| 1551 | dummy=DBSequence_close_internal(self->children_sequences,0,0); |
| 1552 | Py_XDECREF(dummy); |
| 1553 | } |
| 1554 | #endif |
| 1555 | |
Jesus Cea | 5cd5f12 | 2008-09-23 18:54:08 +0000 | [diff] [blame] | 1556 | /* |
| 1557 | ** "do_not_close" is used to dispose all related objects in the |
| 1558 | ** tree, without actually releasing the "root" object. |
| 1559 | ** This is done, for example, because function calls like |
| 1560 | ** "DB.verify()" implicitly close the underlying handle. So |
| 1561 | ** the handle doesn't need to be closed, but related objects |
| 1562 | ** must be cleaned up. |
| 1563 | */ |
| 1564 | if (!do_not_close) { |
| 1565 | MYDB_BEGIN_ALLOW_THREADS; |
| 1566 | err = self->db->close(self->db, flags); |
| 1567 | MYDB_END_ALLOW_THREADS; |
| 1568 | self->db = NULL; |
| 1569 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1570 | RETURN_IF_ERR(); |
| 1571 | } |
| 1572 | RETURN_NONE(); |
| 1573 | } |
| 1574 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1575 | static PyObject* |
| 1576 | DB_close(DBObject* self, PyObject* args) |
| 1577 | { |
| 1578 | int flags=0; |
| 1579 | if (!PyArg_ParseTuple(args,"|i:close", &flags)) |
| 1580 | return NULL; |
Jesus Cea | 5cd5f12 | 2008-09-23 18:54:08 +0000 | [diff] [blame] | 1581 | return DB_close_internal(self, flags, 0); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1584 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1585 | static PyObject* |
| 1586 | _DB_consume(DBObject* self, PyObject* args, PyObject* kwargs, int consume_flag) |
| 1587 | { |
| 1588 | int err, flags=0, type; |
| 1589 | PyObject* txnobj = NULL; |
| 1590 | PyObject* retval = NULL; |
| 1591 | DBT key, data; |
| 1592 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1593 | static char* kwnames[] = { "txn", "flags", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1594 | |
| 1595 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oi:consume", kwnames, |
| 1596 | &txnobj, &flags)) |
| 1597 | return NULL; |
| 1598 | |
| 1599 | CHECK_DB_NOT_CLOSED(self); |
| 1600 | type = _DB_get_type(self); |
| 1601 | if (type == -1) |
| 1602 | return NULL; |
| 1603 | if (type != DB_QUEUE) { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1604 | PyErr_SetString(PyExc_TypeError, |
| 1605 | "Consume methods only allowed for Queue DB's"); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1606 | return NULL; |
| 1607 | } |
| 1608 | if (!checkTxnObj(txnobj, &txn)) |
| 1609 | return NULL; |
| 1610 | |
| 1611 | CLEAR_DBT(key); |
| 1612 | CLEAR_DBT(data); |
| 1613 | if (CHECK_DBFLAG(self, DB_THREAD)) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1614 | /* Tell Berkeley DB to malloc the return value (thread safe) */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1615 | data.flags = DB_DBT_MALLOC; |
| 1616 | key.flags = DB_DBT_MALLOC; |
| 1617 | } |
| 1618 | |
| 1619 | MYDB_BEGIN_ALLOW_THREADS; |
| 1620 | err = self->db->get(self->db, txn, &key, &data, flags|consume_flag); |
| 1621 | MYDB_END_ALLOW_THREADS; |
| 1622 | |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 1623 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 1624 | && self->moduleFlags.getReturnsNone) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1625 | err = 0; |
| 1626 | Py_INCREF(Py_None); |
| 1627 | retval = Py_None; |
| 1628 | } |
| 1629 | else if (!err) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1630 | retval = BuildValue_SS(key.data, key.size, data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1631 | FREE_DBT(key); |
| 1632 | FREE_DBT(data); |
| 1633 | } |
| 1634 | |
| 1635 | RETURN_IF_ERR(); |
| 1636 | return retval; |
| 1637 | } |
| 1638 | |
| 1639 | static PyObject* |
| 1640 | DB_consume(DBObject* self, PyObject* args, PyObject* kwargs, int consume_flag) |
| 1641 | { |
| 1642 | return _DB_consume(self, args, kwargs, DB_CONSUME); |
| 1643 | } |
| 1644 | |
| 1645 | static PyObject* |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1646 | DB_consume_wait(DBObject* self, PyObject* args, PyObject* kwargs, |
| 1647 | int consume_flag) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1648 | { |
| 1649 | return _DB_consume(self, args, kwargs, DB_CONSUME_WAIT); |
| 1650 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1651 | |
| 1652 | |
| 1653 | static PyObject* |
| 1654 | DB_cursor(DBObject* self, PyObject* args, PyObject* kwargs) |
| 1655 | { |
| 1656 | int err, flags=0; |
| 1657 | DBC* dbc; |
| 1658 | PyObject* txnobj = NULL; |
| 1659 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1660 | static char* kwnames[] = { "txn", "flags", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1661 | |
| 1662 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oi:cursor", kwnames, |
| 1663 | &txnobj, &flags)) |
| 1664 | return NULL; |
| 1665 | CHECK_DB_NOT_CLOSED(self); |
| 1666 | if (!checkTxnObj(txnobj, &txn)) |
| 1667 | return NULL; |
| 1668 | |
| 1669 | MYDB_BEGIN_ALLOW_THREADS; |
| 1670 | err = self->db->cursor(self->db, txn, &dbc, flags); |
| 1671 | MYDB_END_ALLOW_THREADS; |
| 1672 | RETURN_IF_ERR(); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1673 | return (PyObject*) newDBCursorObject(dbc, (DBTxnObject *)txnobj, self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | |
| 1677 | static PyObject* |
| 1678 | DB_delete(DBObject* self, PyObject* args, PyObject* kwargs) |
| 1679 | { |
| 1680 | PyObject* txnobj = NULL; |
| 1681 | int flags = 0; |
| 1682 | PyObject* keyobj; |
| 1683 | DBT key; |
| 1684 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1685 | static char* kwnames[] = { "key", "txn", "flags", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1686 | |
| 1687 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|Oi:delete", kwnames, |
| 1688 | &keyobj, &txnobj, &flags)) |
| 1689 | return NULL; |
| 1690 | CHECK_DB_NOT_CLOSED(self); |
| 1691 | if (!make_key_dbt(self, keyobj, &key, NULL)) |
| 1692 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1693 | if (!checkTxnObj(txnobj, &txn)) { |
| 1694 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1695 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1696 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1697 | |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1698 | if (-1 == _DB_delete(self, txn, &key, 0)) { |
| 1699 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1700 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1701 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1702 | |
| 1703 | FREE_DBT(key); |
| 1704 | RETURN_NONE(); |
| 1705 | } |
| 1706 | |
| 1707 | |
| 1708 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1709 | DB_fd(DBObject* self) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1710 | { |
| 1711 | int err, the_fd; |
| 1712 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1713 | CHECK_DB_NOT_CLOSED(self); |
| 1714 | |
| 1715 | MYDB_BEGIN_ALLOW_THREADS; |
| 1716 | err = self->db->fd(self->db, &the_fd); |
| 1717 | MYDB_END_ALLOW_THREADS; |
| 1718 | RETURN_IF_ERR(); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1719 | return NUMBER_FromLong(the_fd); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
| 1722 | |
| 1723 | static PyObject* |
| 1724 | DB_get(DBObject* self, PyObject* args, PyObject* kwargs) |
| 1725 | { |
| 1726 | int err, flags=0; |
| 1727 | PyObject* txnobj = NULL; |
| 1728 | PyObject* keyobj; |
| 1729 | PyObject* dfltobj = NULL; |
| 1730 | PyObject* retval = NULL; |
| 1731 | int dlen = -1; |
| 1732 | int doff = -1; |
| 1733 | DBT key, data; |
| 1734 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1735 | static char* kwnames[] = {"key", "default", "txn", "flags", "dlen", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 1736 | "doff", NULL}; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1737 | |
| 1738 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OOiii:get", kwnames, |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1739 | &keyobj, &dfltobj, &txnobj, &flags, &dlen, |
| 1740 | &doff)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1741 | return NULL; |
| 1742 | |
| 1743 | CHECK_DB_NOT_CLOSED(self); |
| 1744 | if (!make_key_dbt(self, keyobj, &key, &flags)) |
| 1745 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1746 | if (!checkTxnObj(txnobj, &txn)) { |
| 1747 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1748 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1749 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1750 | |
| 1751 | CLEAR_DBT(data); |
| 1752 | if (CHECK_DBFLAG(self, DB_THREAD)) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1753 | /* Tell Berkeley DB to malloc the return value (thread safe) */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1754 | data.flags = DB_DBT_MALLOC; |
| 1755 | } |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1756 | if (!add_partial_dbt(&data, dlen, doff)) { |
| 1757 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1758 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1759 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1760 | |
| 1761 | MYDB_BEGIN_ALLOW_THREADS; |
| 1762 | err = self->db->get(self->db, txn, &key, &data, flags); |
| 1763 | MYDB_END_ALLOW_THREADS; |
| 1764 | |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 1765 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) && (dfltobj != NULL)) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1766 | err = 0; |
| 1767 | Py_INCREF(dfltobj); |
| 1768 | retval = dfltobj; |
| 1769 | } |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 1770 | else if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 1771 | && self->moduleFlags.getReturnsNone) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1772 | err = 0; |
| 1773 | Py_INCREF(Py_None); |
| 1774 | retval = Py_None; |
| 1775 | } |
| 1776 | else if (!err) { |
| 1777 | if (flags & DB_SET_RECNO) /* return both key and data */ |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1778 | retval = BuildValue_SS(key.data, key.size, data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1779 | else /* return just the data */ |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1780 | retval = Build_PyString(data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1781 | FREE_DBT(data); |
| 1782 | } |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1783 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1784 | |
| 1785 | RETURN_IF_ERR(); |
| 1786 | return retval; |
| 1787 | } |
| 1788 | |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1789 | static PyObject* |
| 1790 | DB_pget(DBObject* self, PyObject* args, PyObject* kwargs) |
| 1791 | { |
| 1792 | int err, flags=0; |
| 1793 | PyObject* txnobj = NULL; |
| 1794 | PyObject* keyobj; |
| 1795 | PyObject* dfltobj = NULL; |
| 1796 | PyObject* retval = NULL; |
| 1797 | int dlen = -1; |
| 1798 | int doff = -1; |
| 1799 | DBT key, pkey, data; |
| 1800 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1801 | static char* kwnames[] = {"key", "default", "txn", "flags", "dlen", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 1802 | "doff", NULL}; |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1803 | |
| 1804 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OOiii:pget", kwnames, |
| 1805 | &keyobj, &dfltobj, &txnobj, &flags, &dlen, |
| 1806 | &doff)) |
| 1807 | return NULL; |
| 1808 | |
| 1809 | CHECK_DB_NOT_CLOSED(self); |
| 1810 | if (!make_key_dbt(self, keyobj, &key, &flags)) |
| 1811 | return NULL; |
| 1812 | if (!checkTxnObj(txnobj, &txn)) { |
| 1813 | FREE_DBT(key); |
| 1814 | return NULL; |
| 1815 | } |
| 1816 | |
| 1817 | CLEAR_DBT(data); |
| 1818 | if (CHECK_DBFLAG(self, DB_THREAD)) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1819 | /* Tell Berkeley DB to malloc the return value (thread safe) */ |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1820 | data.flags = DB_DBT_MALLOC; |
| 1821 | } |
| 1822 | if (!add_partial_dbt(&data, dlen, doff)) { |
| 1823 | FREE_DBT(key); |
| 1824 | return NULL; |
| 1825 | } |
| 1826 | |
| 1827 | CLEAR_DBT(pkey); |
| 1828 | pkey.flags = DB_DBT_MALLOC; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1829 | |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1830 | MYDB_BEGIN_ALLOW_THREADS; |
| 1831 | err = self->db->pget(self->db, txn, &key, &pkey, &data, flags); |
| 1832 | MYDB_END_ALLOW_THREADS; |
| 1833 | |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 1834 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) && (dfltobj != NULL)) { |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1835 | err = 0; |
| 1836 | Py_INCREF(dfltobj); |
| 1837 | retval = dfltobj; |
| 1838 | } |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 1839 | else if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 1840 | && self->moduleFlags.getReturnsNone) { |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1841 | err = 0; |
| 1842 | Py_INCREF(Py_None); |
| 1843 | retval = Py_None; |
| 1844 | } |
| 1845 | else if (!err) { |
| 1846 | PyObject *pkeyObj; |
| 1847 | PyObject *dataObj; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1848 | dataObj = Build_PyString(data.data, data.size); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1849 | |
| 1850 | if (self->primaryDBType == DB_RECNO || |
| 1851 | self->primaryDBType == DB_QUEUE) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1852 | pkeyObj = NUMBER_FromLong(*(int *)pkey.data); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1853 | else |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1854 | pkeyObj = Build_PyString(pkey.data, pkey.size); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1855 | |
| 1856 | if (flags & DB_SET_RECNO) /* return key , pkey and data */ |
| 1857 | { |
| 1858 | PyObject *keyObj; |
| 1859 | int type = _DB_get_type(self); |
| 1860 | if (type == DB_RECNO || type == DB_QUEUE) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1861 | keyObj = NUMBER_FromLong(*(int *)key.data); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1862 | else |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1863 | keyObj = Build_PyString(key.data, key.size); |
Gregory P. Smith | fd049a6 | 2006-01-30 00:22:08 +0000 | [diff] [blame] | 1864 | #if (PY_VERSION_HEX >= 0x02040000) |
Gregory P. Smith | 4e414d8 | 2006-01-24 19:55:02 +0000 | [diff] [blame] | 1865 | retval = PyTuple_Pack(3, keyObj, pkeyObj, dataObj); |
Gregory P. Smith | fd049a6 | 2006-01-30 00:22:08 +0000 | [diff] [blame] | 1866 | #else |
| 1867 | retval = Py_BuildValue("OOO", keyObj, pkeyObj, dataObj); |
| 1868 | #endif |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 1869 | Py_DECREF(keyObj); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1870 | } |
| 1871 | else /* return just the pkey and data */ |
| 1872 | { |
Gregory P. Smith | fd049a6 | 2006-01-30 00:22:08 +0000 | [diff] [blame] | 1873 | #if (PY_VERSION_HEX >= 0x02040000) |
Gregory P. Smith | 4e414d8 | 2006-01-24 19:55:02 +0000 | [diff] [blame] | 1874 | retval = PyTuple_Pack(2, pkeyObj, dataObj); |
Gregory P. Smith | fd049a6 | 2006-01-30 00:22:08 +0000 | [diff] [blame] | 1875 | #else |
| 1876 | retval = Py_BuildValue("OO", pkeyObj, dataObj); |
| 1877 | #endif |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1878 | } |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 1879 | Py_DECREF(dataObj); |
| 1880 | Py_DECREF(pkeyObj); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1881 | FREE_DBT(pkey); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1882 | FREE_DBT(data); |
| 1883 | } |
| 1884 | FREE_DBT(key); |
| 1885 | |
| 1886 | RETURN_IF_ERR(); |
| 1887 | return retval; |
| 1888 | } |
| 1889 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1890 | |
| 1891 | /* Return size of entry */ |
| 1892 | static PyObject* |
| 1893 | DB_get_size(DBObject* self, PyObject* args, PyObject* kwargs) |
| 1894 | { |
| 1895 | int err, flags=0; |
| 1896 | PyObject* txnobj = NULL; |
| 1897 | PyObject* keyobj; |
| 1898 | PyObject* retval = NULL; |
| 1899 | DBT key, data; |
| 1900 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1901 | static char* kwnames[] = { "key", "txn", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1902 | |
| 1903 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:get_size", kwnames, |
| 1904 | &keyobj, &txnobj)) |
| 1905 | return NULL; |
| 1906 | CHECK_DB_NOT_CLOSED(self); |
| 1907 | if (!make_key_dbt(self, keyobj, &key, &flags)) |
| 1908 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1909 | if (!checkTxnObj(txnobj, &txn)) { |
| 1910 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1911 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1912 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1913 | CLEAR_DBT(data); |
| 1914 | |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 1915 | /* We don't allocate any memory, forcing a DB_BUFFER_SMALL error and |
| 1916 | thus getting the record size. */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1917 | data.flags = DB_DBT_USERMEM; |
| 1918 | data.ulen = 0; |
| 1919 | MYDB_BEGIN_ALLOW_THREADS; |
| 1920 | err = self->db->get(self->db, txn, &key, &data, flags); |
| 1921 | MYDB_END_ALLOW_THREADS; |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 1922 | if (err == DB_BUFFER_SMALL) { |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1923 | retval = NUMBER_FromLong((long)data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1924 | err = 0; |
| 1925 | } |
| 1926 | |
| 1927 | FREE_DBT(key); |
| 1928 | FREE_DBT(data); |
| 1929 | RETURN_IF_ERR(); |
| 1930 | return retval; |
| 1931 | } |
| 1932 | |
| 1933 | |
| 1934 | static PyObject* |
| 1935 | DB_get_both(DBObject* self, PyObject* args, PyObject* kwargs) |
| 1936 | { |
| 1937 | int err, flags=0; |
| 1938 | PyObject* txnobj = NULL; |
| 1939 | PyObject* keyobj; |
| 1940 | PyObject* dataobj; |
| 1941 | PyObject* retval = NULL; |
| 1942 | DBT key, data; |
Neal Norwitz | 994ebed | 2007-06-03 20:32:50 +0000 | [diff] [blame] | 1943 | void *orig_data; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1944 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1945 | static char* kwnames[] = { "key", "data", "txn", "flags", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1946 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1947 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|Oi:get_both", kwnames, |
| 1948 | &keyobj, &dataobj, &txnobj, &flags)) |
| 1949 | return NULL; |
| 1950 | |
| 1951 | CHECK_DB_NOT_CLOSED(self); |
| 1952 | if (!make_key_dbt(self, keyobj, &key, NULL)) |
| 1953 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1954 | if ( !make_dbt(dataobj, &data) || |
| 1955 | !checkTxnObj(txnobj, &txn) ) |
| 1956 | { |
| 1957 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1958 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1959 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1960 | |
| 1961 | flags |= DB_GET_BOTH; |
Neal Norwitz | 994ebed | 2007-06-03 20:32:50 +0000 | [diff] [blame] | 1962 | orig_data = data.data; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1963 | |
| 1964 | if (CHECK_DBFLAG(self, DB_THREAD)) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1965 | /* Tell Berkeley DB to malloc the return value (thread safe) */ |
Neal Norwitz | 994ebed | 2007-06-03 20:32:50 +0000 | [diff] [blame] | 1966 | /* XXX(nnorwitz): At least 4.4.20 and 4.5.20 require this flag. */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1967 | data.flags = DB_DBT_MALLOC; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1968 | } |
| 1969 | |
| 1970 | MYDB_BEGIN_ALLOW_THREADS; |
| 1971 | err = self->db->get(self->db, txn, &key, &data, flags); |
| 1972 | MYDB_END_ALLOW_THREADS; |
| 1973 | |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 1974 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 1975 | && self->moduleFlags.getReturnsNone) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1976 | err = 0; |
| 1977 | Py_INCREF(Py_None); |
| 1978 | retval = Py_None; |
| 1979 | } |
| 1980 | else if (!err) { |
Neal Norwitz | 994ebed | 2007-06-03 20:32:50 +0000 | [diff] [blame] | 1981 | /* XXX(nnorwitz): can we do: retval = dataobj; Py_INCREF(retval); */ |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 1982 | retval = Build_PyString(data.data, data.size); |
Neal Norwitz | 994ebed | 2007-06-03 20:32:50 +0000 | [diff] [blame] | 1983 | |
| 1984 | /* Even though the flags require DB_DBT_MALLOC, data is not always |
| 1985 | allocated. 4.4: allocated, 4.5: *not* allocated. :-( */ |
| 1986 | if (data.data != orig_data) |
| 1987 | FREE_DBT(data); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | FREE_DBT(key); |
| 1991 | RETURN_IF_ERR(); |
| 1992 | return retval; |
| 1993 | } |
| 1994 | |
| 1995 | |
| 1996 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 1997 | DB_get_byteswapped(DBObject* self) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1998 | { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1999 | int err = 0; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2000 | int retval = -1; |
| 2001 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2002 | CHECK_DB_NOT_CLOSED(self); |
| 2003 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2004 | MYDB_BEGIN_ALLOW_THREADS; |
| 2005 | err = self->db->get_byteswapped(self->db, &retval); |
| 2006 | MYDB_END_ALLOW_THREADS; |
| 2007 | RETURN_IF_ERR(); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2008 | return NUMBER_FromLong(retval); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
| 2011 | |
| 2012 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2013 | DB_get_type(DBObject* self) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2014 | { |
| 2015 | int type; |
| 2016 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2017 | CHECK_DB_NOT_CLOSED(self); |
| 2018 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2019 | type = _DB_get_type(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2020 | if (type == -1) |
| 2021 | return NULL; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2022 | return NUMBER_FromLong(type); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | |
| 2026 | static PyObject* |
| 2027 | DB_join(DBObject* self, PyObject* args) |
| 2028 | { |
| 2029 | int err, flags=0; |
| 2030 | int length, x; |
| 2031 | PyObject* cursorsObj; |
| 2032 | DBC** cursors; |
| 2033 | DBC* dbc; |
| 2034 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2035 | if (!PyArg_ParseTuple(args,"O|i:join", &cursorsObj, &flags)) |
| 2036 | return NULL; |
| 2037 | |
| 2038 | CHECK_DB_NOT_CLOSED(self); |
| 2039 | |
| 2040 | if (!PySequence_Check(cursorsObj)) { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2041 | PyErr_SetString(PyExc_TypeError, |
| 2042 | "Sequence of DBCursor objects expected"); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2043 | return NULL; |
| 2044 | } |
| 2045 | |
| 2046 | length = PyObject_Length(cursorsObj); |
| 2047 | cursors = malloc((length+1) * sizeof(DBC*)); |
Neal Norwitz | 5aa9689 | 2006-08-13 18:11:43 +0000 | [diff] [blame] | 2048 | if (!cursors) { |
| 2049 | PyErr_NoMemory(); |
| 2050 | return NULL; |
| 2051 | } |
| 2052 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2053 | cursors[length] = NULL; |
| 2054 | for (x=0; x<length; x++) { |
| 2055 | PyObject* item = PySequence_GetItem(cursorsObj, x); |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2056 | if (item == NULL) { |
| 2057 | free(cursors); |
| 2058 | return NULL; |
| 2059 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2060 | if (!DBCursorObject_Check(item)) { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2061 | PyErr_SetString(PyExc_TypeError, |
| 2062 | "Sequence of DBCursor objects expected"); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2063 | free(cursors); |
| 2064 | return NULL; |
| 2065 | } |
| 2066 | cursors[x] = ((DBCursorObject*)item)->dbc; |
Thomas Wouters | b2820ae | 2006-03-12 00:01:38 +0000 | [diff] [blame] | 2067 | Py_DECREF(item); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | MYDB_BEGIN_ALLOW_THREADS; |
| 2071 | err = self->db->join(self->db, cursors, &dbc, flags); |
| 2072 | MYDB_END_ALLOW_THREADS; |
| 2073 | free(cursors); |
| 2074 | RETURN_IF_ERR(); |
| 2075 | |
Gregory P. Smith | 7441e65 | 2003-11-03 21:35:31 +0000 | [diff] [blame] | 2076 | /* FIXME: this is a buggy interface. The returned cursor |
| 2077 | contains internal references to the passed in cursors |
| 2078 | but does not hold python references to them or prevent |
| 2079 | them from being closed prematurely. This can cause |
| 2080 | python to crash when things are done in the wrong order. */ |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 2081 | return (PyObject*) newDBCursorObject(dbc, NULL, self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | |
| 2085 | static PyObject* |
| 2086 | DB_key_range(DBObject* self, PyObject* args, PyObject* kwargs) |
| 2087 | { |
| 2088 | int err, flags=0; |
| 2089 | PyObject* txnobj = NULL; |
| 2090 | PyObject* keyobj; |
| 2091 | DBT key; |
| 2092 | DB_TXN *txn = NULL; |
| 2093 | DB_KEY_RANGE range; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2094 | static char* kwnames[] = { "key", "txn", "flags", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2095 | |
| 2096 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|Oi:key_range", kwnames, |
| 2097 | &keyobj, &txnobj, &flags)) |
| 2098 | return NULL; |
| 2099 | CHECK_DB_NOT_CLOSED(self); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2100 | if (!make_dbt(keyobj, &key)) |
| 2101 | /* BTree only, don't need to allow for an int key */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2102 | return NULL; |
| 2103 | if (!checkTxnObj(txnobj, &txn)) |
| 2104 | return NULL; |
| 2105 | |
| 2106 | MYDB_BEGIN_ALLOW_THREADS; |
| 2107 | err = self->db->key_range(self->db, txn, &key, &range, flags); |
| 2108 | MYDB_END_ALLOW_THREADS; |
| 2109 | |
| 2110 | RETURN_IF_ERR(); |
| 2111 | return Py_BuildValue("ddd", range.less, range.equal, range.greater); |
| 2112 | } |
| 2113 | |
| 2114 | |
| 2115 | static PyObject* |
| 2116 | DB_open(DBObject* self, PyObject* args, PyObject* kwargs) |
| 2117 | { |
| 2118 | int err, type = DB_UNKNOWN, flags=0, mode=0660; |
| 2119 | char* filename = NULL; |
| 2120 | char* dbname = NULL; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2121 | #if (DBVER >= 41) |
| 2122 | PyObject *txnobj = NULL; |
| 2123 | DB_TXN *txn = NULL; |
| 2124 | /* with dbname */ |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2125 | static char* kwnames[] = { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2126 | "filename", "dbname", "dbtype", "flags", "mode", "txn", NULL}; |
| 2127 | /* without dbname */ |
Tim Peters | 85b1052 | 2006-02-28 18:33:35 +0000 | [diff] [blame] | 2128 | static char* kwnames_basic[] = { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2129 | "filename", "dbtype", "flags", "mode", "txn", NULL}; |
| 2130 | #else |
| 2131 | /* with dbname */ |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2132 | static char* kwnames[] = { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2133 | "filename", "dbname", "dbtype", "flags", "mode", NULL}; |
| 2134 | /* without dbname */ |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2135 | static char* kwnames_basic[] = { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2136 | "filename", "dbtype", "flags", "mode", NULL}; |
| 2137 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2138 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2139 | #if (DBVER >= 41) |
| 2140 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "z|ziiiO:open", kwnames, |
| 2141 | &filename, &dbname, &type, &flags, &mode, |
| 2142 | &txnobj)) |
| 2143 | #else |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2144 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "z|ziii:open", kwnames, |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2145 | &filename, &dbname, &type, &flags, |
| 2146 | &mode)) |
| 2147 | #endif |
| 2148 | { |
| 2149 | PyErr_Clear(); |
| 2150 | type = DB_UNKNOWN; flags = 0; mode = 0660; |
| 2151 | filename = NULL; dbname = NULL; |
| 2152 | #if (DBVER >= 41) |
| 2153 | if (!PyArg_ParseTupleAndKeywords(args, kwargs,"z|iiiO:open", |
| 2154 | kwnames_basic, |
| 2155 | &filename, &type, &flags, &mode, |
| 2156 | &txnobj)) |
| 2157 | return NULL; |
| 2158 | #else |
| 2159 | if (!PyArg_ParseTupleAndKeywords(args, kwargs,"z|iii:open", |
| 2160 | kwnames_basic, |
| 2161 | &filename, &type, &flags, &mode)) |
| 2162 | return NULL; |
| 2163 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2166 | #if (DBVER >= 41) |
| 2167 | if (!checkTxnObj(txnobj, &txn)) return NULL; |
| 2168 | #endif |
| 2169 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2170 | if (NULL == self->db) { |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2171 | PyObject *t = Py_BuildValue("(is)", 0, |
| 2172 | "Cannot call open() twice for DB object"); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2173 | if (t) { |
| 2174 | PyErr_SetObject(DBError, t); |
| 2175 | Py_DECREF(t); |
| 2176 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2177 | return NULL; |
| 2178 | } |
| 2179 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 2180 | #if (DBVER >= 41) |
| 2181 | if (txn) { /* Can't use 'txnobj' because could be 'txnobj==Py_None' */ |
| 2182 | INSERT_IN_DOUBLE_LINKED_LIST_TXN(((DBTxnObject *)txnobj)->children_dbs,self); |
| 2183 | self->txn=(DBTxnObject *)txnobj; |
| 2184 | } else { |
| 2185 | self->txn=NULL; |
| 2186 | } |
| 2187 | #else |
| 2188 | self->txn=NULL; |
| 2189 | #endif |
| 2190 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2191 | MYDB_BEGIN_ALLOW_THREADS; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2192 | #if (DBVER >= 41) |
| 2193 | err = self->db->open(self->db, txn, filename, dbname, type, flags, mode); |
| 2194 | #else |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2195 | err = self->db->open(self->db, filename, dbname, type, flags, mode); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2196 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2197 | MYDB_END_ALLOW_THREADS; |
| 2198 | if (makeDBError(err)) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 2199 | PyObject *dummy; |
| 2200 | |
Jesus Cea | 5cd5f12 | 2008-09-23 18:54:08 +0000 | [diff] [blame] | 2201 | dummy=DB_close_internal(self, 0, 0); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 2202 | Py_XDECREF(dummy); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2203 | return NULL; |
| 2204 | } |
| 2205 | |
Gregory P. Smith | fc00669 | 2007-11-05 09:06:28 +0000 | [diff] [blame] | 2206 | #if (DBVER >= 42) |
Gregory P. Smith | ec10a4a | 2007-11-05 02:32:26 +0000 | [diff] [blame] | 2207 | self->db->get_flags(self->db, &self->setflags); |
Gregory P. Smith | fc00669 | 2007-11-05 09:06:28 +0000 | [diff] [blame] | 2208 | #endif |
Gregory P. Smith | ec10a4a | 2007-11-05 02:32:26 +0000 | [diff] [blame] | 2209 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2210 | self->flags = flags; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 2211 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2212 | RETURN_NONE(); |
| 2213 | } |
| 2214 | |
| 2215 | |
| 2216 | static PyObject* |
| 2217 | DB_put(DBObject* self, PyObject* args, PyObject* kwargs) |
| 2218 | { |
| 2219 | int flags=0; |
| 2220 | PyObject* txnobj = NULL; |
| 2221 | int dlen = -1; |
| 2222 | int doff = -1; |
| 2223 | PyObject* keyobj, *dataobj, *retval; |
| 2224 | DBT key, data; |
| 2225 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2226 | static char* kwnames[] = { "key", "data", "txn", "flags", "dlen", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 2227 | "doff", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2228 | |
| 2229 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|Oiii:put", kwnames, |
| 2230 | &keyobj, &dataobj, &txnobj, &flags, &dlen, &doff)) |
| 2231 | return NULL; |
| 2232 | |
| 2233 | CHECK_DB_NOT_CLOSED(self); |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 2234 | if (!make_key_dbt(self, keyobj, &key, NULL)) |
| 2235 | return NULL; |
| 2236 | if ( !make_dbt(dataobj, &data) || |
| 2237 | !add_partial_dbt(&data, dlen, doff) || |
| 2238 | !checkTxnObj(txnobj, &txn) ) |
| 2239 | { |
| 2240 | FREE_DBT(key); |
| 2241 | return NULL; |
| 2242 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2243 | |
| 2244 | if (-1 == _DB_put(self, txn, &key, &data, flags)) { |
| 2245 | FREE_DBT(key); |
| 2246 | return NULL; |
| 2247 | } |
| 2248 | |
| 2249 | if (flags & DB_APPEND) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2250 | retval = NUMBER_FromLong(*((db_recno_t*)key.data)); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2251 | else { |
| 2252 | retval = Py_None; |
| 2253 | Py_INCREF(retval); |
| 2254 | } |
| 2255 | FREE_DBT(key); |
| 2256 | return retval; |
| 2257 | } |
| 2258 | |
| 2259 | |
| 2260 | |
| 2261 | static PyObject* |
| 2262 | DB_remove(DBObject* self, PyObject* args, PyObject* kwargs) |
| 2263 | { |
| 2264 | char* filename; |
| 2265 | char* database = NULL; |
| 2266 | int err, flags=0; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2267 | static char* kwnames[] = { "filename", "dbname", "flags", NULL}; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2268 | |
| 2269 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|zi:remove", kwnames, |
| 2270 | &filename, &database, &flags)) |
| 2271 | return NULL; |
| 2272 | CHECK_DB_NOT_CLOSED(self); |
| 2273 | |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 2274 | EXTRACT_FROM_DOUBLE_LINKED_LIST_MAYBE_NULL(self); |
| 2275 | |
| 2276 | MYDB_BEGIN_ALLOW_THREADS; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2277 | err = self->db->remove(self->db, filename, database, flags); |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 2278 | MYDB_END_ALLOW_THREADS; |
| 2279 | |
Gregory P. Smith | f655dff | 2003-05-15 00:13:18 +0000 | [diff] [blame] | 2280 | self->db = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2281 | RETURN_IF_ERR(); |
| 2282 | RETURN_NONE(); |
| 2283 | } |
| 2284 | |
| 2285 | |
| 2286 | |
| 2287 | static PyObject* |
| 2288 | DB_rename(DBObject* self, PyObject* args) |
| 2289 | { |
| 2290 | char* filename; |
| 2291 | char* database; |
| 2292 | char* newname; |
| 2293 | int err, flags=0; |
| 2294 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2295 | if (!PyArg_ParseTuple(args, "sss|i:rename", &filename, &database, &newname, |
| 2296 | &flags)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2297 | return NULL; |
| 2298 | CHECK_DB_NOT_CLOSED(self); |
| 2299 | |
| 2300 | MYDB_BEGIN_ALLOW_THREADS; |
| 2301 | err = self->db->rename(self->db, filename, database, newname, flags); |
| 2302 | MYDB_END_ALLOW_THREADS; |
| 2303 | RETURN_IF_ERR(); |
| 2304 | RETURN_NONE(); |
| 2305 | } |
| 2306 | |
| 2307 | |
| 2308 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2309 | DB_get_private(DBObject* self) |
| 2310 | { |
| 2311 | /* We can give out the private field even if db is closed */ |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 2312 | Py_INCREF(self->private_obj); |
| 2313 | return self->private_obj; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2314 | } |
| 2315 | |
| 2316 | static PyObject* |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 2317 | DB_set_private(DBObject* self, PyObject* private_obj) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2318 | { |
| 2319 | /* We can set the private field even if db is closed */ |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 2320 | Py_DECREF(self->private_obj); |
| 2321 | Py_INCREF(private_obj); |
| 2322 | self->private_obj = private_obj; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2323 | RETURN_NONE(); |
| 2324 | } |
| 2325 | |
| 2326 | |
| 2327 | static PyObject* |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2328 | DB_set_bt_minkey(DBObject* self, PyObject* args) |
| 2329 | { |
| 2330 | int err, minkey; |
| 2331 | |
| 2332 | if (!PyArg_ParseTuple(args,"i:set_bt_minkey", &minkey )) |
| 2333 | return NULL; |
| 2334 | CHECK_DB_NOT_CLOSED(self); |
| 2335 | |
| 2336 | MYDB_BEGIN_ALLOW_THREADS; |
| 2337 | err = self->db->set_bt_minkey(self->db, minkey); |
| 2338 | MYDB_END_ALLOW_THREADS; |
| 2339 | RETURN_IF_ERR(); |
| 2340 | RETURN_NONE(); |
| 2341 | } |
| 2342 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2343 | static int |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2344 | _default_cmp(const DBT *leftKey, |
| 2345 | const DBT *rightKey) |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2346 | { |
| 2347 | int res; |
| 2348 | int lsize = leftKey->size, rsize = rightKey->size; |
| 2349 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2350 | res = memcmp(leftKey->data, rightKey->data, |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2351 | lsize < rsize ? lsize : rsize); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2352 | |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2353 | if (res == 0) { |
| 2354 | if (lsize < rsize) { |
| 2355 | res = -1; |
| 2356 | } |
| 2357 | else if (lsize > rsize) { |
| 2358 | res = 1; |
| 2359 | } |
| 2360 | } |
| 2361 | return res; |
| 2362 | } |
| 2363 | |
| 2364 | static int |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 2365 | _db_compareCallback(DB* db, |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2366 | const DBT *leftKey, |
| 2367 | const DBT *rightKey) |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2368 | { |
| 2369 | int res = 0; |
| 2370 | PyObject *args; |
Thomas Wouters | b2820ae | 2006-03-12 00:01:38 +0000 | [diff] [blame] | 2371 | PyObject *result = NULL; |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2372 | DBObject *self = (DBObject *)db->app_private; |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2373 | |
| 2374 | if (self == NULL || self->btCompareCallback == NULL) { |
| 2375 | MYDB_BEGIN_BLOCK_THREADS; |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2376 | PyErr_SetString(PyExc_TypeError, |
| 2377 | (self == 0 |
| 2378 | ? "DB_bt_compare db is NULL." |
| 2379 | : "DB_bt_compare callback is NULL.")); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2380 | /* we're in a callback within the DB code, we can't raise */ |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2381 | PyErr_Print(); |
| 2382 | res = _default_cmp(leftKey, rightKey); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2383 | MYDB_END_BLOCK_THREADS; |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2384 | } else { |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2385 | MYDB_BEGIN_BLOCK_THREADS; |
| 2386 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 2387 | args = BuildValue_SS(leftKey->data, leftKey->size, rightKey->data, rightKey->size); |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2388 | if (args != NULL) { |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2389 | result = PyEval_CallObject(self->btCompareCallback, args); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2390 | } |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2391 | if (args == NULL || result == NULL) { |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2392 | /* we're in a callback within the DB code, we can't raise */ |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2393 | PyErr_Print(); |
| 2394 | res = _default_cmp(leftKey, rightKey); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2395 | } else if (NUMBER_Check(result)) { |
| 2396 | res = NUMBER_AsLong(result); |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2397 | } else { |
| 2398 | PyErr_SetString(PyExc_TypeError, |
| 2399 | "DB_bt_compare callback MUST return an int."); |
| 2400 | /* we're in a callback within the DB code, we can't raise */ |
| 2401 | PyErr_Print(); |
| 2402 | res = _default_cmp(leftKey, rightKey); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2403 | } |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2404 | |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2405 | Py_XDECREF(args); |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2406 | Py_XDECREF(result); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2407 | |
| 2408 | MYDB_END_BLOCK_THREADS; |
| 2409 | } |
| 2410 | return res; |
| 2411 | } |
| 2412 | |
| 2413 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2414 | DB_set_bt_compare(DBObject* self, PyObject* comparator) |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2415 | { |
| 2416 | int err; |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2417 | PyObject *tuple, *result; |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2418 | |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2419 | CHECK_DB_NOT_CLOSED(self); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2420 | |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2421 | if (!PyCallable_Check(comparator)) { |
| 2422 | makeTypeError("Callable", comparator); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2423 | return NULL; |
| 2424 | } |
| 2425 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2426 | /* |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2427 | * Perform a test call of the comparator function with two empty |
| 2428 | * string objects here. verify that it returns an int (0). |
| 2429 | * err if not. |
| 2430 | */ |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2431 | tuple = Py_BuildValue("(ss)", "", ""); |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2432 | result = PyEval_CallObject(comparator, tuple); |
| 2433 | Py_DECREF(tuple); |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2434 | if (result == NULL) |
| 2435 | return NULL; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2436 | if (!NUMBER_Check(result)) { |
Florent Xicluna | e7901c5 | 2010-03-01 20:45:01 +0000 | [diff] [blame] | 2437 | Py_DECREF(result); |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2438 | PyErr_SetString(PyExc_TypeError, |
| 2439 | "callback MUST return an int"); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2440 | return NULL; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2441 | } else if (NUMBER_AsLong(result) != 0) { |
Florent Xicluna | e7901c5 | 2010-03-01 20:45:01 +0000 | [diff] [blame] | 2442 | Py_DECREF(result); |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2443 | PyErr_SetString(PyExc_TypeError, |
| 2444 | "callback failed to return 0 on two empty strings"); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2445 | return NULL; |
| 2446 | } |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2447 | Py_DECREF(result); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2448 | |
| 2449 | /* We don't accept multiple set_bt_compare operations, in order to |
| 2450 | * simplify the code. This would have no real use, as one cannot |
| 2451 | * change the function once the db is opened anyway */ |
| 2452 | if (self->btCompareCallback != NULL) { |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2453 | PyErr_SetString(PyExc_RuntimeError, "set_bt_compare() cannot be called more than once"); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2454 | return NULL; |
| 2455 | } |
| 2456 | |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2457 | Py_INCREF(comparator); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2458 | self->btCompareCallback = comparator; |
| 2459 | |
| 2460 | /* This is to workaround a problem with un-initialized threads (see |
| 2461 | comment in DB_associate) */ |
| 2462 | #ifdef WITH_THREAD |
| 2463 | PyEval_InitThreads(); |
| 2464 | #endif |
| 2465 | |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2466 | err = self->db->set_bt_compare(self->db, _db_compareCallback); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2467 | |
| 2468 | if (err) { |
| 2469 | /* restore the old state in case of error */ |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2470 | Py_DECREF(comparator); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2471 | self->btCompareCallback = NULL; |
| 2472 | } |
| 2473 | |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2474 | RETURN_IF_ERR(); |
| 2475 | RETURN_NONE(); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2478 | |
| 2479 | static PyObject* |
| 2480 | DB_set_cachesize(DBObject* self, PyObject* args) |
| 2481 | { |
| 2482 | int err; |
| 2483 | int gbytes = 0, bytes = 0, ncache = 0; |
| 2484 | |
| 2485 | if (!PyArg_ParseTuple(args,"ii|i:set_cachesize", |
| 2486 | &gbytes,&bytes,&ncache)) |
| 2487 | return NULL; |
| 2488 | CHECK_DB_NOT_CLOSED(self); |
| 2489 | |
| 2490 | MYDB_BEGIN_ALLOW_THREADS; |
| 2491 | err = self->db->set_cachesize(self->db, gbytes, bytes, ncache); |
| 2492 | MYDB_END_ALLOW_THREADS; |
| 2493 | RETURN_IF_ERR(); |
| 2494 | RETURN_NONE(); |
| 2495 | } |
| 2496 | |
| 2497 | |
| 2498 | static PyObject* |
| 2499 | DB_set_flags(DBObject* self, PyObject* args) |
| 2500 | { |
| 2501 | int err, flags; |
| 2502 | |
| 2503 | if (!PyArg_ParseTuple(args,"i:set_flags", &flags)) |
| 2504 | return NULL; |
| 2505 | CHECK_DB_NOT_CLOSED(self); |
| 2506 | |
| 2507 | MYDB_BEGIN_ALLOW_THREADS; |
| 2508 | err = self->db->set_flags(self->db, flags); |
| 2509 | MYDB_END_ALLOW_THREADS; |
| 2510 | RETURN_IF_ERR(); |
| 2511 | |
| 2512 | self->setflags |= flags; |
| 2513 | RETURN_NONE(); |
| 2514 | } |
| 2515 | |
| 2516 | |
| 2517 | static PyObject* |
| 2518 | DB_set_h_ffactor(DBObject* self, PyObject* args) |
| 2519 | { |
| 2520 | int err, ffactor; |
| 2521 | |
| 2522 | if (!PyArg_ParseTuple(args,"i:set_h_ffactor", &ffactor)) |
| 2523 | return NULL; |
| 2524 | CHECK_DB_NOT_CLOSED(self); |
| 2525 | |
| 2526 | MYDB_BEGIN_ALLOW_THREADS; |
| 2527 | err = self->db->set_h_ffactor(self->db, ffactor); |
| 2528 | MYDB_END_ALLOW_THREADS; |
| 2529 | RETURN_IF_ERR(); |
| 2530 | RETURN_NONE(); |
| 2531 | } |
| 2532 | |
| 2533 | |
| 2534 | static PyObject* |
| 2535 | DB_set_h_nelem(DBObject* self, PyObject* args) |
| 2536 | { |
| 2537 | int err, nelem; |
| 2538 | |
| 2539 | if (!PyArg_ParseTuple(args,"i:set_h_nelem", &nelem)) |
| 2540 | return NULL; |
| 2541 | CHECK_DB_NOT_CLOSED(self); |
| 2542 | |
| 2543 | MYDB_BEGIN_ALLOW_THREADS; |
| 2544 | err = self->db->set_h_nelem(self->db, nelem); |
| 2545 | MYDB_END_ALLOW_THREADS; |
| 2546 | RETURN_IF_ERR(); |
| 2547 | RETURN_NONE(); |
| 2548 | } |
| 2549 | |
| 2550 | |
| 2551 | static PyObject* |
| 2552 | DB_set_lorder(DBObject* self, PyObject* args) |
| 2553 | { |
| 2554 | int err, lorder; |
| 2555 | |
| 2556 | if (!PyArg_ParseTuple(args,"i:set_lorder", &lorder)) |
| 2557 | return NULL; |
| 2558 | CHECK_DB_NOT_CLOSED(self); |
| 2559 | |
| 2560 | MYDB_BEGIN_ALLOW_THREADS; |
| 2561 | err = self->db->set_lorder(self->db, lorder); |
| 2562 | MYDB_END_ALLOW_THREADS; |
| 2563 | RETURN_IF_ERR(); |
| 2564 | RETURN_NONE(); |
| 2565 | } |
| 2566 | |
| 2567 | |
| 2568 | static PyObject* |
| 2569 | DB_set_pagesize(DBObject* self, PyObject* args) |
| 2570 | { |
| 2571 | int err, pagesize; |
| 2572 | |
| 2573 | if (!PyArg_ParseTuple(args,"i:set_pagesize", &pagesize)) |
| 2574 | return NULL; |
| 2575 | CHECK_DB_NOT_CLOSED(self); |
| 2576 | |
| 2577 | MYDB_BEGIN_ALLOW_THREADS; |
| 2578 | err = self->db->set_pagesize(self->db, pagesize); |
| 2579 | MYDB_END_ALLOW_THREADS; |
| 2580 | RETURN_IF_ERR(); |
| 2581 | RETURN_NONE(); |
| 2582 | } |
| 2583 | |
| 2584 | |
| 2585 | static PyObject* |
| 2586 | DB_set_re_delim(DBObject* self, PyObject* args) |
| 2587 | { |
| 2588 | int err; |
| 2589 | char delim; |
| 2590 | |
| 2591 | if (!PyArg_ParseTuple(args,"b:set_re_delim", &delim)) { |
| 2592 | PyErr_Clear(); |
| 2593 | if (!PyArg_ParseTuple(args,"c:set_re_delim", &delim)) |
| 2594 | return NULL; |
| 2595 | } |
| 2596 | |
| 2597 | CHECK_DB_NOT_CLOSED(self); |
| 2598 | |
| 2599 | MYDB_BEGIN_ALLOW_THREADS; |
| 2600 | err = self->db->set_re_delim(self->db, delim); |
| 2601 | MYDB_END_ALLOW_THREADS; |
| 2602 | RETURN_IF_ERR(); |
| 2603 | RETURN_NONE(); |
| 2604 | } |
| 2605 | |
| 2606 | static PyObject* |
| 2607 | DB_set_re_len(DBObject* self, PyObject* args) |
| 2608 | { |
| 2609 | int err, len; |
| 2610 | |
| 2611 | if (!PyArg_ParseTuple(args,"i:set_re_len", &len)) |
| 2612 | return NULL; |
| 2613 | CHECK_DB_NOT_CLOSED(self); |
| 2614 | |
| 2615 | MYDB_BEGIN_ALLOW_THREADS; |
| 2616 | err = self->db->set_re_len(self->db, len); |
| 2617 | MYDB_END_ALLOW_THREADS; |
| 2618 | RETURN_IF_ERR(); |
| 2619 | RETURN_NONE(); |
| 2620 | } |
| 2621 | |
| 2622 | |
| 2623 | static PyObject* |
| 2624 | DB_set_re_pad(DBObject* self, PyObject* args) |
| 2625 | { |
| 2626 | int err; |
| 2627 | char pad; |
| 2628 | |
| 2629 | if (!PyArg_ParseTuple(args,"b:set_re_pad", &pad)) { |
| 2630 | PyErr_Clear(); |
| 2631 | if (!PyArg_ParseTuple(args,"c:set_re_pad", &pad)) |
| 2632 | return NULL; |
| 2633 | } |
| 2634 | CHECK_DB_NOT_CLOSED(self); |
| 2635 | |
| 2636 | MYDB_BEGIN_ALLOW_THREADS; |
| 2637 | err = self->db->set_re_pad(self->db, pad); |
| 2638 | MYDB_END_ALLOW_THREADS; |
| 2639 | RETURN_IF_ERR(); |
| 2640 | RETURN_NONE(); |
| 2641 | } |
| 2642 | |
| 2643 | |
| 2644 | static PyObject* |
| 2645 | DB_set_re_source(DBObject* self, PyObject* args) |
| 2646 | { |
| 2647 | int err; |
| 2648 | char *re_source; |
| 2649 | |
| 2650 | if (!PyArg_ParseTuple(args,"s:set_re_source", &re_source)) |
| 2651 | return NULL; |
| 2652 | CHECK_DB_NOT_CLOSED(self); |
| 2653 | |
| 2654 | MYDB_BEGIN_ALLOW_THREADS; |
| 2655 | err = self->db->set_re_source(self->db, re_source); |
| 2656 | MYDB_END_ALLOW_THREADS; |
| 2657 | RETURN_IF_ERR(); |
| 2658 | RETURN_NONE(); |
| 2659 | } |
| 2660 | |
| 2661 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2662 | static PyObject* |
| 2663 | DB_set_q_extentsize(DBObject* self, PyObject* args) |
| 2664 | { |
| 2665 | int err; |
| 2666 | int extentsize; |
| 2667 | |
| 2668 | if (!PyArg_ParseTuple(args,"i:set_q_extentsize", &extentsize)) |
| 2669 | return NULL; |
| 2670 | CHECK_DB_NOT_CLOSED(self); |
| 2671 | |
| 2672 | MYDB_BEGIN_ALLOW_THREADS; |
| 2673 | err = self->db->set_q_extentsize(self->db, extentsize); |
| 2674 | MYDB_END_ALLOW_THREADS; |
| 2675 | RETURN_IF_ERR(); |
| 2676 | RETURN_NONE(); |
| 2677 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2678 | |
| 2679 | static PyObject* |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 2680 | DB_stat(DBObject* self, PyObject* args, PyObject* kwargs) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2681 | { |
| 2682 | int err, flags = 0, type; |
| 2683 | void* sp; |
| 2684 | PyObject* d; |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 2685 | #if (DBVER >= 43) |
| 2686 | PyObject* txnobj = NULL; |
| 2687 | DB_TXN *txn = NULL; |
Gregory P. Smith | 2fa0679 | 2006-09-19 17:35:04 +0000 | [diff] [blame] | 2688 | static char* kwnames[] = { "flags", "txn", NULL }; |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 2689 | #else |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2690 | static char* kwnames[] = { "flags", NULL }; |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 2691 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2692 | |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 2693 | #if (DBVER >= 43) |
| 2694 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iO:stat", kwnames, |
| 2695 | &flags, &txnobj)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2696 | return NULL; |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 2697 | if (!checkTxnObj(txnobj, &txn)) |
| 2698 | return NULL; |
| 2699 | #else |
| 2700 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:stat", kwnames, &flags)) |
| 2701 | return NULL; |
| 2702 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2703 | CHECK_DB_NOT_CLOSED(self); |
| 2704 | |
| 2705 | MYDB_BEGIN_ALLOW_THREADS; |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 2706 | #if (DBVER >= 43) |
| 2707 | err = self->db->stat(self->db, txn, &sp, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2708 | #else |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2709 | err = self->db->stat(self->db, &sp, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2710 | #endif |
| 2711 | MYDB_END_ALLOW_THREADS; |
| 2712 | RETURN_IF_ERR(); |
| 2713 | |
| 2714 | self->haveStat = 1; |
| 2715 | |
| 2716 | /* Turn the stat structure into a dictionary */ |
| 2717 | type = _DB_get_type(self); |
| 2718 | if ((type == -1) || ((d = PyDict_New()) == NULL)) { |
| 2719 | free(sp); |
| 2720 | return NULL; |
| 2721 | } |
| 2722 | |
| 2723 | #define MAKE_HASH_ENTRY(name) _addIntToDict(d, #name, ((DB_HASH_STAT*)sp)->hash_##name) |
| 2724 | #define MAKE_BT_ENTRY(name) _addIntToDict(d, #name, ((DB_BTREE_STAT*)sp)->bt_##name) |
| 2725 | #define MAKE_QUEUE_ENTRY(name) _addIntToDict(d, #name, ((DB_QUEUE_STAT*)sp)->qs_##name) |
| 2726 | |
| 2727 | switch (type) { |
| 2728 | case DB_HASH: |
| 2729 | MAKE_HASH_ENTRY(magic); |
| 2730 | MAKE_HASH_ENTRY(version); |
| 2731 | MAKE_HASH_ENTRY(nkeys); |
| 2732 | MAKE_HASH_ENTRY(ndata); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 2733 | #if (DBVER >= 46) |
| 2734 | MAKE_HASH_ENTRY(pagecnt); |
| 2735 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2736 | MAKE_HASH_ENTRY(pagesize); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2737 | #if (DBVER < 41) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2738 | MAKE_HASH_ENTRY(nelem); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2739 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2740 | MAKE_HASH_ENTRY(ffactor); |
| 2741 | MAKE_HASH_ENTRY(buckets); |
| 2742 | MAKE_HASH_ENTRY(free); |
| 2743 | MAKE_HASH_ENTRY(bfree); |
| 2744 | MAKE_HASH_ENTRY(bigpages); |
| 2745 | MAKE_HASH_ENTRY(big_bfree); |
| 2746 | MAKE_HASH_ENTRY(overflows); |
| 2747 | MAKE_HASH_ENTRY(ovfl_free); |
| 2748 | MAKE_HASH_ENTRY(dup); |
| 2749 | MAKE_HASH_ENTRY(dup_free); |
| 2750 | break; |
| 2751 | |
| 2752 | case DB_BTREE: |
| 2753 | case DB_RECNO: |
| 2754 | MAKE_BT_ENTRY(magic); |
| 2755 | MAKE_BT_ENTRY(version); |
| 2756 | MAKE_BT_ENTRY(nkeys); |
| 2757 | MAKE_BT_ENTRY(ndata); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 2758 | #if (DBVER >= 46) |
| 2759 | MAKE_BT_ENTRY(pagecnt); |
| 2760 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2761 | MAKE_BT_ENTRY(pagesize); |
| 2762 | MAKE_BT_ENTRY(minkey); |
| 2763 | MAKE_BT_ENTRY(re_len); |
| 2764 | MAKE_BT_ENTRY(re_pad); |
| 2765 | MAKE_BT_ENTRY(levels); |
| 2766 | MAKE_BT_ENTRY(int_pg); |
| 2767 | MAKE_BT_ENTRY(leaf_pg); |
| 2768 | MAKE_BT_ENTRY(dup_pg); |
| 2769 | MAKE_BT_ENTRY(over_pg); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 2770 | #if (DBVER >= 43) |
| 2771 | MAKE_BT_ENTRY(empty_pg); |
| 2772 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2773 | MAKE_BT_ENTRY(free); |
| 2774 | MAKE_BT_ENTRY(int_pgfree); |
| 2775 | MAKE_BT_ENTRY(leaf_pgfree); |
| 2776 | MAKE_BT_ENTRY(dup_pgfree); |
| 2777 | MAKE_BT_ENTRY(over_pgfree); |
| 2778 | break; |
| 2779 | |
| 2780 | case DB_QUEUE: |
| 2781 | MAKE_QUEUE_ENTRY(magic); |
| 2782 | MAKE_QUEUE_ENTRY(version); |
| 2783 | MAKE_QUEUE_ENTRY(nkeys); |
| 2784 | MAKE_QUEUE_ENTRY(ndata); |
| 2785 | MAKE_QUEUE_ENTRY(pagesize); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2786 | #if (DBVER >= 41) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 2787 | MAKE_QUEUE_ENTRY(extentsize); |
| 2788 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2789 | MAKE_QUEUE_ENTRY(pages); |
| 2790 | MAKE_QUEUE_ENTRY(re_len); |
| 2791 | MAKE_QUEUE_ENTRY(re_pad); |
| 2792 | MAKE_QUEUE_ENTRY(pgfree); |
| 2793 | #if (DBVER == 31) |
| 2794 | MAKE_QUEUE_ENTRY(start); |
| 2795 | #endif |
| 2796 | MAKE_QUEUE_ENTRY(first_recno); |
| 2797 | MAKE_QUEUE_ENTRY(cur_recno); |
| 2798 | break; |
| 2799 | |
| 2800 | default: |
| 2801 | PyErr_SetString(PyExc_TypeError, "Unknown DB type, unable to stat"); |
| 2802 | Py_DECREF(d); |
| 2803 | d = NULL; |
| 2804 | } |
| 2805 | |
| 2806 | #undef MAKE_HASH_ENTRY |
| 2807 | #undef MAKE_BT_ENTRY |
| 2808 | #undef MAKE_QUEUE_ENTRY |
| 2809 | |
| 2810 | free(sp); |
| 2811 | return d; |
| 2812 | } |
| 2813 | |
| 2814 | static PyObject* |
| 2815 | DB_sync(DBObject* self, PyObject* args) |
| 2816 | { |
| 2817 | int err; |
| 2818 | int flags = 0; |
| 2819 | |
| 2820 | if (!PyArg_ParseTuple(args,"|i:sync", &flags )) |
| 2821 | return NULL; |
| 2822 | CHECK_DB_NOT_CLOSED(self); |
| 2823 | |
| 2824 | MYDB_BEGIN_ALLOW_THREADS; |
| 2825 | err = self->db->sync(self->db, flags); |
| 2826 | MYDB_END_ALLOW_THREADS; |
| 2827 | RETURN_IF_ERR(); |
| 2828 | RETURN_NONE(); |
| 2829 | } |
| 2830 | |
| 2831 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2832 | static PyObject* |
| 2833 | DB_truncate(DBObject* self, PyObject* args, PyObject* kwargs) |
| 2834 | { |
| 2835 | int err, flags=0; |
| 2836 | u_int32_t count=0; |
| 2837 | PyObject* txnobj = NULL; |
| 2838 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2839 | static char* kwnames[] = { "txn", "flags", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2840 | |
| 2841 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oi:cursor", kwnames, |
| 2842 | &txnobj, &flags)) |
| 2843 | return NULL; |
| 2844 | CHECK_DB_NOT_CLOSED(self); |
| 2845 | if (!checkTxnObj(txnobj, &txn)) |
| 2846 | return NULL; |
| 2847 | |
| 2848 | MYDB_BEGIN_ALLOW_THREADS; |
| 2849 | err = self->db->truncate(self->db, txn, &count, flags); |
| 2850 | MYDB_END_ALLOW_THREADS; |
| 2851 | RETURN_IF_ERR(); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2852 | return NUMBER_FromLong(count); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2853 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2854 | |
| 2855 | |
| 2856 | static PyObject* |
| 2857 | DB_upgrade(DBObject* self, PyObject* args) |
| 2858 | { |
| 2859 | int err, flags=0; |
| 2860 | char *filename; |
| 2861 | |
| 2862 | if (!PyArg_ParseTuple(args,"s|i:upgrade", &filename, &flags)) |
| 2863 | return NULL; |
| 2864 | CHECK_DB_NOT_CLOSED(self); |
| 2865 | |
| 2866 | MYDB_BEGIN_ALLOW_THREADS; |
| 2867 | err = self->db->upgrade(self->db, filename, flags); |
| 2868 | MYDB_END_ALLOW_THREADS; |
| 2869 | RETURN_IF_ERR(); |
| 2870 | RETURN_NONE(); |
| 2871 | } |
| 2872 | |
| 2873 | |
| 2874 | static PyObject* |
| 2875 | DB_verify(DBObject* self, PyObject* args, PyObject* kwargs) |
| 2876 | { |
| 2877 | int err, flags=0; |
| 2878 | char* fileName; |
| 2879 | char* dbName=NULL; |
| 2880 | char* outFileName=NULL; |
| 2881 | FILE* outFile=NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2882 | static char* kwnames[] = { "filename", "dbname", "outfile", "flags", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 2883 | NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2884 | |
| 2885 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|zzi:verify", kwnames, |
| 2886 | &fileName, &dbName, &outFileName, &flags)) |
| 2887 | return NULL; |
| 2888 | |
| 2889 | CHECK_DB_NOT_CLOSED(self); |
| 2890 | if (outFileName) |
| 2891 | outFile = fopen(outFileName, "w"); |
Neal Norwitz | 5aa9689 | 2006-08-13 18:11:43 +0000 | [diff] [blame] | 2892 | /* XXX(nnorwitz): it should probably be an exception if outFile |
| 2893 | can't be opened. */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2894 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 2895 | { /* DB.verify acts as a DB handle destructor (like close) */ |
| 2896 | PyObject *error; |
| 2897 | |
Jesus Cea | 5cd5f12 | 2008-09-23 18:54:08 +0000 | [diff] [blame] | 2898 | error=DB_close_internal(self, 0, 1); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 2899 | if (error ) { |
| 2900 | return error; |
| 2901 | } |
| 2902 | } |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 2903 | |
Jesus Cea | 5cd5f12 | 2008-09-23 18:54:08 +0000 | [diff] [blame] | 2904 | MYDB_BEGIN_ALLOW_THREADS; |
| 2905 | err = self->db->verify(self->db, fileName, dbName, outFile, flags); |
| 2906 | MYDB_END_ALLOW_THREADS; |
| 2907 | |
| 2908 | self->db = NULL; /* Implicit close; related objects already released */ |
| 2909 | |
| 2910 | if (outFile) |
| 2911 | fclose(outFile); |
| 2912 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2913 | RETURN_IF_ERR(); |
| 2914 | RETURN_NONE(); |
| 2915 | } |
| 2916 | |
| 2917 | |
| 2918 | static PyObject* |
| 2919 | DB_set_get_returns_none(DBObject* self, PyObject* args) |
| 2920 | { |
| 2921 | int flags=0; |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 2922 | int oldValue=0; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2923 | |
| 2924 | if (!PyArg_ParseTuple(args,"i:set_get_returns_none", &flags)) |
| 2925 | return NULL; |
| 2926 | CHECK_DB_NOT_CLOSED(self); |
| 2927 | |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 2928 | if (self->moduleFlags.getReturnsNone) |
| 2929 | ++oldValue; |
| 2930 | if (self->moduleFlags.cursorSetReturnsNone) |
| 2931 | ++oldValue; |
| 2932 | self->moduleFlags.getReturnsNone = (flags >= 1); |
| 2933 | self->moduleFlags.cursorSetReturnsNone = (flags >= 2); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2934 | return NUMBER_FromLong(oldValue); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2935 | } |
| 2936 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2937 | #if (DBVER >= 41) |
| 2938 | static PyObject* |
| 2939 | DB_set_encrypt(DBObject* self, PyObject* args, PyObject* kwargs) |
| 2940 | { |
| 2941 | int err; |
| 2942 | u_int32_t flags=0; |
| 2943 | char *passwd = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2944 | static char* kwnames[] = { "passwd", "flags", NULL }; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2945 | |
| 2946 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|i:set_encrypt", kwnames, |
| 2947 | &passwd, &flags)) { |
| 2948 | return NULL; |
| 2949 | } |
| 2950 | |
| 2951 | MYDB_BEGIN_ALLOW_THREADS; |
| 2952 | err = self->db->set_encrypt(self->db, passwd, flags); |
| 2953 | MYDB_END_ALLOW_THREADS; |
| 2954 | |
| 2955 | RETURN_IF_ERR(); |
| 2956 | RETURN_NONE(); |
| 2957 | } |
| 2958 | #endif /* DBVER >= 41 */ |
| 2959 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2960 | |
| 2961 | /*-------------------------------------------------------------- */ |
| 2962 | /* Mapping and Dictionary-like access routines */ |
| 2963 | |
Martin v. Löwis | 70ee3cc | 2006-06-12 04:26:31 +0000 | [diff] [blame] | 2964 | Py_ssize_t DB_length(PyObject* _self) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2965 | { |
| 2966 | int err; |
Gregory P. Smith | 3c228b1 | 2006-06-05 23:59:37 +0000 | [diff] [blame] | 2967 | Py_ssize_t size = 0; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2968 | int flags = 0; |
| 2969 | void* sp; |
Martin v. Löwis | 70ee3cc | 2006-06-12 04:26:31 +0000 | [diff] [blame] | 2970 | DBObject* self = (DBObject*)_self; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2971 | |
| 2972 | if (self->db == NULL) { |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2973 | PyObject *t = Py_BuildValue("(is)", 0, "DB object has been closed"); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2974 | if (t) { |
| 2975 | PyErr_SetObject(DBError, t); |
| 2976 | Py_DECREF(t); |
| 2977 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2978 | return -1; |
| 2979 | } |
| 2980 | |
| 2981 | if (self->haveStat) { /* Has the stat function been called recently? If |
| 2982 | so, we can use the cached value. */ |
Gregory P. Smith | 3c228b1 | 2006-06-05 23:59:37 +0000 | [diff] [blame] | 2983 | flags = DB_FAST_STAT; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2984 | } |
| 2985 | |
| 2986 | MYDB_BEGIN_ALLOW_THREADS; |
Gregory P. Smith | 3c228b1 | 2006-06-05 23:59:37 +0000 | [diff] [blame] | 2987 | redo_stat_for_length: |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 2988 | #if (DBVER >= 43) |
| 2989 | err = self->db->stat(self->db, /*txnid*/ NULL, &sp, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2990 | #else |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 2991 | err = self->db->stat(self->db, &sp, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2992 | #endif |
Gregory P. Smith | 3c228b1 | 2006-06-05 23:59:37 +0000 | [diff] [blame] | 2993 | |
| 2994 | /* All the stat structures have matching fields upto the ndata field, |
| 2995 | so we can use any of them for the type cast */ |
| 2996 | size = ((DB_BTREE_STAT*)sp)->bt_ndata; |
| 2997 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 2998 | /* A size of 0 could mean that Berkeley DB no longer had the stat values cached. |
Gregory P. Smith | 3c228b1 | 2006-06-05 23:59:37 +0000 | [diff] [blame] | 2999 | * redo a full stat to make sure. |
| 3000 | * Fixes SF python bug 1493322, pybsddb bug 1184012 |
| 3001 | */ |
| 3002 | if (size == 0 && (flags & DB_FAST_STAT)) { |
| 3003 | flags = 0; |
Neal Norwitz | e75cad6 | 2006-06-17 22:38:15 +0000 | [diff] [blame] | 3004 | if (!err) |
| 3005 | free(sp); |
Gregory P. Smith | 3c228b1 | 2006-06-05 23:59:37 +0000 | [diff] [blame] | 3006 | goto redo_stat_for_length; |
| 3007 | } |
| 3008 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3009 | MYDB_END_ALLOW_THREADS; |
| 3010 | |
| 3011 | if (err) |
| 3012 | return -1; |
| 3013 | |
| 3014 | self->haveStat = 1; |
| 3015 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3016 | free(sp); |
| 3017 | return size; |
| 3018 | } |
| 3019 | |
| 3020 | |
| 3021 | PyObject* DB_subscript(DBObject* self, PyObject* keyobj) |
| 3022 | { |
| 3023 | int err; |
| 3024 | PyObject* retval; |
| 3025 | DBT key; |
| 3026 | DBT data; |
| 3027 | |
| 3028 | CHECK_DB_NOT_CLOSED(self); |
| 3029 | if (!make_key_dbt(self, keyobj, &key, NULL)) |
| 3030 | return NULL; |
| 3031 | |
| 3032 | CLEAR_DBT(data); |
| 3033 | if (CHECK_DBFLAG(self, DB_THREAD)) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3034 | /* Tell Berkeley DB to malloc the return value (thread safe) */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3035 | data.flags = DB_DBT_MALLOC; |
| 3036 | } |
| 3037 | MYDB_BEGIN_ALLOW_THREADS; |
| 3038 | err = self->db->get(self->db, NULL, &key, &data, 0); |
| 3039 | MYDB_END_ALLOW_THREADS; |
| 3040 | if (err == DB_NOTFOUND || err == DB_KEYEMPTY) { |
| 3041 | PyErr_SetObject(PyExc_KeyError, keyobj); |
| 3042 | retval = NULL; |
| 3043 | } |
| 3044 | else if (makeDBError(err)) { |
| 3045 | retval = NULL; |
| 3046 | } |
| 3047 | else { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3048 | retval = Build_PyString(data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3049 | FREE_DBT(data); |
| 3050 | } |
| 3051 | |
| 3052 | FREE_DBT(key); |
| 3053 | return retval; |
| 3054 | } |
| 3055 | |
| 3056 | |
| 3057 | static int |
| 3058 | DB_ass_sub(DBObject* self, PyObject* keyobj, PyObject* dataobj) |
| 3059 | { |
| 3060 | DBT key, data; |
| 3061 | int retval; |
| 3062 | int flags = 0; |
| 3063 | |
| 3064 | if (self->db == NULL) { |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 3065 | PyObject *t = Py_BuildValue("(is)", 0, "DB object has been closed"); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 3066 | if (t) { |
| 3067 | PyErr_SetObject(DBError, t); |
| 3068 | Py_DECREF(t); |
| 3069 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3070 | return -1; |
| 3071 | } |
| 3072 | |
| 3073 | if (!make_key_dbt(self, keyobj, &key, NULL)) |
| 3074 | return -1; |
| 3075 | |
| 3076 | if (dataobj != NULL) { |
| 3077 | if (!make_dbt(dataobj, &data)) |
| 3078 | retval = -1; |
| 3079 | else { |
| 3080 | if (self->setflags & (DB_DUP|DB_DUPSORT)) |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 3081 | /* dictionaries shouldn't have duplicate keys */ |
| 3082 | flags = DB_NOOVERWRITE; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3083 | retval = _DB_put(self, NULL, &key, &data, flags); |
| 3084 | |
| 3085 | if ((retval == -1) && (self->setflags & (DB_DUP|DB_DUPSORT))) { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 3086 | /* try deleting any old record that matches and then PUT it |
| 3087 | * again... */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3088 | _DB_delete(self, NULL, &key, 0); |
| 3089 | PyErr_Clear(); |
| 3090 | retval = _DB_put(self, NULL, &key, &data, flags); |
| 3091 | } |
| 3092 | } |
| 3093 | } |
| 3094 | else { |
| 3095 | /* dataobj == NULL, so delete the key */ |
| 3096 | retval = _DB_delete(self, NULL, &key, 0); |
| 3097 | } |
| 3098 | FREE_DBT(key); |
| 3099 | return retval; |
| 3100 | } |
| 3101 | |
| 3102 | |
| 3103 | static PyObject* |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 3104 | DB_has_key(DBObject* self, PyObject* args, PyObject* kwargs) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3105 | { |
| 3106 | int err; |
| 3107 | PyObject* keyobj; |
| 3108 | DBT key, data; |
| 3109 | PyObject* txnobj = NULL; |
| 3110 | DB_TXN *txn = NULL; |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 3111 | static char* kwnames[] = {"key","txn", NULL}; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3112 | |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 3113 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:has_key", kwnames, |
| 3114 | &keyobj, &txnobj)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3115 | return NULL; |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 3116 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3117 | CHECK_DB_NOT_CLOSED(self); |
| 3118 | if (!make_key_dbt(self, keyobj, &key, NULL)) |
| 3119 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3120 | if (!checkTxnObj(txnobj, &txn)) { |
| 3121 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3122 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3123 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3124 | |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 3125 | /* This causes DB_BUFFER_SMALL to be returned when the db has the key because |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3126 | it has a record but can't allocate a buffer for the data. This saves |
| 3127 | having to deal with data we won't be using. |
| 3128 | */ |
| 3129 | CLEAR_DBT(data); |
| 3130 | data.flags = DB_DBT_USERMEM; |
| 3131 | |
| 3132 | MYDB_BEGIN_ALLOW_THREADS; |
Gregory P. Smith | 0c65771 | 2004-03-16 06:56:58 +0000 | [diff] [blame] | 3133 | err = self->db->get(self->db, txn, &key, &data, 0); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3134 | MYDB_END_ALLOW_THREADS; |
| 3135 | FREE_DBT(key); |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 3136 | |
| 3137 | if (err == DB_BUFFER_SMALL || err == 0) { |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 3138 | return NUMBER_FromLong(1); |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 3139 | } else if (err == DB_NOTFOUND || err == DB_KEYEMPTY) { |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 3140 | return NUMBER_FromLong(0); |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 3141 | } |
| 3142 | |
| 3143 | makeDBError(err); |
| 3144 | return NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3145 | } |
| 3146 | |
| 3147 | |
| 3148 | #define _KEYS_LIST 1 |
| 3149 | #define _VALUES_LIST 2 |
| 3150 | #define _ITEMS_LIST 3 |
| 3151 | |
| 3152 | static PyObject* |
| 3153 | _DB_make_list(DBObject* self, DB_TXN* txn, int type) |
| 3154 | { |
| 3155 | int err, dbtype; |
| 3156 | DBT key; |
| 3157 | DBT data; |
| 3158 | DBC *cursor; |
| 3159 | PyObject* list; |
| 3160 | PyObject* item = NULL; |
| 3161 | |
| 3162 | CHECK_DB_NOT_CLOSED(self); |
| 3163 | CLEAR_DBT(key); |
| 3164 | CLEAR_DBT(data); |
| 3165 | |
| 3166 | dbtype = _DB_get_type(self); |
| 3167 | if (dbtype == -1) |
| 3168 | return NULL; |
| 3169 | |
| 3170 | list = PyList_New(0); |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 3171 | if (list == NULL) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3172 | return NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3173 | |
| 3174 | /* get a cursor */ |
| 3175 | MYDB_BEGIN_ALLOW_THREADS; |
Gregory P. Smith | 442c9fc | 2004-09-04 01:36:59 +0000 | [diff] [blame] | 3176 | err = self->db->cursor(self->db, txn, &cursor, 0); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3177 | MYDB_END_ALLOW_THREADS; |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 3178 | if (makeDBError(err)) { |
| 3179 | Py_DECREF(list); |
| 3180 | return NULL; |
| 3181 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3182 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3183 | while (1) { /* use the cursor to traverse the DB, collecting items */ |
| 3184 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3185 | err = _DBC_get(cursor, &key, &data, DB_NEXT); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3186 | MYDB_END_ALLOW_THREADS; |
| 3187 | |
| 3188 | if (err) { |
| 3189 | /* for any error, break out of the loop */ |
| 3190 | break; |
| 3191 | } |
| 3192 | |
| 3193 | switch (type) { |
| 3194 | case _KEYS_LIST: |
| 3195 | switch(dbtype) { |
| 3196 | case DB_BTREE: |
| 3197 | case DB_HASH: |
| 3198 | default: |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3199 | item = Build_PyString(key.data, key.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3200 | break; |
| 3201 | case DB_RECNO: |
| 3202 | case DB_QUEUE: |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 3203 | item = NUMBER_FromLong(*((db_recno_t*)key.data)); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3204 | break; |
| 3205 | } |
| 3206 | break; |
| 3207 | |
| 3208 | case _VALUES_LIST: |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3209 | item = Build_PyString(data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3210 | break; |
| 3211 | |
| 3212 | case _ITEMS_LIST: |
| 3213 | switch(dbtype) { |
| 3214 | case DB_BTREE: |
| 3215 | case DB_HASH: |
| 3216 | default: |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3217 | item = BuildValue_SS(key.data, key.size, data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3218 | break; |
| 3219 | case DB_RECNO: |
| 3220 | case DB_QUEUE: |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3221 | item = BuildValue_IS(*((db_recno_t*)key.data), data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3222 | break; |
| 3223 | } |
| 3224 | break; |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 3225 | default: |
| 3226 | PyErr_Format(PyExc_ValueError, "Unknown key type 0x%x", type); |
| 3227 | item = NULL; |
| 3228 | break; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3229 | } |
| 3230 | if (item == NULL) { |
| 3231 | Py_DECREF(list); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3232 | list = NULL; |
| 3233 | goto done; |
| 3234 | } |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 3235 | if (PyList_Append(list, item)) { |
| 3236 | Py_DECREF(list); |
| 3237 | Py_DECREF(item); |
| 3238 | list = NULL; |
| 3239 | goto done; |
| 3240 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3241 | Py_DECREF(item); |
| 3242 | } |
| 3243 | |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 3244 | /* DB_NOTFOUND || DB_KEYEMPTY is okay, it means we got to the end */ |
| 3245 | if (err != DB_NOTFOUND && err != DB_KEYEMPTY && makeDBError(err)) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3246 | Py_DECREF(list); |
| 3247 | list = NULL; |
| 3248 | } |
| 3249 | |
| 3250 | done: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3251 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3252 | _DBC_close(cursor); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3253 | MYDB_END_ALLOW_THREADS; |
| 3254 | return list; |
| 3255 | } |
| 3256 | |
| 3257 | |
| 3258 | static PyObject* |
| 3259 | DB_keys(DBObject* self, PyObject* args) |
| 3260 | { |
| 3261 | PyObject* txnobj = NULL; |
| 3262 | DB_TXN *txn = NULL; |
| 3263 | |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 3264 | if (!PyArg_UnpackTuple(args, "keys", 0, 1, &txnobj)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3265 | return NULL; |
| 3266 | if (!checkTxnObj(txnobj, &txn)) |
| 3267 | return NULL; |
| 3268 | return _DB_make_list(self, txn, _KEYS_LIST); |
| 3269 | } |
| 3270 | |
| 3271 | |
| 3272 | static PyObject* |
| 3273 | DB_items(DBObject* self, PyObject* args) |
| 3274 | { |
| 3275 | PyObject* txnobj = NULL; |
| 3276 | DB_TXN *txn = NULL; |
| 3277 | |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 3278 | if (!PyArg_UnpackTuple(args, "items", 0, 1, &txnobj)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3279 | return NULL; |
| 3280 | if (!checkTxnObj(txnobj, &txn)) |
| 3281 | return NULL; |
| 3282 | return _DB_make_list(self, txn, _ITEMS_LIST); |
| 3283 | } |
| 3284 | |
| 3285 | |
| 3286 | static PyObject* |
| 3287 | DB_values(DBObject* self, PyObject* args) |
| 3288 | { |
| 3289 | PyObject* txnobj = NULL; |
| 3290 | DB_TXN *txn = NULL; |
| 3291 | |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 3292 | if (!PyArg_UnpackTuple(args, "values", 0, 1, &txnobj)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3293 | return NULL; |
| 3294 | if (!checkTxnObj(txnobj, &txn)) |
| 3295 | return NULL; |
| 3296 | return _DB_make_list(self, txn, _VALUES_LIST); |
| 3297 | } |
| 3298 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3299 | /* --------------------------------------------------------------------- */ |
| 3300 | /* DBCursor methods */ |
| 3301 | |
| 3302 | |
| 3303 | static PyObject* |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3304 | DBC_close_internal(DBCursorObject* self) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3305 | { |
| 3306 | int err = 0; |
| 3307 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3308 | if (self->dbc != NULL) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3309 | EXTRACT_FROM_DOUBLE_LINKED_LIST(self); |
| 3310 | if (self->txn) { |
| 3311 | EXTRACT_FROM_DOUBLE_LINKED_LIST_TXN(self); |
| 3312 | self->txn=NULL; |
| 3313 | } |
| 3314 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3315 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3316 | err = _DBC_close(self->dbc); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3317 | MYDB_END_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3318 | self->dbc = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3319 | } |
| 3320 | RETURN_IF_ERR(); |
| 3321 | RETURN_NONE(); |
| 3322 | } |
| 3323 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3324 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 3325 | DBC_close(DBCursorObject* self) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3326 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3327 | return DBC_close_internal(self); |
| 3328 | } |
| 3329 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3330 | |
| 3331 | static PyObject* |
| 3332 | DBC_count(DBCursorObject* self, PyObject* args) |
| 3333 | { |
| 3334 | int err = 0; |
| 3335 | db_recno_t count; |
| 3336 | int flags = 0; |
| 3337 | |
| 3338 | if (!PyArg_ParseTuple(args, "|i:count", &flags)) |
| 3339 | return NULL; |
| 3340 | |
| 3341 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3342 | |
| 3343 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3344 | err = _DBC_count(self->dbc, &count, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3345 | MYDB_END_ALLOW_THREADS; |
| 3346 | RETURN_IF_ERR(); |
| 3347 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 3348 | return NUMBER_FromLong(count); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3349 | } |
| 3350 | |
| 3351 | |
| 3352 | static PyObject* |
| 3353 | DBC_current(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3354 | { |
| 3355 | return _DBCursor_get(self,DB_CURRENT,args,kwargs,"|iii:current"); |
| 3356 | } |
| 3357 | |
| 3358 | |
| 3359 | static PyObject* |
| 3360 | DBC_delete(DBCursorObject* self, PyObject* args) |
| 3361 | { |
| 3362 | int err, flags=0; |
| 3363 | |
| 3364 | if (!PyArg_ParseTuple(args, "|i:delete", &flags)) |
| 3365 | return NULL; |
| 3366 | |
| 3367 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3368 | |
| 3369 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3370 | err = _DBC_del(self->dbc, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3371 | MYDB_END_ALLOW_THREADS; |
| 3372 | RETURN_IF_ERR(); |
| 3373 | |
| 3374 | self->mydb->haveStat = 0; |
| 3375 | RETURN_NONE(); |
| 3376 | } |
| 3377 | |
| 3378 | |
| 3379 | static PyObject* |
| 3380 | DBC_dup(DBCursorObject* self, PyObject* args) |
| 3381 | { |
| 3382 | int err, flags =0; |
| 3383 | DBC* dbc = NULL; |
| 3384 | |
| 3385 | if (!PyArg_ParseTuple(args, "|i:dup", &flags)) |
| 3386 | return NULL; |
| 3387 | |
| 3388 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3389 | |
| 3390 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3391 | err = _DBC_dup(self->dbc, &dbc, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3392 | MYDB_END_ALLOW_THREADS; |
| 3393 | RETURN_IF_ERR(); |
| 3394 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3395 | return (PyObject*) newDBCursorObject(dbc, self->txn, self->mydb); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3396 | } |
| 3397 | |
| 3398 | static PyObject* |
| 3399 | DBC_first(DBCursorObject* self, PyObject* args, PyObject* kwargs) |
| 3400 | { |
| 3401 | return _DBCursor_get(self,DB_FIRST,args,kwargs,"|iii:first"); |
| 3402 | } |
| 3403 | |
| 3404 | |
| 3405 | static PyObject* |
| 3406 | DBC_get(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3407 | { |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 3408 | int err, flags=0; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3409 | PyObject* keyobj = NULL; |
| 3410 | PyObject* dataobj = NULL; |
| 3411 | PyObject* retval = NULL; |
| 3412 | int dlen = -1; |
| 3413 | int doff = -1; |
| 3414 | DBT key, data; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3415 | static char* kwnames[] = { "key","data", "flags", "dlen", "doff", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 3416 | NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3417 | |
| 3418 | CLEAR_DBT(key); |
| 3419 | CLEAR_DBT(data); |
| 3420 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|ii:get", &kwnames[2], |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 3421 | &flags, &dlen, &doff)) |
| 3422 | { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3423 | PyErr_Clear(); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 3424 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi|ii:get", |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 3425 | &kwnames[1], |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 3426 | &keyobj, &flags, &dlen, &doff)) |
| 3427 | { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3428 | PyErr_Clear(); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 3429 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OOi|ii:get", |
| 3430 | kwnames, &keyobj, &dataobj, |
| 3431 | &flags, &dlen, &doff)) |
| 3432 | { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3433 | return NULL; |
| 3434 | } |
| 3435 | } |
| 3436 | } |
| 3437 | |
| 3438 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3439 | |
| 3440 | if (keyobj && !make_key_dbt(self->mydb, keyobj, &key, NULL)) |
| 3441 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3442 | if ( (dataobj && !make_dbt(dataobj, &data)) || |
| 3443 | (!add_partial_dbt(&data, dlen, doff)) ) |
| 3444 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3445 | FREE_DBT(key); /* 'make_key_dbt' could do a 'malloc' */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3446 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3447 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3448 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3449 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3450 | err = _DBC_get(self->dbc, &key, &data, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3451 | MYDB_END_ALLOW_THREADS; |
| 3452 | |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 3453 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 3454 | && self->mydb->moduleFlags.getReturnsNone) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3455 | Py_INCREF(Py_None); |
| 3456 | retval = Py_None; |
| 3457 | } |
| 3458 | else if (makeDBError(err)) { |
| 3459 | retval = NULL; |
| 3460 | } |
| 3461 | else { |
| 3462 | switch (_DB_get_type(self->mydb)) { |
| 3463 | case -1: |
| 3464 | retval = NULL; |
| 3465 | break; |
| 3466 | case DB_BTREE: |
| 3467 | case DB_HASH: |
| 3468 | default: |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3469 | retval = BuildValue_SS(key.data, key.size, data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3470 | break; |
| 3471 | case DB_RECNO: |
| 3472 | case DB_QUEUE: |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3473 | retval = BuildValue_IS(*((db_recno_t*)key.data), data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3474 | break; |
| 3475 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3476 | } |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3477 | FREE_DBT(key); /* 'make_key_dbt' could do a 'malloc' */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3478 | return retval; |
| 3479 | } |
| 3480 | |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3481 | static PyObject* |
| 3482 | DBC_pget(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3483 | { |
| 3484 | int err, flags=0; |
| 3485 | PyObject* keyobj = NULL; |
| 3486 | PyObject* dataobj = NULL; |
| 3487 | PyObject* retval = NULL; |
| 3488 | int dlen = -1; |
| 3489 | int doff = -1; |
| 3490 | DBT key, pkey, data; |
Gregory P. Smith | 372b583 | 2006-06-05 18:48:21 +0000 | [diff] [blame] | 3491 | static char* kwnames_keyOnly[] = { "key", "flags", "dlen", "doff", NULL }; |
| 3492 | static char* kwnames[] = { "key", "data", "flags", "dlen", "doff", NULL }; |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3493 | |
| 3494 | CLEAR_DBT(key); |
| 3495 | CLEAR_DBT(data); |
| 3496 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|ii:pget", &kwnames[2], |
| 3497 | &flags, &dlen, &doff)) |
| 3498 | { |
| 3499 | PyErr_Clear(); |
| 3500 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi|ii:pget", |
Gregory P. Smith | 372b583 | 2006-06-05 18:48:21 +0000 | [diff] [blame] | 3501 | kwnames_keyOnly, |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3502 | &keyobj, &flags, &dlen, &doff)) |
| 3503 | { |
| 3504 | PyErr_Clear(); |
| 3505 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OOi|ii:pget", |
| 3506 | kwnames, &keyobj, &dataobj, |
| 3507 | &flags, &dlen, &doff)) |
| 3508 | { |
| 3509 | return NULL; |
| 3510 | } |
| 3511 | } |
| 3512 | } |
| 3513 | |
| 3514 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3515 | |
| 3516 | if (keyobj && !make_key_dbt(self->mydb, keyobj, &key, NULL)) |
| 3517 | return NULL; |
| 3518 | if ( (dataobj && !make_dbt(dataobj, &data)) || |
| 3519 | (!add_partial_dbt(&data, dlen, doff)) ) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3520 | FREE_DBT(key); /* 'make_key_dbt' could do a 'malloc' */ |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3521 | return NULL; |
| 3522 | } |
| 3523 | |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3524 | CLEAR_DBT(pkey); |
| 3525 | pkey.flags = DB_DBT_MALLOC; |
| 3526 | |
| 3527 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3528 | err = _DBC_pget(self->dbc, &key, &pkey, &data, flags); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3529 | MYDB_END_ALLOW_THREADS; |
| 3530 | |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 3531 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 3532 | && self->mydb->moduleFlags.getReturnsNone) { |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3533 | Py_INCREF(Py_None); |
| 3534 | retval = Py_None; |
| 3535 | } |
| 3536 | else if (makeDBError(err)) { |
| 3537 | retval = NULL; |
| 3538 | } |
| 3539 | else { |
| 3540 | PyObject *pkeyObj; |
| 3541 | PyObject *dataObj; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3542 | dataObj = Build_PyString(data.data, data.size); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3543 | |
| 3544 | if (self->mydb->primaryDBType == DB_RECNO || |
| 3545 | self->mydb->primaryDBType == DB_QUEUE) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 3546 | pkeyObj = NUMBER_FromLong(*(int *)pkey.data); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3547 | else |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3548 | pkeyObj = Build_PyString(pkey.data, pkey.size); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3549 | |
Gregory P. Smith | 4e414d8 | 2006-01-24 19:55:02 +0000 | [diff] [blame] | 3550 | if (key.data && key.size) /* return key, pkey and data */ |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3551 | { |
| 3552 | PyObject *keyObj; |
| 3553 | int type = _DB_get_type(self->mydb); |
| 3554 | if (type == DB_RECNO || type == DB_QUEUE) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 3555 | keyObj = NUMBER_FromLong(*(int *)key.data); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3556 | else |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3557 | keyObj = Build_PyString(key.data, key.size); |
Gregory P. Smith | fd049a6 | 2006-01-30 00:22:08 +0000 | [diff] [blame] | 3558 | #if (PY_VERSION_HEX >= 0x02040000) |
Gregory P. Smith | 4e414d8 | 2006-01-24 19:55:02 +0000 | [diff] [blame] | 3559 | retval = PyTuple_Pack(3, keyObj, pkeyObj, dataObj); |
Gregory P. Smith | fd049a6 | 2006-01-30 00:22:08 +0000 | [diff] [blame] | 3560 | #else |
| 3561 | retval = Py_BuildValue("OOO", keyObj, pkeyObj, dataObj); |
| 3562 | #endif |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 3563 | Py_DECREF(keyObj); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3564 | FREE_DBT(key); /* 'make_key_dbt' could do a 'malloc' */ |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3565 | } |
| 3566 | else /* return just the pkey and data */ |
| 3567 | { |
Gregory P. Smith | fd049a6 | 2006-01-30 00:22:08 +0000 | [diff] [blame] | 3568 | #if (PY_VERSION_HEX >= 0x02040000) |
Gregory P. Smith | 4e414d8 | 2006-01-24 19:55:02 +0000 | [diff] [blame] | 3569 | retval = PyTuple_Pack(2, pkeyObj, dataObj); |
Gregory P. Smith | fd049a6 | 2006-01-30 00:22:08 +0000 | [diff] [blame] | 3570 | #else |
| 3571 | retval = Py_BuildValue("OO", pkeyObj, dataObj); |
| 3572 | #endif |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3573 | } |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 3574 | Py_DECREF(dataObj); |
| 3575 | Py_DECREF(pkeyObj); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3576 | FREE_DBT(pkey); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3577 | } |
| 3578 | /* the only time REALLOC should be set is if we used an integer |
| 3579 | * key that make_key_dbt malloc'd for us. always free these. */ |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3580 | if (key.flags & DB_DBT_REALLOC) { /* 'make_key_dbt' could do a 'malloc' */ |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3581 | FREE_DBT(key); |
| 3582 | } |
| 3583 | return retval; |
| 3584 | } |
| 3585 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3586 | |
| 3587 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 3588 | DBC_get_recno(DBCursorObject* self) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3589 | { |
| 3590 | int err; |
| 3591 | db_recno_t recno; |
| 3592 | DBT key; |
| 3593 | DBT data; |
| 3594 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3595 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3596 | |
| 3597 | CLEAR_DBT(key); |
| 3598 | CLEAR_DBT(data); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3599 | |
| 3600 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3601 | err = _DBC_get(self->dbc, &key, &data, DB_GET_RECNO); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3602 | MYDB_END_ALLOW_THREADS; |
| 3603 | RETURN_IF_ERR(); |
| 3604 | |
| 3605 | recno = *((db_recno_t*)data.data); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 3606 | return NUMBER_FromLong(recno); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3607 | } |
| 3608 | |
| 3609 | |
| 3610 | static PyObject* |
| 3611 | DBC_last(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3612 | { |
| 3613 | return _DBCursor_get(self,DB_LAST,args,kwargs,"|iii:last"); |
| 3614 | } |
| 3615 | |
| 3616 | |
| 3617 | static PyObject* |
| 3618 | DBC_next(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3619 | { |
| 3620 | return _DBCursor_get(self,DB_NEXT,args,kwargs,"|iii:next"); |
| 3621 | } |
| 3622 | |
| 3623 | |
| 3624 | static PyObject* |
| 3625 | DBC_prev(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3626 | { |
| 3627 | return _DBCursor_get(self,DB_PREV,args,kwargs,"|iii:prev"); |
| 3628 | } |
| 3629 | |
| 3630 | |
| 3631 | static PyObject* |
| 3632 | DBC_put(DBCursorObject* self, PyObject* args, PyObject* kwargs) |
| 3633 | { |
| 3634 | int err, flags = 0; |
| 3635 | PyObject* keyobj, *dataobj; |
| 3636 | DBT key, data; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3637 | static char* kwnames[] = { "key", "data", "flags", "dlen", "doff", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 3638 | NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3639 | int dlen = -1; |
| 3640 | int doff = -1; |
| 3641 | |
| 3642 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|iii:put", kwnames, |
| 3643 | &keyobj, &dataobj, &flags, &dlen, &doff)) |
| 3644 | return NULL; |
| 3645 | |
| 3646 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3647 | |
| 3648 | if (!make_key_dbt(self->mydb, keyobj, &key, NULL)) |
| 3649 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3650 | if (!make_dbt(dataobj, &data) || |
| 3651 | !add_partial_dbt(&data, dlen, doff) ) |
| 3652 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3653 | FREE_DBT(key); /* 'make_key_dbt' could do a 'malloc' */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3654 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3655 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3656 | |
| 3657 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3658 | err = _DBC_put(self->dbc, &key, &data, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3659 | MYDB_END_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3660 | FREE_DBT(key); /* 'make_key_dbt' could do a 'malloc' */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3661 | RETURN_IF_ERR(); |
| 3662 | self->mydb->haveStat = 0; |
| 3663 | RETURN_NONE(); |
| 3664 | } |
| 3665 | |
| 3666 | |
| 3667 | static PyObject* |
| 3668 | DBC_set(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3669 | { |
| 3670 | int err, flags = 0; |
| 3671 | DBT key, data; |
| 3672 | PyObject* retval, *keyobj; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3673 | static char* kwnames[] = { "key", "flags", "dlen", "doff", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3674 | int dlen = -1; |
| 3675 | int doff = -1; |
| 3676 | |
| 3677 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|iii:set", kwnames, |
| 3678 | &keyobj, &flags, &dlen, &doff)) |
| 3679 | return NULL; |
| 3680 | |
| 3681 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3682 | |
| 3683 | if (!make_key_dbt(self->mydb, keyobj, &key, NULL)) |
| 3684 | return NULL; |
| 3685 | |
| 3686 | CLEAR_DBT(data); |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3687 | if (!add_partial_dbt(&data, dlen, doff)) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3688 | FREE_DBT(key); /* 'make_key_dbt' could do a 'malloc' */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3689 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3690 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3691 | |
| 3692 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3693 | err = _DBC_get(self->dbc, &key, &data, flags|DB_SET); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3694 | MYDB_END_ALLOW_THREADS; |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 3695 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 3696 | && self->mydb->moduleFlags.cursorSetReturnsNone) { |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3697 | Py_INCREF(Py_None); |
| 3698 | retval = Py_None; |
| 3699 | } |
| 3700 | else if (makeDBError(err)) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3701 | retval = NULL; |
| 3702 | } |
| 3703 | else { |
| 3704 | switch (_DB_get_type(self->mydb)) { |
| 3705 | case -1: |
| 3706 | retval = NULL; |
| 3707 | break; |
| 3708 | case DB_BTREE: |
| 3709 | case DB_HASH: |
| 3710 | default: |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3711 | retval = BuildValue_SS(key.data, key.size, data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3712 | break; |
| 3713 | case DB_RECNO: |
| 3714 | case DB_QUEUE: |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3715 | retval = BuildValue_IS(*((db_recno_t*)key.data), data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3716 | break; |
| 3717 | } |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3718 | FREE_DBT(key); /* 'make_key_dbt' could do a 'malloc' */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3719 | } |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3720 | /* the only time REALLOC should be set is if we used an integer |
| 3721 | * key that make_key_dbt malloc'd for us. always free these. */ |
| 3722 | if (key.flags & DB_DBT_REALLOC) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3723 | FREE_DBT(key); /* 'make_key_dbt' could do a 'malloc' */ |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3724 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3725 | |
| 3726 | return retval; |
| 3727 | } |
| 3728 | |
| 3729 | |
| 3730 | static PyObject* |
| 3731 | DBC_set_range(DBCursorObject* self, PyObject* args, PyObject* kwargs) |
| 3732 | { |
| 3733 | int err, flags = 0; |
| 3734 | DBT key, data; |
| 3735 | PyObject* retval, *keyobj; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3736 | static char* kwnames[] = { "key", "flags", "dlen", "doff", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3737 | int dlen = -1; |
| 3738 | int doff = -1; |
| 3739 | |
| 3740 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|iii:set_range", kwnames, |
| 3741 | &keyobj, &flags, &dlen, &doff)) |
| 3742 | return NULL; |
| 3743 | |
| 3744 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3745 | |
| 3746 | if (!make_key_dbt(self->mydb, keyobj, &key, NULL)) |
| 3747 | return NULL; |
| 3748 | |
| 3749 | CLEAR_DBT(data); |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3750 | if (!add_partial_dbt(&data, dlen, doff)) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3751 | FREE_DBT(key); /* 'make_key_dbt' could do a 'malloc' */ |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3752 | return NULL; |
| 3753 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3754 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3755 | err = _DBC_get(self->dbc, &key, &data, flags|DB_SET_RANGE); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3756 | MYDB_END_ALLOW_THREADS; |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 3757 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 3758 | && self->mydb->moduleFlags.cursorSetReturnsNone) { |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3759 | Py_INCREF(Py_None); |
| 3760 | retval = Py_None; |
| 3761 | } |
| 3762 | else if (makeDBError(err)) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3763 | retval = NULL; |
| 3764 | } |
| 3765 | else { |
| 3766 | switch (_DB_get_type(self->mydb)) { |
| 3767 | case -1: |
| 3768 | retval = NULL; |
| 3769 | break; |
| 3770 | case DB_BTREE: |
| 3771 | case DB_HASH: |
| 3772 | default: |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3773 | retval = BuildValue_SS(key.data, key.size, data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3774 | break; |
| 3775 | case DB_RECNO: |
| 3776 | case DB_QUEUE: |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3777 | retval = BuildValue_IS(*((db_recno_t*)key.data), data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3778 | break; |
| 3779 | } |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3780 | FREE_DBT(key); /* 'make_key_dbt' could do a 'malloc' */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3781 | } |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3782 | /* the only time REALLOC should be set is if we used an integer |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3783 | * key that make_key_dbt malloc'd for us. always free these. */ |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3784 | if (key.flags & DB_DBT_REALLOC) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3785 | FREE_DBT(key); /* 'make_key_dbt' could do a 'malloc' */ |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3786 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3787 | |
| 3788 | return retval; |
| 3789 | } |
| 3790 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3791 | static PyObject* |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3792 | _DBC_get_set_both(DBCursorObject* self, PyObject* keyobj, PyObject* dataobj, |
| 3793 | int flags, unsigned int returnsNone) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3794 | { |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3795 | int err; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3796 | DBT key, data; |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3797 | PyObject* retval; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3798 | |
Gregory P. Smith | 7441e65 | 2003-11-03 21:35:31 +0000 | [diff] [blame] | 3799 | /* the caller did this: CHECK_CURSOR_NOT_CLOSED(self); */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3800 | if (!make_key_dbt(self->mydb, keyobj, &key, NULL)) |
| 3801 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3802 | if (!make_dbt(dataobj, &data)) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3803 | FREE_DBT(key); /* 'make_key_dbt' could do a 'malloc' */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3804 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3805 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3806 | |
| 3807 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3808 | err = _DBC_get(self->dbc, &key, &data, flags|DB_GET_BOTH); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3809 | MYDB_END_ALLOW_THREADS; |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 3810 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) && returnsNone) { |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3811 | Py_INCREF(Py_None); |
| 3812 | retval = Py_None; |
| 3813 | } |
| 3814 | else if (makeDBError(err)) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3815 | retval = NULL; |
| 3816 | } |
| 3817 | else { |
| 3818 | switch (_DB_get_type(self->mydb)) { |
| 3819 | case -1: |
| 3820 | retval = NULL; |
| 3821 | break; |
| 3822 | case DB_BTREE: |
| 3823 | case DB_HASH: |
| 3824 | default: |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3825 | retval = BuildValue_SS(key.data, key.size, data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3826 | break; |
| 3827 | case DB_RECNO: |
| 3828 | case DB_QUEUE: |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3829 | retval = BuildValue_IS(*((db_recno_t*)key.data), data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3830 | break; |
| 3831 | } |
| 3832 | } |
| 3833 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3834 | FREE_DBT(key); /* 'make_key_dbt' could do a 'malloc' */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3835 | return retval; |
| 3836 | } |
| 3837 | |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3838 | static PyObject* |
| 3839 | DBC_get_both(DBCursorObject* self, PyObject* args) |
| 3840 | { |
| 3841 | int flags=0; |
| 3842 | PyObject *keyobj, *dataobj; |
| 3843 | |
| 3844 | if (!PyArg_ParseTuple(args, "OO|i:get_both", &keyobj, &dataobj, &flags)) |
| 3845 | return NULL; |
| 3846 | |
Gregory P. Smith | 7441e65 | 2003-11-03 21:35:31 +0000 | [diff] [blame] | 3847 | /* if the cursor is closed, self->mydb may be invalid */ |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3848 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3849 | |
| 3850 | return _DBC_get_set_both(self, keyobj, dataobj, flags, |
| 3851 | self->mydb->moduleFlags.getReturnsNone); |
| 3852 | } |
| 3853 | |
Gregory P. Smith | be0db8b | 2003-10-01 06:48:51 +0000 | [diff] [blame] | 3854 | /* Return size of entry */ |
| 3855 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 3856 | DBC_get_current_size(DBCursorObject* self) |
Gregory P. Smith | be0db8b | 2003-10-01 06:48:51 +0000 | [diff] [blame] | 3857 | { |
| 3858 | int err, flags=DB_CURRENT; |
| 3859 | PyObject* retval = NULL; |
| 3860 | DBT key, data; |
| 3861 | |
Gregory P. Smith | be0db8b | 2003-10-01 06:48:51 +0000 | [diff] [blame] | 3862 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3863 | CLEAR_DBT(key); |
| 3864 | CLEAR_DBT(data); |
| 3865 | |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 3866 | /* We don't allocate any memory, forcing a DB_BUFFER_SMALL error and thus |
Gregory P. Smith | be0db8b | 2003-10-01 06:48:51 +0000 | [diff] [blame] | 3867 | getting the record size. */ |
| 3868 | data.flags = DB_DBT_USERMEM; |
| 3869 | data.ulen = 0; |
| 3870 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3871 | err = _DBC_get(self->dbc, &key, &data, flags); |
Gregory P. Smith | be0db8b | 2003-10-01 06:48:51 +0000 | [diff] [blame] | 3872 | MYDB_END_ALLOW_THREADS; |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 3873 | if (err == DB_BUFFER_SMALL || !err) { |
| 3874 | /* DB_BUFFER_SMALL means positive size, !err means zero length value */ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 3875 | retval = NUMBER_FromLong((long)data.size); |
Gregory P. Smith | be0db8b | 2003-10-01 06:48:51 +0000 | [diff] [blame] | 3876 | err = 0; |
| 3877 | } |
| 3878 | |
Gregory P. Smith | be0db8b | 2003-10-01 06:48:51 +0000 | [diff] [blame] | 3879 | RETURN_IF_ERR(); |
| 3880 | return retval; |
| 3881 | } |
| 3882 | |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3883 | static PyObject* |
| 3884 | DBC_set_both(DBCursorObject* self, PyObject* args) |
| 3885 | { |
| 3886 | int flags=0; |
| 3887 | PyObject *keyobj, *dataobj; |
| 3888 | |
| 3889 | if (!PyArg_ParseTuple(args, "OO|i:set_both", &keyobj, &dataobj, &flags)) |
| 3890 | return NULL; |
| 3891 | |
Gregory P. Smith | 7441e65 | 2003-11-03 21:35:31 +0000 | [diff] [blame] | 3892 | /* if the cursor is closed, self->mydb may be invalid */ |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3893 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3894 | |
| 3895 | return _DBC_get_set_both(self, keyobj, dataobj, flags, |
| 3896 | self->mydb->moduleFlags.cursorSetReturnsNone); |
| 3897 | } |
| 3898 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3899 | |
| 3900 | static PyObject* |
| 3901 | DBC_set_recno(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3902 | { |
| 3903 | int err, irecno, flags=0; |
| 3904 | db_recno_t recno; |
| 3905 | DBT key, data; |
| 3906 | PyObject* retval; |
| 3907 | int dlen = -1; |
| 3908 | int doff = -1; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3909 | static char* kwnames[] = { "recno","flags", "dlen", "doff", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3910 | |
| 3911 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|iii:set_recno", kwnames, |
| 3912 | &irecno, &flags, &dlen, &doff)) |
| 3913 | return NULL; |
| 3914 | |
| 3915 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3916 | |
| 3917 | CLEAR_DBT(key); |
| 3918 | recno = (db_recno_t) irecno; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 3919 | /* use allocated space so DB will be able to realloc room for the real |
| 3920 | * key */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3921 | key.data = malloc(sizeof(db_recno_t)); |
| 3922 | if (key.data == NULL) { |
| 3923 | PyErr_SetString(PyExc_MemoryError, "Key memory allocation failed"); |
| 3924 | return NULL; |
| 3925 | } |
| 3926 | key.size = sizeof(db_recno_t); |
| 3927 | key.ulen = key.size; |
| 3928 | memcpy(key.data, &recno, sizeof(db_recno_t)); |
| 3929 | key.flags = DB_DBT_REALLOC; |
| 3930 | |
| 3931 | CLEAR_DBT(data); |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3932 | if (!add_partial_dbt(&data, dlen, doff)) { |
| 3933 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3934 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3935 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3936 | |
| 3937 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3938 | err = _DBC_get(self->dbc, &key, &data, flags|DB_SET_RECNO); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3939 | MYDB_END_ALLOW_THREADS; |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 3940 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 3941 | && self->mydb->moduleFlags.cursorSetReturnsNone) { |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3942 | Py_INCREF(Py_None); |
| 3943 | retval = Py_None; |
| 3944 | } |
| 3945 | else if (makeDBError(err)) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3946 | retval = NULL; |
| 3947 | } |
| 3948 | else { /* Can only be used for BTrees, so no need to return int key */ |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 3949 | retval = BuildValue_SS(key.data, key.size, data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3950 | } |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3951 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3952 | |
| 3953 | return retval; |
| 3954 | } |
| 3955 | |
| 3956 | |
| 3957 | static PyObject* |
| 3958 | DBC_consume(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3959 | { |
| 3960 | return _DBCursor_get(self,DB_CONSUME,args,kwargs,"|iii:consume"); |
| 3961 | } |
| 3962 | |
| 3963 | |
| 3964 | static PyObject* |
| 3965 | DBC_next_dup(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3966 | { |
| 3967 | return _DBCursor_get(self,DB_NEXT_DUP,args,kwargs,"|iii:next_dup"); |
| 3968 | } |
| 3969 | |
| 3970 | |
| 3971 | static PyObject* |
| 3972 | DBC_next_nodup(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3973 | { |
| 3974 | return _DBCursor_get(self,DB_NEXT_NODUP,args,kwargs,"|iii:next_nodup"); |
| 3975 | } |
| 3976 | |
| 3977 | |
| 3978 | static PyObject* |
| 3979 | DBC_prev_nodup(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3980 | { |
| 3981 | return _DBCursor_get(self,DB_PREV_NODUP,args,kwargs,"|iii:prev_nodup"); |
| 3982 | } |
| 3983 | |
| 3984 | |
| 3985 | static PyObject* |
| 3986 | DBC_join_item(DBCursorObject* self, PyObject* args) |
| 3987 | { |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3988 | int err, flags=0; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3989 | DBT key, data; |
| 3990 | PyObject* retval; |
| 3991 | |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3992 | if (!PyArg_ParseTuple(args, "|i:join_item", &flags)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3993 | return NULL; |
| 3994 | |
| 3995 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3996 | |
| 3997 | CLEAR_DBT(key); |
| 3998 | CLEAR_DBT(data); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3999 | |
| 4000 | MYDB_BEGIN_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4001 | err = _DBC_get(self->dbc, &key, &data, flags | DB_JOIN_ITEM); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4002 | MYDB_END_ALLOW_THREADS; |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 4003 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 4004 | && self->mydb->moduleFlags.getReturnsNone) { |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 4005 | Py_INCREF(Py_None); |
| 4006 | retval = Py_None; |
| 4007 | } |
| 4008 | else if (makeDBError(err)) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4009 | retval = NULL; |
| 4010 | } |
| 4011 | else { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4012 | retval = BuildValue_S(key.data, key.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4013 | } |
| 4014 | |
| 4015 | return retval; |
| 4016 | } |
| 4017 | |
| 4018 | |
| 4019 | |
| 4020 | /* --------------------------------------------------------------------- */ |
| 4021 | /* DBEnv methods */ |
| 4022 | |
| 4023 | |
| 4024 | static PyObject* |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4025 | DBEnv_close_internal(DBEnvObject* self, int flags) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4026 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4027 | PyObject *dummy; |
| 4028 | int err; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4029 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4030 | if (!self->closed) { /* Don't close more than once */ |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4031 | while(self->children_txns) { |
| 4032 | dummy=DBTxn_abort_discard_internal(self->children_txns,0); |
| 4033 | Py_XDECREF(dummy); |
| 4034 | } |
| 4035 | while(self->children_dbs) { |
Jesus Cea | 5cd5f12 | 2008-09-23 18:54:08 +0000 | [diff] [blame] | 4036 | dummy=DB_close_internal(self->children_dbs, 0, 0); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4037 | Py_XDECREF(dummy); |
| 4038 | } |
Jesus Cea | ac25fab | 2008-09-03 17:50:32 +0000 | [diff] [blame] | 4039 | } |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4040 | |
Jesus Cea | ac25fab | 2008-09-03 17:50:32 +0000 | [diff] [blame] | 4041 | self->closed = 1; |
| 4042 | if (self->db_env) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4043 | MYDB_BEGIN_ALLOW_THREADS; |
| 4044 | err = self->db_env->close(self->db_env, flags); |
| 4045 | MYDB_END_ALLOW_THREADS; |
| 4046 | /* after calling DBEnv->close, regardless of error, this DBEnv |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4047 | * may not be accessed again (Berkeley DB docs). */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4048 | self->db_env = NULL; |
| 4049 | RETURN_IF_ERR(); |
| 4050 | } |
| 4051 | RETURN_NONE(); |
| 4052 | } |
| 4053 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4054 | static PyObject* |
| 4055 | DBEnv_close(DBEnvObject* self, PyObject* args) |
| 4056 | { |
| 4057 | int flags = 0; |
| 4058 | |
| 4059 | if (!PyArg_ParseTuple(args, "|i:close", &flags)) |
| 4060 | return NULL; |
Jesus Cea | 5cd5f12 | 2008-09-23 18:54:08 +0000 | [diff] [blame] | 4061 | return DBEnv_close_internal(self, flags); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4062 | } |
| 4063 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4064 | |
| 4065 | static PyObject* |
| 4066 | DBEnv_open(DBEnvObject* self, PyObject* args) |
| 4067 | { |
| 4068 | int err, flags=0, mode=0660; |
| 4069 | char *db_home; |
| 4070 | |
| 4071 | if (!PyArg_ParseTuple(args, "z|ii:open", &db_home, &flags, &mode)) |
| 4072 | return NULL; |
| 4073 | |
| 4074 | CHECK_ENV_NOT_CLOSED(self); |
| 4075 | |
| 4076 | MYDB_BEGIN_ALLOW_THREADS; |
| 4077 | err = self->db_env->open(self->db_env, db_home, flags, mode); |
| 4078 | MYDB_END_ALLOW_THREADS; |
| 4079 | RETURN_IF_ERR(); |
| 4080 | self->closed = 0; |
| 4081 | self->flags = flags; |
| 4082 | RETURN_NONE(); |
| 4083 | } |
| 4084 | |
| 4085 | |
| 4086 | static PyObject* |
| 4087 | DBEnv_remove(DBEnvObject* self, PyObject* args) |
| 4088 | { |
| 4089 | int err, flags=0; |
| 4090 | char *db_home; |
| 4091 | |
| 4092 | if (!PyArg_ParseTuple(args, "s|i:remove", &db_home, &flags)) |
| 4093 | return NULL; |
| 4094 | CHECK_ENV_NOT_CLOSED(self); |
| 4095 | MYDB_BEGIN_ALLOW_THREADS; |
| 4096 | err = self->db_env->remove(self->db_env, db_home, flags); |
| 4097 | MYDB_END_ALLOW_THREADS; |
| 4098 | RETURN_IF_ERR(); |
| 4099 | RETURN_NONE(); |
| 4100 | } |
| 4101 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 4102 | #if (DBVER >= 41) |
| 4103 | static PyObject* |
| 4104 | DBEnv_dbremove(DBEnvObject* self, PyObject* args, PyObject* kwargs) |
| 4105 | { |
| 4106 | int err; |
| 4107 | u_int32_t flags=0; |
| 4108 | char *file = NULL; |
| 4109 | char *database = NULL; |
| 4110 | PyObject *txnobj = NULL; |
| 4111 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 4112 | static char* kwnames[] = { "file", "database", "txn", "flags", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4113 | NULL }; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 4114 | |
Gregory P. Smith | 641cddf | 2006-07-28 01:35:25 +0000 | [diff] [blame] | 4115 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|zOi:dbremove", kwnames, |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 4116 | &file, &database, &txnobj, &flags)) { |
| 4117 | return NULL; |
| 4118 | } |
| 4119 | if (!checkTxnObj(txnobj, &txn)) { |
| 4120 | return NULL; |
| 4121 | } |
| 4122 | CHECK_ENV_NOT_CLOSED(self); |
| 4123 | MYDB_BEGIN_ALLOW_THREADS; |
| 4124 | err = self->db_env->dbremove(self->db_env, txn, file, database, flags); |
| 4125 | MYDB_END_ALLOW_THREADS; |
| 4126 | RETURN_IF_ERR(); |
| 4127 | RETURN_NONE(); |
| 4128 | } |
| 4129 | |
| 4130 | static PyObject* |
| 4131 | DBEnv_dbrename(DBEnvObject* self, PyObject* args, PyObject* kwargs) |
| 4132 | { |
| 4133 | int err; |
| 4134 | u_int32_t flags=0; |
| 4135 | char *file = NULL; |
| 4136 | char *database = NULL; |
| 4137 | char *newname = NULL; |
| 4138 | PyObject *txnobj = NULL; |
| 4139 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 4140 | static char* kwnames[] = { "file", "database", "newname", "txn", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 4141 | "flags", NULL }; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 4142 | |
Gregory P. Smith | 641cddf | 2006-07-28 01:35:25 +0000 | [diff] [blame] | 4143 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "szs|Oi:dbrename", kwnames, |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 4144 | &file, &database, &newname, &txnobj, &flags)) { |
| 4145 | return NULL; |
| 4146 | } |
| 4147 | if (!checkTxnObj(txnobj, &txn)) { |
| 4148 | return NULL; |
| 4149 | } |
| 4150 | CHECK_ENV_NOT_CLOSED(self); |
| 4151 | MYDB_BEGIN_ALLOW_THREADS; |
| 4152 | err = self->db_env->dbrename(self->db_env, txn, file, database, newname, |
| 4153 | flags); |
| 4154 | MYDB_END_ALLOW_THREADS; |
| 4155 | RETURN_IF_ERR(); |
| 4156 | RETURN_NONE(); |
| 4157 | } |
| 4158 | |
| 4159 | static PyObject* |
| 4160 | DBEnv_set_encrypt(DBEnvObject* self, PyObject* args, PyObject* kwargs) |
| 4161 | { |
| 4162 | int err; |
| 4163 | u_int32_t flags=0; |
| 4164 | char *passwd = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 4165 | static char* kwnames[] = { "passwd", "flags", NULL }; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 4166 | |
| 4167 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|i:set_encrypt", kwnames, |
| 4168 | &passwd, &flags)) { |
| 4169 | return NULL; |
| 4170 | } |
| 4171 | |
| 4172 | MYDB_BEGIN_ALLOW_THREADS; |
| 4173 | err = self->db_env->set_encrypt(self->db_env, passwd, flags); |
| 4174 | MYDB_END_ALLOW_THREADS; |
| 4175 | |
| 4176 | RETURN_IF_ERR(); |
| 4177 | RETURN_NONE(); |
| 4178 | } |
| 4179 | #endif /* DBVER >= 41 */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4180 | |
Gregory P. Smith | fe11d3e | 2003-03-27 17:23:29 +0000 | [diff] [blame] | 4181 | static PyObject* |
| 4182 | DBEnv_set_timeout(DBEnvObject* self, PyObject* args, PyObject* kwargs) |
| 4183 | { |
| 4184 | int err; |
| 4185 | u_int32_t flags=0; |
| 4186 | u_int32_t timeout = 0; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 4187 | static char* kwnames[] = { "timeout", "flags", NULL }; |
Gregory P. Smith | fe11d3e | 2003-03-27 17:23:29 +0000 | [diff] [blame] | 4188 | |
| 4189 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii:set_timeout", kwnames, |
| 4190 | &timeout, &flags)) { |
| 4191 | return NULL; |
| 4192 | } |
| 4193 | |
| 4194 | MYDB_BEGIN_ALLOW_THREADS; |
| 4195 | err = self->db_env->set_timeout(self->db_env, (db_timeout_t)timeout, flags); |
| 4196 | MYDB_END_ALLOW_THREADS; |
| 4197 | |
| 4198 | RETURN_IF_ERR(); |
| 4199 | RETURN_NONE(); |
| 4200 | } |
Gregory P. Smith | fe11d3e | 2003-03-27 17:23:29 +0000 | [diff] [blame] | 4201 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4202 | static PyObject* |
Gregory P. Smith | 6676f6e | 2003-08-28 21:50:30 +0000 | [diff] [blame] | 4203 | DBEnv_set_shm_key(DBEnvObject* self, PyObject* args) |
| 4204 | { |
| 4205 | int err; |
| 4206 | long shm_key = 0; |
| 4207 | |
| 4208 | if (!PyArg_ParseTuple(args, "l:set_shm_key", &shm_key)) |
| 4209 | return NULL; |
| 4210 | CHECK_ENV_NOT_CLOSED(self); |
| 4211 | |
| 4212 | err = self->db_env->set_shm_key(self->db_env, shm_key); |
| 4213 | RETURN_IF_ERR(); |
| 4214 | RETURN_NONE(); |
| 4215 | } |
| 4216 | |
| 4217 | static PyObject* |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4218 | DBEnv_set_cachesize(DBEnvObject* self, PyObject* args) |
| 4219 | { |
| 4220 | int err, gbytes=0, bytes=0, ncache=0; |
| 4221 | |
| 4222 | if (!PyArg_ParseTuple(args, "ii|i:set_cachesize", |
| 4223 | &gbytes, &bytes, &ncache)) |
| 4224 | return NULL; |
| 4225 | CHECK_ENV_NOT_CLOSED(self); |
| 4226 | |
| 4227 | MYDB_BEGIN_ALLOW_THREADS; |
| 4228 | err = self->db_env->set_cachesize(self->db_env, gbytes, bytes, ncache); |
| 4229 | MYDB_END_ALLOW_THREADS; |
| 4230 | RETURN_IF_ERR(); |
| 4231 | RETURN_NONE(); |
| 4232 | } |
| 4233 | |
| 4234 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4235 | static PyObject* |
| 4236 | DBEnv_set_flags(DBEnvObject* self, PyObject* args) |
| 4237 | { |
| 4238 | int err, flags=0, onoff=0; |
| 4239 | |
| 4240 | if (!PyArg_ParseTuple(args, "ii:set_flags", |
| 4241 | &flags, &onoff)) |
| 4242 | return NULL; |
| 4243 | CHECK_ENV_NOT_CLOSED(self); |
| 4244 | |
| 4245 | MYDB_BEGIN_ALLOW_THREADS; |
| 4246 | err = self->db_env->set_flags(self->db_env, flags, onoff); |
| 4247 | MYDB_END_ALLOW_THREADS; |
| 4248 | RETURN_IF_ERR(); |
| 4249 | RETURN_NONE(); |
| 4250 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4251 | |
| 4252 | |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 4253 | #if (DBVER >= 47) |
| 4254 | static PyObject* |
| 4255 | DBEnv_log_set_config(DBEnvObject* self, PyObject* args) |
| 4256 | { |
| 4257 | int err, flags, onoff; |
| 4258 | |
| 4259 | if (!PyArg_ParseTuple(args, "ii:log_set_config", |
| 4260 | &flags, &onoff)) |
| 4261 | return NULL; |
| 4262 | CHECK_ENV_NOT_CLOSED(self); |
| 4263 | |
| 4264 | MYDB_BEGIN_ALLOW_THREADS; |
| 4265 | err = self->db_env->log_set_config(self->db_env, flags, onoff); |
| 4266 | MYDB_END_ALLOW_THREADS; |
| 4267 | RETURN_IF_ERR(); |
| 4268 | RETURN_NONE(); |
| 4269 | } |
| 4270 | #endif /* DBVER >= 47 */ |
| 4271 | |
| 4272 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4273 | static PyObject* |
| 4274 | DBEnv_set_data_dir(DBEnvObject* self, PyObject* args) |
| 4275 | { |
| 4276 | int err; |
| 4277 | char *dir; |
| 4278 | |
| 4279 | if (!PyArg_ParseTuple(args, "s:set_data_dir", &dir)) |
| 4280 | return NULL; |
| 4281 | CHECK_ENV_NOT_CLOSED(self); |
| 4282 | |
| 4283 | MYDB_BEGIN_ALLOW_THREADS; |
| 4284 | err = self->db_env->set_data_dir(self->db_env, dir); |
| 4285 | MYDB_END_ALLOW_THREADS; |
| 4286 | RETURN_IF_ERR(); |
| 4287 | RETURN_NONE(); |
| 4288 | } |
| 4289 | |
| 4290 | |
| 4291 | static PyObject* |
| 4292 | DBEnv_set_lg_bsize(DBEnvObject* self, PyObject* args) |
| 4293 | { |
| 4294 | int err, lg_bsize; |
| 4295 | |
| 4296 | if (!PyArg_ParseTuple(args, "i:set_lg_bsize", &lg_bsize)) |
| 4297 | return NULL; |
| 4298 | CHECK_ENV_NOT_CLOSED(self); |
| 4299 | |
| 4300 | MYDB_BEGIN_ALLOW_THREADS; |
| 4301 | err = self->db_env->set_lg_bsize(self->db_env, lg_bsize); |
| 4302 | MYDB_END_ALLOW_THREADS; |
| 4303 | RETURN_IF_ERR(); |
| 4304 | RETURN_NONE(); |
| 4305 | } |
| 4306 | |
| 4307 | |
| 4308 | static PyObject* |
| 4309 | DBEnv_set_lg_dir(DBEnvObject* self, PyObject* args) |
| 4310 | { |
| 4311 | int err; |
| 4312 | char *dir; |
| 4313 | |
| 4314 | if (!PyArg_ParseTuple(args, "s:set_lg_dir", &dir)) |
| 4315 | return NULL; |
| 4316 | CHECK_ENV_NOT_CLOSED(self); |
| 4317 | |
| 4318 | MYDB_BEGIN_ALLOW_THREADS; |
| 4319 | err = self->db_env->set_lg_dir(self->db_env, dir); |
| 4320 | MYDB_END_ALLOW_THREADS; |
| 4321 | RETURN_IF_ERR(); |
| 4322 | RETURN_NONE(); |
| 4323 | } |
| 4324 | |
| 4325 | static PyObject* |
| 4326 | DBEnv_set_lg_max(DBEnvObject* self, PyObject* args) |
| 4327 | { |
| 4328 | int err, lg_max; |
| 4329 | |
| 4330 | if (!PyArg_ParseTuple(args, "i:set_lg_max", &lg_max)) |
| 4331 | return NULL; |
| 4332 | CHECK_ENV_NOT_CLOSED(self); |
| 4333 | |
| 4334 | MYDB_BEGIN_ALLOW_THREADS; |
| 4335 | err = self->db_env->set_lg_max(self->db_env, lg_max); |
| 4336 | MYDB_END_ALLOW_THREADS; |
| 4337 | RETURN_IF_ERR(); |
| 4338 | RETURN_NONE(); |
| 4339 | } |
| 4340 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4341 | #if (DBVER >= 42) |
| 4342 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 4343 | DBEnv_get_lg_max(DBEnvObject* self) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4344 | { |
| 4345 | int err; |
| 4346 | u_int32_t lg_max; |
| 4347 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4348 | CHECK_ENV_NOT_CLOSED(self); |
| 4349 | |
| 4350 | MYDB_BEGIN_ALLOW_THREADS; |
| 4351 | err = self->db_env->get_lg_max(self->db_env, &lg_max); |
| 4352 | MYDB_END_ALLOW_THREADS; |
| 4353 | RETURN_IF_ERR(); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 4354 | return NUMBER_FromLong(lg_max); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4355 | } |
| 4356 | #endif |
| 4357 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4358 | |
| 4359 | static PyObject* |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 4360 | DBEnv_set_lg_regionmax(DBEnvObject* self, PyObject* args) |
| 4361 | { |
| 4362 | int err, lg_max; |
| 4363 | |
| 4364 | if (!PyArg_ParseTuple(args, "i:set_lg_regionmax", &lg_max)) |
| 4365 | return NULL; |
| 4366 | CHECK_ENV_NOT_CLOSED(self); |
| 4367 | |
| 4368 | MYDB_BEGIN_ALLOW_THREADS; |
| 4369 | err = self->db_env->set_lg_regionmax(self->db_env, lg_max); |
| 4370 | MYDB_END_ALLOW_THREADS; |
| 4371 | RETURN_IF_ERR(); |
| 4372 | RETURN_NONE(); |
| 4373 | } |
| 4374 | |
| 4375 | |
| 4376 | static PyObject* |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4377 | DBEnv_set_lk_detect(DBEnvObject* self, PyObject* args) |
| 4378 | { |
| 4379 | int err, lk_detect; |
| 4380 | |
| 4381 | if (!PyArg_ParseTuple(args, "i:set_lk_detect", &lk_detect)) |
| 4382 | return NULL; |
| 4383 | CHECK_ENV_NOT_CLOSED(self); |
| 4384 | |
| 4385 | MYDB_BEGIN_ALLOW_THREADS; |
| 4386 | err = self->db_env->set_lk_detect(self->db_env, lk_detect); |
| 4387 | MYDB_END_ALLOW_THREADS; |
| 4388 | RETURN_IF_ERR(); |
| 4389 | RETURN_NONE(); |
| 4390 | } |
| 4391 | |
| 4392 | |
Gregory P. Smith | 8b96a35 | 2007-01-05 01:59:42 +0000 | [diff] [blame] | 4393 | #if (DBVER < 45) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4394 | static PyObject* |
| 4395 | DBEnv_set_lk_max(DBEnvObject* self, PyObject* args) |
| 4396 | { |
| 4397 | int err, max; |
| 4398 | |
| 4399 | if (!PyArg_ParseTuple(args, "i:set_lk_max", &max)) |
| 4400 | return NULL; |
| 4401 | CHECK_ENV_NOT_CLOSED(self); |
| 4402 | |
| 4403 | MYDB_BEGIN_ALLOW_THREADS; |
| 4404 | err = self->db_env->set_lk_max(self->db_env, max); |
| 4405 | MYDB_END_ALLOW_THREADS; |
| 4406 | RETURN_IF_ERR(); |
| 4407 | RETURN_NONE(); |
| 4408 | } |
Gregory P. Smith | 8b96a35 | 2007-01-05 01:59:42 +0000 | [diff] [blame] | 4409 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4410 | |
| 4411 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4412 | |
| 4413 | static PyObject* |
| 4414 | DBEnv_set_lk_max_locks(DBEnvObject* self, PyObject* args) |
| 4415 | { |
| 4416 | int err, max; |
| 4417 | |
| 4418 | if (!PyArg_ParseTuple(args, "i:set_lk_max_locks", &max)) |
| 4419 | return NULL; |
| 4420 | CHECK_ENV_NOT_CLOSED(self); |
| 4421 | |
| 4422 | MYDB_BEGIN_ALLOW_THREADS; |
| 4423 | err = self->db_env->set_lk_max_locks(self->db_env, max); |
| 4424 | MYDB_END_ALLOW_THREADS; |
| 4425 | RETURN_IF_ERR(); |
| 4426 | RETURN_NONE(); |
| 4427 | } |
| 4428 | |
| 4429 | |
| 4430 | static PyObject* |
| 4431 | DBEnv_set_lk_max_lockers(DBEnvObject* self, PyObject* args) |
| 4432 | { |
| 4433 | int err, max; |
| 4434 | |
| 4435 | if (!PyArg_ParseTuple(args, "i:set_lk_max_lockers", &max)) |
| 4436 | return NULL; |
| 4437 | CHECK_ENV_NOT_CLOSED(self); |
| 4438 | |
| 4439 | MYDB_BEGIN_ALLOW_THREADS; |
| 4440 | err = self->db_env->set_lk_max_lockers(self->db_env, max); |
| 4441 | MYDB_END_ALLOW_THREADS; |
| 4442 | RETURN_IF_ERR(); |
| 4443 | RETURN_NONE(); |
| 4444 | } |
| 4445 | |
| 4446 | |
| 4447 | static PyObject* |
| 4448 | DBEnv_set_lk_max_objects(DBEnvObject* self, PyObject* args) |
| 4449 | { |
| 4450 | int err, max; |
| 4451 | |
| 4452 | if (!PyArg_ParseTuple(args, "i:set_lk_max_objects", &max)) |
| 4453 | return NULL; |
| 4454 | CHECK_ENV_NOT_CLOSED(self); |
| 4455 | |
| 4456 | MYDB_BEGIN_ALLOW_THREADS; |
| 4457 | err = self->db_env->set_lk_max_objects(self->db_env, max); |
| 4458 | MYDB_END_ALLOW_THREADS; |
| 4459 | RETURN_IF_ERR(); |
| 4460 | RETURN_NONE(); |
| 4461 | } |
| 4462 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4463 | |
| 4464 | static PyObject* |
| 4465 | DBEnv_set_mp_mmapsize(DBEnvObject* self, PyObject* args) |
| 4466 | { |
| 4467 | int err, mp_mmapsize; |
| 4468 | |
| 4469 | if (!PyArg_ParseTuple(args, "i:set_mp_mmapsize", &mp_mmapsize)) |
| 4470 | return NULL; |
| 4471 | CHECK_ENV_NOT_CLOSED(self); |
| 4472 | |
| 4473 | MYDB_BEGIN_ALLOW_THREADS; |
| 4474 | err = self->db_env->set_mp_mmapsize(self->db_env, mp_mmapsize); |
| 4475 | MYDB_END_ALLOW_THREADS; |
| 4476 | RETURN_IF_ERR(); |
| 4477 | RETURN_NONE(); |
| 4478 | } |
| 4479 | |
| 4480 | |
| 4481 | static PyObject* |
| 4482 | DBEnv_set_tmp_dir(DBEnvObject* self, PyObject* args) |
| 4483 | { |
| 4484 | int err; |
| 4485 | char *dir; |
| 4486 | |
| 4487 | if (!PyArg_ParseTuple(args, "s:set_tmp_dir", &dir)) |
| 4488 | return NULL; |
| 4489 | CHECK_ENV_NOT_CLOSED(self); |
| 4490 | |
| 4491 | MYDB_BEGIN_ALLOW_THREADS; |
| 4492 | err = self->db_env->set_tmp_dir(self->db_env, dir); |
| 4493 | MYDB_END_ALLOW_THREADS; |
| 4494 | RETURN_IF_ERR(); |
| 4495 | RETURN_NONE(); |
| 4496 | } |
| 4497 | |
| 4498 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4499 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 4500 | DBEnv_txn_recover(DBEnvObject* self) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4501 | { |
| 4502 | int flags = DB_FIRST; |
| 4503 | int err, i; |
| 4504 | PyObject *list, *tuple, *gid; |
| 4505 | DBTxnObject *txn; |
| 4506 | #define PREPLIST_LEN 16 |
| 4507 | DB_PREPLIST preplist[PREPLIST_LEN]; |
Matthias Klose | 54cc539 | 2010-03-15 12:46:18 +0000 | [diff] [blame] | 4508 | #if (DBVER < 48) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4509 | long retp; |
Matthias Klose | 54cc539 | 2010-03-15 12:46:18 +0000 | [diff] [blame] | 4510 | #else |
| 4511 | u_int32_t retp; |
| 4512 | #endif |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4513 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4514 | CHECK_ENV_NOT_CLOSED(self); |
| 4515 | |
| 4516 | list=PyList_New(0); |
| 4517 | if (!list) |
| 4518 | return NULL; |
| 4519 | while (!0) { |
| 4520 | MYDB_BEGIN_ALLOW_THREADS |
| 4521 | err=self->db_env->txn_recover(self->db_env, |
| 4522 | preplist, PREPLIST_LEN, &retp, flags); |
| 4523 | #undef PREPLIST_LEN |
| 4524 | MYDB_END_ALLOW_THREADS |
| 4525 | if (err) { |
| 4526 | Py_DECREF(list); |
| 4527 | RETURN_IF_ERR(); |
| 4528 | } |
| 4529 | if (!retp) break; |
| 4530 | flags=DB_NEXT; /* Prepare for next loop pass */ |
| 4531 | for (i=0; i<retp; i++) { |
Christian Heimes | 593daf5 | 2008-05-26 12:51:38 +0000 | [diff] [blame] | 4532 | gid=PyBytes_FromStringAndSize((char *)(preplist[i].gid), |
Matthias Klose | 54cc539 | 2010-03-15 12:46:18 +0000 | [diff] [blame] | 4533 | DB_GID_SIZE); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4534 | if (!gid) { |
| 4535 | Py_DECREF(list); |
| 4536 | return NULL; |
| 4537 | } |
| 4538 | txn=newDBTxnObject(self, NULL, preplist[i].txn, flags); |
| 4539 | if (!txn) { |
| 4540 | Py_DECREF(list); |
| 4541 | Py_DECREF(gid); |
| 4542 | return NULL; |
| 4543 | } |
| 4544 | txn->flag_prepare=1; /* Recover state */ |
| 4545 | tuple=PyTuple_New(2); |
| 4546 | if (!tuple) { |
| 4547 | Py_DECREF(list); |
| 4548 | Py_DECREF(gid); |
| 4549 | Py_DECREF(txn); |
| 4550 | return NULL; |
| 4551 | } |
| 4552 | if (PyTuple_SetItem(tuple, 0, gid)) { |
| 4553 | Py_DECREF(list); |
| 4554 | Py_DECREF(gid); |
| 4555 | Py_DECREF(txn); |
| 4556 | Py_DECREF(tuple); |
| 4557 | return NULL; |
| 4558 | } |
| 4559 | if (PyTuple_SetItem(tuple, 1, (PyObject *)txn)) { |
| 4560 | Py_DECREF(list); |
| 4561 | Py_DECREF(txn); |
| 4562 | Py_DECREF(tuple); /* This delete the "gid" also */ |
| 4563 | return NULL; |
| 4564 | } |
| 4565 | if (PyList_Append(list, tuple)) { |
| 4566 | Py_DECREF(list); |
| 4567 | Py_DECREF(tuple);/* This delete the "gid" and the "txn" also */ |
| 4568 | return NULL; |
| 4569 | } |
| 4570 | Py_DECREF(tuple); |
| 4571 | } |
| 4572 | } |
| 4573 | return list; |
| 4574 | } |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4575 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4576 | static PyObject* |
| 4577 | DBEnv_txn_begin(DBEnvObject* self, PyObject* args, PyObject* kwargs) |
| 4578 | { |
| 4579 | int flags = 0; |
| 4580 | PyObject* txnobj = NULL; |
| 4581 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 4582 | static char* kwnames[] = { "parent", "flags", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4583 | |
| 4584 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oi:txn_begin", kwnames, |
| 4585 | &txnobj, &flags)) |
| 4586 | return NULL; |
| 4587 | |
| 4588 | if (!checkTxnObj(txnobj, &txn)) |
| 4589 | return NULL; |
| 4590 | CHECK_ENV_NOT_CLOSED(self); |
| 4591 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4592 | return (PyObject*)newDBTxnObject(self, (DBTxnObject *)txnobj, NULL, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4593 | } |
| 4594 | |
| 4595 | |
| 4596 | static PyObject* |
| 4597 | DBEnv_txn_checkpoint(DBEnvObject* self, PyObject* args) |
| 4598 | { |
| 4599 | int err, kbyte=0, min=0, flags=0; |
| 4600 | |
| 4601 | if (!PyArg_ParseTuple(args, "|iii:txn_checkpoint", &kbyte, &min, &flags)) |
| 4602 | return NULL; |
| 4603 | CHECK_ENV_NOT_CLOSED(self); |
| 4604 | |
| 4605 | MYDB_BEGIN_ALLOW_THREADS; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4606 | err = self->db_env->txn_checkpoint(self->db_env, kbyte, min, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4607 | MYDB_END_ALLOW_THREADS; |
| 4608 | RETURN_IF_ERR(); |
| 4609 | RETURN_NONE(); |
| 4610 | } |
| 4611 | |
| 4612 | |
| 4613 | static PyObject* |
| 4614 | DBEnv_set_tx_max(DBEnvObject* self, PyObject* args) |
| 4615 | { |
| 4616 | int err, max; |
| 4617 | |
| 4618 | if (!PyArg_ParseTuple(args, "i:set_tx_max", &max)) |
| 4619 | return NULL; |
| 4620 | CHECK_ENV_NOT_CLOSED(self); |
| 4621 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4622 | err = self->db_env->set_tx_max(self->db_env, max); |
Gregory P. Smith | 8a47404 | 2006-01-27 07:05:40 +0000 | [diff] [blame] | 4623 | RETURN_IF_ERR(); |
| 4624 | RETURN_NONE(); |
| 4625 | } |
| 4626 | |
| 4627 | |
| 4628 | static PyObject* |
| 4629 | DBEnv_set_tx_timestamp(DBEnvObject* self, PyObject* args) |
| 4630 | { |
| 4631 | int err; |
Thomas Wouters | 9d63cca | 2006-03-01 01:01:55 +0000 | [diff] [blame] | 4632 | long stamp; |
| 4633 | time_t timestamp; |
Gregory P. Smith | 8a47404 | 2006-01-27 07:05:40 +0000 | [diff] [blame] | 4634 | |
Thomas Wouters | 9d63cca | 2006-03-01 01:01:55 +0000 | [diff] [blame] | 4635 | if (!PyArg_ParseTuple(args, "l:set_tx_timestamp", &stamp)) |
Gregory P. Smith | 8a47404 | 2006-01-27 07:05:40 +0000 | [diff] [blame] | 4636 | return NULL; |
| 4637 | CHECK_ENV_NOT_CLOSED(self); |
Thomas Wouters | 9d63cca | 2006-03-01 01:01:55 +0000 | [diff] [blame] | 4638 | timestamp = (time_t)stamp; |
| 4639 | err = self->db_env->set_tx_timestamp(self->db_env, ×tamp); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4640 | RETURN_IF_ERR(); |
| 4641 | RETURN_NONE(); |
| 4642 | } |
| 4643 | |
| 4644 | |
| 4645 | static PyObject* |
| 4646 | DBEnv_lock_detect(DBEnvObject* self, PyObject* args) |
| 4647 | { |
| 4648 | int err, atype, flags=0; |
| 4649 | int aborted = 0; |
| 4650 | |
| 4651 | if (!PyArg_ParseTuple(args, "i|i:lock_detect", &atype, &flags)) |
| 4652 | return NULL; |
| 4653 | CHECK_ENV_NOT_CLOSED(self); |
| 4654 | |
| 4655 | MYDB_BEGIN_ALLOW_THREADS; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4656 | err = self->db_env->lock_detect(self->db_env, flags, atype, &aborted); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4657 | MYDB_END_ALLOW_THREADS; |
| 4658 | RETURN_IF_ERR(); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 4659 | return NUMBER_FromLong(aborted); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4660 | } |
| 4661 | |
| 4662 | |
| 4663 | static PyObject* |
| 4664 | DBEnv_lock_get(DBEnvObject* self, PyObject* args) |
| 4665 | { |
| 4666 | int flags=0; |
| 4667 | int locker, lock_mode; |
| 4668 | DBT obj; |
| 4669 | PyObject* objobj; |
| 4670 | |
| 4671 | if (!PyArg_ParseTuple(args, "iOi|i:lock_get", &locker, &objobj, &lock_mode, &flags)) |
| 4672 | return NULL; |
| 4673 | |
| 4674 | |
| 4675 | if (!make_dbt(objobj, &obj)) |
| 4676 | return NULL; |
| 4677 | |
| 4678 | return (PyObject*)newDBLockObject(self, locker, &obj, lock_mode, flags); |
| 4679 | } |
| 4680 | |
| 4681 | |
| 4682 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 4683 | DBEnv_lock_id(DBEnvObject* self) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4684 | { |
| 4685 | int err; |
| 4686 | u_int32_t theID; |
| 4687 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4688 | CHECK_ENV_NOT_CLOSED(self); |
| 4689 | MYDB_BEGIN_ALLOW_THREADS; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4690 | err = self->db_env->lock_id(self->db_env, &theID); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4691 | MYDB_END_ALLOW_THREADS; |
| 4692 | RETURN_IF_ERR(); |
| 4693 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 4694 | return NUMBER_FromLong((long)theID); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4695 | } |
| 4696 | |
Gregory P. Smith | ac11e02 | 2007-11-05 02:56:31 +0000 | [diff] [blame] | 4697 | static PyObject* |
| 4698 | DBEnv_lock_id_free(DBEnvObject* self, PyObject* args) |
| 4699 | { |
| 4700 | int err; |
| 4701 | u_int32_t theID; |
| 4702 | |
| 4703 | if (!PyArg_ParseTuple(args, "I:lock_id_free", &theID)) |
| 4704 | return NULL; |
| 4705 | |
| 4706 | CHECK_ENV_NOT_CLOSED(self); |
| 4707 | MYDB_BEGIN_ALLOW_THREADS; |
| 4708 | err = self->db_env->lock_id_free(self->db_env, theID); |
| 4709 | MYDB_END_ALLOW_THREADS; |
| 4710 | RETURN_IF_ERR(); |
| 4711 | RETURN_NONE(); |
| 4712 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4713 | |
| 4714 | static PyObject* |
| 4715 | DBEnv_lock_put(DBEnvObject* self, PyObject* args) |
| 4716 | { |
| 4717 | int err; |
| 4718 | DBLockObject* dblockobj; |
| 4719 | |
| 4720 | if (!PyArg_ParseTuple(args, "O!:lock_put", &DBLock_Type, &dblockobj)) |
| 4721 | return NULL; |
| 4722 | |
| 4723 | CHECK_ENV_NOT_CLOSED(self); |
| 4724 | MYDB_BEGIN_ALLOW_THREADS; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4725 | err = self->db_env->lock_put(self->db_env, &dblockobj->lock); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4726 | MYDB_END_ALLOW_THREADS; |
| 4727 | RETURN_IF_ERR(); |
| 4728 | RETURN_NONE(); |
| 4729 | } |
| 4730 | |
Gregory P. Smith | db8a807 | 2006-06-05 01:56:15 +0000 | [diff] [blame] | 4731 | #if (DBVER >= 44) |
| 4732 | static PyObject* |
| 4733 | DBEnv_lsn_reset(DBEnvObject* self, PyObject* args, PyObject* kwargs) |
| 4734 | { |
| 4735 | int err; |
| 4736 | char *file; |
| 4737 | u_int32_t flags = 0; |
| 4738 | static char* kwnames[] = { "file", "flags", NULL}; |
| 4739 | |
| 4740 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "z|i:lsn_reset", kwnames, |
| 4741 | &file, &flags)) |
| 4742 | return NULL; |
| 4743 | CHECK_ENV_NOT_CLOSED(self); |
| 4744 | |
| 4745 | MYDB_BEGIN_ALLOW_THREADS; |
| 4746 | err = self->db_env->lsn_reset(self->db_env, file, flags); |
| 4747 | MYDB_END_ALLOW_THREADS; |
| 4748 | RETURN_IF_ERR(); |
| 4749 | RETURN_NONE(); |
| 4750 | } |
| 4751 | #endif /* DBVER >= 4.4 */ |
| 4752 | |
Gregory P. Smith | 76a82e8 | 2006-06-05 01:39:52 +0000 | [diff] [blame] | 4753 | static PyObject* |
| 4754 | DBEnv_log_stat(DBEnvObject* self, PyObject* args) |
| 4755 | { |
| 4756 | int err; |
| 4757 | DB_LOG_STAT* statp = NULL; |
| 4758 | PyObject* d = NULL; |
| 4759 | u_int32_t flags = 0; |
| 4760 | |
| 4761 | if (!PyArg_ParseTuple(args, "|i:log_stat", &flags)) |
| 4762 | return NULL; |
| 4763 | CHECK_ENV_NOT_CLOSED(self); |
| 4764 | |
| 4765 | MYDB_BEGIN_ALLOW_THREADS; |
| 4766 | err = self->db_env->log_stat(self->db_env, &statp, flags); |
| 4767 | MYDB_END_ALLOW_THREADS; |
| 4768 | RETURN_IF_ERR(); |
| 4769 | |
| 4770 | /* Turn the stat structure into a dictionary */ |
| 4771 | d = PyDict_New(); |
| 4772 | if (d == NULL) { |
| 4773 | if (statp) |
| 4774 | free(statp); |
| 4775 | return NULL; |
| 4776 | } |
| 4777 | |
| 4778 | #define MAKE_ENTRY(name) _addIntToDict(d, #name, statp->st_##name) |
| 4779 | |
| 4780 | MAKE_ENTRY(magic); |
| 4781 | MAKE_ENTRY(version); |
| 4782 | MAKE_ENTRY(mode); |
| 4783 | MAKE_ENTRY(lg_bsize); |
| 4784 | #if (DBVER >= 44) |
| 4785 | MAKE_ENTRY(lg_size); |
| 4786 | MAKE_ENTRY(record); |
| 4787 | #endif |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 4788 | #if (DBVER < 41) |
Gregory P. Smith | 76a82e8 | 2006-06-05 01:39:52 +0000 | [diff] [blame] | 4789 | MAKE_ENTRY(lg_max); |
| 4790 | #endif |
| 4791 | MAKE_ENTRY(w_mbytes); |
| 4792 | MAKE_ENTRY(w_bytes); |
| 4793 | MAKE_ENTRY(wc_mbytes); |
| 4794 | MAKE_ENTRY(wc_bytes); |
| 4795 | MAKE_ENTRY(wcount); |
| 4796 | MAKE_ENTRY(wcount_fill); |
| 4797 | #if (DBVER >= 44) |
| 4798 | MAKE_ENTRY(rcount); |
| 4799 | #endif |
| 4800 | MAKE_ENTRY(scount); |
| 4801 | MAKE_ENTRY(cur_file); |
| 4802 | MAKE_ENTRY(cur_offset); |
| 4803 | MAKE_ENTRY(disk_file); |
| 4804 | MAKE_ENTRY(disk_offset); |
| 4805 | MAKE_ENTRY(maxcommitperflush); |
| 4806 | MAKE_ENTRY(mincommitperflush); |
| 4807 | MAKE_ENTRY(regsize); |
| 4808 | MAKE_ENTRY(region_wait); |
| 4809 | MAKE_ENTRY(region_nowait); |
| 4810 | |
| 4811 | #undef MAKE_ENTRY |
| 4812 | free(statp); |
| 4813 | return d; |
| 4814 | } /* DBEnv_log_stat */ |
Gregory P. Smith | 76a82e8 | 2006-06-05 01:39:52 +0000 | [diff] [blame] | 4815 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4816 | |
| 4817 | static PyObject* |
| 4818 | DBEnv_lock_stat(DBEnvObject* self, PyObject* args) |
| 4819 | { |
| 4820 | int err; |
| 4821 | DB_LOCK_STAT* sp; |
| 4822 | PyObject* d = NULL; |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 4823 | u_int32_t flags = 0; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4824 | |
| 4825 | if (!PyArg_ParseTuple(args, "|i:lock_stat", &flags)) |
| 4826 | return NULL; |
| 4827 | CHECK_ENV_NOT_CLOSED(self); |
| 4828 | |
| 4829 | MYDB_BEGIN_ALLOW_THREADS; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4830 | err = self->db_env->lock_stat(self->db_env, &sp, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4831 | MYDB_END_ALLOW_THREADS; |
| 4832 | RETURN_IF_ERR(); |
| 4833 | |
| 4834 | /* Turn the stat structure into a dictionary */ |
| 4835 | d = PyDict_New(); |
| 4836 | if (d == NULL) { |
| 4837 | free(sp); |
| 4838 | return NULL; |
| 4839 | } |
| 4840 | |
| 4841 | #define MAKE_ENTRY(name) _addIntToDict(d, #name, sp->st_##name) |
| 4842 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 4843 | #if (DBVER < 41) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4844 | MAKE_ENTRY(lastid); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 4845 | #endif |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4846 | #if (DBVER >=41) |
| 4847 | MAKE_ENTRY(id); |
| 4848 | MAKE_ENTRY(cur_maxid); |
| 4849 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4850 | MAKE_ENTRY(nmodes); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4851 | MAKE_ENTRY(maxlocks); |
| 4852 | MAKE_ENTRY(maxlockers); |
| 4853 | MAKE_ENTRY(maxobjects); |
| 4854 | MAKE_ENTRY(nlocks); |
| 4855 | MAKE_ENTRY(maxnlocks); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4856 | MAKE_ENTRY(nlockers); |
| 4857 | MAKE_ENTRY(maxnlockers); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4858 | MAKE_ENTRY(nobjects); |
| 4859 | MAKE_ENTRY(maxnobjects); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4860 | MAKE_ENTRY(nrequests); |
| 4861 | MAKE_ENTRY(nreleases); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4862 | #if (DBVER >= 44) |
| 4863 | MAKE_ENTRY(nupgrade); |
| 4864 | MAKE_ENTRY(ndowngrade); |
| 4865 | #endif |
Gregory P. Smith | 29602d2 | 2006-01-24 09:46:48 +0000 | [diff] [blame] | 4866 | #if (DBVER < 44) |
| 4867 | MAKE_ENTRY(nnowaits); /* these were renamed in 4.4 */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4868 | MAKE_ENTRY(nconflicts); |
Gregory P. Smith | 29602d2 | 2006-01-24 09:46:48 +0000 | [diff] [blame] | 4869 | #else |
| 4870 | MAKE_ENTRY(lock_nowait); |
| 4871 | MAKE_ENTRY(lock_wait); |
| 4872 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4873 | MAKE_ENTRY(ndeadlocks); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4874 | #if (DBVER >= 41) |
| 4875 | MAKE_ENTRY(locktimeout); |
| 4876 | MAKE_ENTRY(txntimeout); |
| 4877 | #endif |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4878 | MAKE_ENTRY(nlocktimeouts); |
| 4879 | MAKE_ENTRY(ntxntimeouts); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4880 | #if (DBVER >= 46) |
| 4881 | MAKE_ENTRY(objs_wait); |
| 4882 | MAKE_ENTRY(objs_nowait); |
| 4883 | MAKE_ENTRY(lockers_wait); |
| 4884 | MAKE_ENTRY(lockers_nowait); |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 4885 | #if (DBVER >= 47) |
| 4886 | MAKE_ENTRY(lock_wait); |
| 4887 | MAKE_ENTRY(lock_nowait); |
| 4888 | #else |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4889 | MAKE_ENTRY(locks_wait); |
| 4890 | MAKE_ENTRY(locks_nowait); |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 4891 | #endif |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4892 | MAKE_ENTRY(hash_len); |
| 4893 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4894 | MAKE_ENTRY(regsize); |
| 4895 | MAKE_ENTRY(region_wait); |
| 4896 | MAKE_ENTRY(region_nowait); |
| 4897 | |
| 4898 | #undef MAKE_ENTRY |
| 4899 | free(sp); |
| 4900 | return d; |
| 4901 | } |
| 4902 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4903 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 4904 | DBEnv_log_flush(DBEnvObject* self) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4905 | { |
| 4906 | int err; |
| 4907 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4908 | CHECK_ENV_NOT_CLOSED(self); |
| 4909 | |
| 4910 | MYDB_BEGIN_ALLOW_THREADS |
| 4911 | err = self->db_env->log_flush(self->db_env, NULL); |
| 4912 | MYDB_END_ALLOW_THREADS |
| 4913 | |
| 4914 | RETURN_IF_ERR(); |
| 4915 | RETURN_NONE(); |
| 4916 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4917 | |
| 4918 | static PyObject* |
| 4919 | DBEnv_log_archive(DBEnvObject* self, PyObject* args) |
| 4920 | { |
| 4921 | int flags=0; |
| 4922 | int err; |
Gregory P. Smith | 3dd2002 | 2006-06-05 00:31:01 +0000 | [diff] [blame] | 4923 | char **log_list = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4924 | PyObject* list; |
| 4925 | PyObject* item = NULL; |
| 4926 | |
| 4927 | if (!PyArg_ParseTuple(args, "|i:log_archive", &flags)) |
| 4928 | return NULL; |
| 4929 | |
| 4930 | CHECK_ENV_NOT_CLOSED(self); |
| 4931 | MYDB_BEGIN_ALLOW_THREADS; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4932 | err = self->db_env->log_archive(self->db_env, &log_list, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4933 | MYDB_END_ALLOW_THREADS; |
| 4934 | RETURN_IF_ERR(); |
| 4935 | |
Gregory P. Smith | bad4745 | 2006-06-05 00:33:35 +0000 | [diff] [blame] | 4936 | list = PyList_New(0); |
| 4937 | if (list == NULL) { |
| 4938 | if (log_list) |
| 4939 | free(log_list); |
| 4940 | return NULL; |
| 4941 | } |
| 4942 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4943 | if (log_list) { |
Gregory P. Smith | 3dd2002 | 2006-06-05 00:31:01 +0000 | [diff] [blame] | 4944 | char **log_list_start; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4945 | for (log_list_start = log_list; *log_list != NULL; ++log_list) { |
Christian Heimes | 593daf5 | 2008-05-26 12:51:38 +0000 | [diff] [blame] | 4946 | item = PyBytes_FromString (*log_list); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4947 | if (item == NULL) { |
| 4948 | Py_DECREF(list); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4949 | list = NULL; |
| 4950 | break; |
| 4951 | } |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 4952 | if (PyList_Append(list, item)) { |
| 4953 | Py_DECREF(list); |
| 4954 | list = NULL; |
| 4955 | Py_DECREF(item); |
| 4956 | break; |
| 4957 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4958 | Py_DECREF(item); |
| 4959 | } |
| 4960 | free(log_list_start); |
| 4961 | } |
| 4962 | return list; |
| 4963 | } |
| 4964 | |
| 4965 | |
| 4966 | static PyObject* |
| 4967 | DBEnv_txn_stat(DBEnvObject* self, PyObject* args) |
| 4968 | { |
| 4969 | int err; |
| 4970 | DB_TXN_STAT* sp; |
| 4971 | PyObject* d = NULL; |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 4972 | u_int32_t flags=0; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4973 | |
| 4974 | if (!PyArg_ParseTuple(args, "|i:txn_stat", &flags)) |
| 4975 | return NULL; |
| 4976 | CHECK_ENV_NOT_CLOSED(self); |
| 4977 | |
| 4978 | MYDB_BEGIN_ALLOW_THREADS; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4979 | err = self->db_env->txn_stat(self->db_env, &sp, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4980 | MYDB_END_ALLOW_THREADS; |
| 4981 | RETURN_IF_ERR(); |
| 4982 | |
| 4983 | /* Turn the stat structure into a dictionary */ |
| 4984 | d = PyDict_New(); |
| 4985 | if (d == NULL) { |
| 4986 | free(sp); |
| 4987 | return NULL; |
| 4988 | } |
| 4989 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4990 | #define MAKE_ENTRY(name) _addIntToDict(d, #name, sp->st_##name) |
| 4991 | #define MAKE_TIME_T_ENTRY(name) _addTimeTToDict(d, #name, sp->st_##name) |
| 4992 | #define MAKE_DB_LSN_ENTRY(name) _addDB_lsnToDict(d, #name, sp->st_##name) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4993 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 4994 | MAKE_DB_LSN_ENTRY(last_ckp); |
Kristján Valur Jónsson | bd77c03 | 2007-04-26 15:24:54 +0000 | [diff] [blame] | 4995 | MAKE_TIME_T_ENTRY(time_ckp); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4996 | MAKE_ENTRY(last_txnid); |
| 4997 | MAKE_ENTRY(maxtxns); |
| 4998 | MAKE_ENTRY(nactive); |
| 4999 | MAKE_ENTRY(maxnactive); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5000 | #if (DBVER >= 45) |
| 5001 | MAKE_ENTRY(nsnapshot); |
| 5002 | MAKE_ENTRY(maxnsnapshot); |
| 5003 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5004 | MAKE_ENTRY(nbegins); |
| 5005 | MAKE_ENTRY(naborts); |
| 5006 | MAKE_ENTRY(ncommits); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5007 | MAKE_ENTRY(nrestores); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5008 | MAKE_ENTRY(regsize); |
| 5009 | MAKE_ENTRY(region_wait); |
| 5010 | MAKE_ENTRY(region_nowait); |
| 5011 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5012 | #undef MAKE_DB_LSN_ENTRY |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5013 | #undef MAKE_ENTRY |
Kristján Valur Jónsson | bd77c03 | 2007-04-26 15:24:54 +0000 | [diff] [blame] | 5014 | #undef MAKE_TIME_T_ENTRY |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5015 | free(sp); |
| 5016 | return d; |
| 5017 | } |
| 5018 | |
| 5019 | |
| 5020 | static PyObject* |
| 5021 | DBEnv_set_get_returns_none(DBEnvObject* self, PyObject* args) |
| 5022 | { |
| 5023 | int flags=0; |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 5024 | int oldValue=0; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5025 | |
| 5026 | if (!PyArg_ParseTuple(args,"i:set_get_returns_none", &flags)) |
| 5027 | return NULL; |
| 5028 | CHECK_ENV_NOT_CLOSED(self); |
| 5029 | |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 5030 | if (self->moduleFlags.getReturnsNone) |
| 5031 | ++oldValue; |
| 5032 | if (self->moduleFlags.cursorSetReturnsNone) |
| 5033 | ++oldValue; |
| 5034 | self->moduleFlags.getReturnsNone = (flags >= 1); |
| 5035 | self->moduleFlags.cursorSetReturnsNone = (flags >= 2); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5036 | return NUMBER_FromLong(oldValue); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5037 | } |
| 5038 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5039 | static PyObject* |
| 5040 | DBEnv_get_private(DBEnvObject* self) |
| 5041 | { |
| 5042 | /* We can give out the private field even if dbenv is closed */ |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 5043 | Py_INCREF(self->private_obj); |
| 5044 | return self->private_obj; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5045 | } |
| 5046 | |
| 5047 | static PyObject* |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 5048 | DBEnv_set_private(DBEnvObject* self, PyObject* private_obj) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5049 | { |
| 5050 | /* We can set the private field even if dbenv is closed */ |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 5051 | Py_DECREF(self->private_obj); |
| 5052 | Py_INCREF(private_obj); |
| 5053 | self->private_obj = private_obj; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5054 | RETURN_NONE(); |
| 5055 | } |
| 5056 | |
| 5057 | |
Matthias Klose | 54cc539 | 2010-03-15 12:46:18 +0000 | [diff] [blame] | 5058 | #if (DBVER < 48) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5059 | static PyObject* |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 5060 | DBEnv_set_rpc_server(DBEnvObject* self, PyObject* args, PyObject* kwargs) |
| 5061 | { |
| 5062 | int err; |
| 5063 | char *host; |
| 5064 | long cl_timeout=0, sv_timeout=0; |
| 5065 | |
| 5066 | static char* kwnames[] = { "host", "cl_timeout", "sv_timeout", NULL}; |
| 5067 | |
| 5068 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|ll:set_rpc_server", kwnames, |
| 5069 | &host, &cl_timeout, &sv_timeout)) |
| 5070 | return NULL; |
| 5071 | CHECK_ENV_NOT_CLOSED(self); |
| 5072 | |
| 5073 | MYDB_BEGIN_ALLOW_THREADS; |
| 5074 | err = self->db_env->set_rpc_server(self->db_env, NULL, host, cl_timeout, |
| 5075 | sv_timeout, 0); |
| 5076 | MYDB_END_ALLOW_THREADS; |
| 5077 | RETURN_IF_ERR(); |
| 5078 | RETURN_NONE(); |
| 5079 | } |
Matthias Klose | 54cc539 | 2010-03-15 12:46:18 +0000 | [diff] [blame] | 5080 | #endif |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 5081 | |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 5082 | static PyObject* |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5083 | DBEnv_set_verbose(DBEnvObject* self, PyObject* args) |
| 5084 | { |
| 5085 | int err; |
| 5086 | int which, onoff; |
| 5087 | |
| 5088 | if (!PyArg_ParseTuple(args, "ii:set_verbose", &which, &onoff)) { |
| 5089 | return NULL; |
| 5090 | } |
| 5091 | CHECK_ENV_NOT_CLOSED(self); |
| 5092 | MYDB_BEGIN_ALLOW_THREADS; |
| 5093 | err = self->db_env->set_verbose(self->db_env, which, onoff); |
| 5094 | MYDB_END_ALLOW_THREADS; |
| 5095 | RETURN_IF_ERR(); |
| 5096 | RETURN_NONE(); |
| 5097 | } |
| 5098 | |
| 5099 | #if (DBVER >= 42) |
| 5100 | static PyObject* |
| 5101 | DBEnv_get_verbose(DBEnvObject* self, PyObject* args) |
| 5102 | { |
| 5103 | int err; |
| 5104 | int which; |
| 5105 | int verbose; |
| 5106 | |
| 5107 | if (!PyArg_ParseTuple(args, "i:get_verbose", &which)) { |
| 5108 | return NULL; |
| 5109 | } |
| 5110 | CHECK_ENV_NOT_CLOSED(self); |
| 5111 | MYDB_BEGIN_ALLOW_THREADS; |
| 5112 | err = self->db_env->get_verbose(self->db_env, which, &verbose); |
| 5113 | MYDB_END_ALLOW_THREADS; |
| 5114 | RETURN_IF_ERR(); |
| 5115 | return PyBool_FromLong(verbose); |
| 5116 | } |
| 5117 | #endif |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5118 | |
| 5119 | #if (DBVER >= 45) |
| 5120 | static void |
| 5121 | _dbenv_event_notifyCallback(DB_ENV* db_env, u_int32_t event, void *event_info) |
| 5122 | { |
| 5123 | DBEnvObject *dbenv; |
| 5124 | PyObject* callback; |
| 5125 | PyObject* args; |
| 5126 | PyObject* result = NULL; |
| 5127 | |
| 5128 | MYDB_BEGIN_BLOCK_THREADS; |
| 5129 | dbenv = (DBEnvObject *)db_env->app_private; |
| 5130 | callback = dbenv->event_notifyCallback; |
| 5131 | if (callback) { |
| 5132 | if (event == DB_EVENT_REP_NEWMASTER) { |
| 5133 | args = Py_BuildValue("(Oii)", dbenv, event, *((int *)event_info)); |
| 5134 | } else { |
| 5135 | args = Py_BuildValue("(OiO)", dbenv, event, Py_None); |
| 5136 | } |
| 5137 | if (args) { |
| 5138 | result = PyEval_CallObject(callback, args); |
| 5139 | } |
| 5140 | if ((!args) || (!result)) { |
| 5141 | PyErr_Print(); |
| 5142 | } |
| 5143 | Py_XDECREF(args); |
| 5144 | Py_XDECREF(result); |
| 5145 | } |
| 5146 | MYDB_END_BLOCK_THREADS; |
| 5147 | } |
| 5148 | #endif |
| 5149 | |
| 5150 | #if (DBVER >= 45) |
| 5151 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5152 | DBEnv_set_event_notify(DBEnvObject* self, PyObject* notifyFunc) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5153 | { |
| 5154 | int err; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5155 | |
| 5156 | CHECK_ENV_NOT_CLOSED(self); |
| 5157 | |
| 5158 | if (!PyCallable_Check(notifyFunc)) { |
| 5159 | makeTypeError("Callable", notifyFunc); |
| 5160 | return NULL; |
| 5161 | } |
| 5162 | |
| 5163 | Py_XDECREF(self->event_notifyCallback); |
| 5164 | Py_INCREF(notifyFunc); |
| 5165 | self->event_notifyCallback = notifyFunc; |
| 5166 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5167 | /* This is to workaround a problem with un-initialized threads (see |
| 5168 | comment in DB_associate) */ |
| 5169 | #ifdef WITH_THREAD |
| 5170 | PyEval_InitThreads(); |
| 5171 | #endif |
| 5172 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5173 | MYDB_BEGIN_ALLOW_THREADS; |
| 5174 | err = self->db_env->set_event_notify(self->db_env, _dbenv_event_notifyCallback); |
| 5175 | MYDB_END_ALLOW_THREADS; |
| 5176 | |
| 5177 | if (err) { |
| 5178 | Py_DECREF(notifyFunc); |
| 5179 | self->event_notifyCallback = NULL; |
| 5180 | } |
| 5181 | |
| 5182 | RETURN_IF_ERR(); |
| 5183 | RETURN_NONE(); |
| 5184 | } |
| 5185 | #endif |
| 5186 | |
| 5187 | |
| 5188 | /* --------------------------------------------------------------------- */ |
| 5189 | /* REPLICATION METHODS: Base Replication */ |
| 5190 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5191 | |
| 5192 | static PyObject* |
| 5193 | DBEnv_rep_process_message(DBEnvObject* self, PyObject* args) |
| 5194 | { |
| 5195 | int err; |
| 5196 | PyObject *control_py, *rec_py; |
| 5197 | DBT control, rec; |
| 5198 | int envid; |
| 5199 | #if (DBVER >= 42) |
| 5200 | DB_LSN lsn; |
| 5201 | #endif |
| 5202 | |
| 5203 | if (!PyArg_ParseTuple(args, "OOi:rep_process_message", &control_py, |
| 5204 | &rec_py, &envid)) |
| 5205 | return NULL; |
| 5206 | CHECK_ENV_NOT_CLOSED(self); |
| 5207 | |
| 5208 | if (!make_dbt(control_py, &control)) |
| 5209 | return NULL; |
| 5210 | if (!make_dbt(rec_py, &rec)) |
| 5211 | return NULL; |
| 5212 | |
| 5213 | MYDB_BEGIN_ALLOW_THREADS; |
| 5214 | #if (DBVER >= 46) |
| 5215 | err = self->db_env->rep_process_message(self->db_env, &control, &rec, |
| 5216 | envid, &lsn); |
| 5217 | #else |
| 5218 | #if (DBVER >= 42) |
| 5219 | err = self->db_env->rep_process_message(self->db_env, &control, &rec, |
| 5220 | &envid, &lsn); |
| 5221 | #else |
| 5222 | err = self->db_env->rep_process_message(self->db_env, &control, &rec, |
| 5223 | &envid); |
| 5224 | #endif |
| 5225 | #endif |
| 5226 | MYDB_END_ALLOW_THREADS; |
| 5227 | switch (err) { |
| 5228 | case DB_REP_NEWMASTER : |
| 5229 | return Py_BuildValue("(iO)", envid, Py_None); |
| 5230 | break; |
| 5231 | |
| 5232 | case DB_REP_DUPMASTER : |
| 5233 | case DB_REP_HOLDELECTION : |
| 5234 | #if (DBVER >= 44) |
| 5235 | case DB_REP_IGNORE : |
| 5236 | case DB_REP_JOIN_FAILURE : |
| 5237 | #endif |
| 5238 | return Py_BuildValue("(iO)", err, Py_None); |
| 5239 | break; |
| 5240 | case DB_REP_NEWSITE : |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 5241 | { |
| 5242 | PyObject *tmp, *r; |
| 5243 | |
| 5244 | if (!(tmp = PyBytes_FromStringAndSize(rec.data, rec.size))) { |
| 5245 | return NULL; |
| 5246 | } |
| 5247 | |
| 5248 | r = Py_BuildValue("(iO)", err, tmp); |
| 5249 | Py_DECREF(tmp); |
| 5250 | return r; |
| 5251 | break; |
| 5252 | } |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5253 | #if (DBVER >= 42) |
| 5254 | case DB_REP_NOTPERM : |
| 5255 | case DB_REP_ISPERM : |
| 5256 | return Py_BuildValue("(i(ll))", err, lsn.file, lsn.offset); |
| 5257 | break; |
| 5258 | #endif |
| 5259 | } |
| 5260 | RETURN_IF_ERR(); |
| 5261 | return Py_BuildValue("(OO)", Py_None, Py_None); |
| 5262 | } |
| 5263 | |
| 5264 | static int |
| 5265 | _DBEnv_rep_transportCallback(DB_ENV* db_env, const DBT* control, const DBT* rec, |
| 5266 | const DB_LSN *lsn, int envid, u_int32_t flags) |
| 5267 | { |
| 5268 | DBEnvObject *dbenv; |
| 5269 | PyObject* rep_transport; |
| 5270 | PyObject* args; |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 5271 | PyObject *a, *b; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5272 | PyObject* result = NULL; |
| 5273 | int ret=0; |
| 5274 | |
| 5275 | MYDB_BEGIN_BLOCK_THREADS; |
| 5276 | dbenv = (DBEnvObject *)db_env->app_private; |
| 5277 | rep_transport = dbenv->rep_transport; |
| 5278 | |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 5279 | /* |
| 5280 | ** The errors in 'a' or 'b' are detected in "Py_BuildValue". |
| 5281 | */ |
| 5282 | a = PyBytes_FromStringAndSize(control->data, control->size); |
| 5283 | b = PyBytes_FromStringAndSize(rec->data, rec->size); |
| 5284 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5285 | args = Py_BuildValue( |
| 5286 | #if (PY_VERSION_HEX >= 0x02040000) |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 5287 | "(OOO(ll)iI)", |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5288 | #else |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 5289 | "(OOO(ll)ii)", |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5290 | #endif |
| 5291 | dbenv, |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 5292 | a, b, |
| 5293 | lsn->file, lsn->offset, envid, flags); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5294 | if (args) { |
| 5295 | result = PyEval_CallObject(rep_transport, args); |
| 5296 | } |
| 5297 | |
| 5298 | if ((!args) || (!result)) { |
| 5299 | PyErr_Print(); |
| 5300 | ret = -1; |
| 5301 | } |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 5302 | Py_XDECREF(a); |
| 5303 | Py_XDECREF(b); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5304 | Py_XDECREF(args); |
| 5305 | Py_XDECREF(result); |
| 5306 | MYDB_END_BLOCK_THREADS; |
| 5307 | return ret; |
| 5308 | } |
| 5309 | |
| 5310 | #if (DBVER <= 41) |
| 5311 | static int |
| 5312 | _DBEnv_rep_transportCallbackOLD(DB_ENV* db_env, const DBT* control, const DBT* rec, |
| 5313 | int envid, u_int32_t flags) |
| 5314 | { |
| 5315 | DB_LSN lsn; |
| 5316 | |
| 5317 | lsn.file = -1; /* Dummy values */ |
| 5318 | lsn.offset = -1; |
| 5319 | return _DBEnv_rep_transportCallback(db_env, control, rec, &lsn, envid, |
| 5320 | flags); |
| 5321 | } |
| 5322 | #endif |
| 5323 | |
| 5324 | static PyObject* |
| 5325 | DBEnv_rep_set_transport(DBEnvObject* self, PyObject* args) |
| 5326 | { |
| 5327 | int err; |
| 5328 | int envid; |
| 5329 | PyObject *rep_transport; |
| 5330 | |
| 5331 | if (!PyArg_ParseTuple(args, "iO:rep_set_transport", &envid, &rep_transport)) |
| 5332 | return NULL; |
| 5333 | CHECK_ENV_NOT_CLOSED(self); |
| 5334 | if (!PyCallable_Check(rep_transport)) { |
| 5335 | makeTypeError("Callable", rep_transport); |
| 5336 | return NULL; |
| 5337 | } |
| 5338 | |
| 5339 | MYDB_BEGIN_ALLOW_THREADS; |
| 5340 | #if (DBVER >=45) |
| 5341 | err = self->db_env->rep_set_transport(self->db_env, envid, |
| 5342 | &_DBEnv_rep_transportCallback); |
| 5343 | #else |
| 5344 | #if (DBVER >= 42) |
| 5345 | err = self->db_env->set_rep_transport(self->db_env, envid, |
| 5346 | &_DBEnv_rep_transportCallback); |
| 5347 | #else |
| 5348 | err = self->db_env->set_rep_transport(self->db_env, envid, |
| 5349 | &_DBEnv_rep_transportCallbackOLD); |
| 5350 | #endif |
| 5351 | #endif |
| 5352 | MYDB_END_ALLOW_THREADS; |
| 5353 | RETURN_IF_ERR(); |
| 5354 | |
| 5355 | Py_DECREF(self->rep_transport); |
| 5356 | Py_INCREF(rep_transport); |
| 5357 | self->rep_transport = rep_transport; |
| 5358 | RETURN_NONE(); |
| 5359 | } |
| 5360 | |
| 5361 | #if (DBVER >= 47) |
| 5362 | static PyObject* |
| 5363 | DBEnv_rep_set_request(DBEnvObject* self, PyObject* args) |
| 5364 | { |
| 5365 | int err; |
| 5366 | unsigned int minimum, maximum; |
| 5367 | |
| 5368 | if (!PyArg_ParseTuple(args,"II:rep_set_request", &minimum, &maximum)) |
| 5369 | return NULL; |
| 5370 | CHECK_ENV_NOT_CLOSED(self); |
| 5371 | |
| 5372 | MYDB_BEGIN_ALLOW_THREADS; |
| 5373 | err = self->db_env->rep_set_request(self->db_env, minimum, maximum); |
| 5374 | MYDB_END_ALLOW_THREADS; |
| 5375 | RETURN_IF_ERR(); |
| 5376 | RETURN_NONE(); |
| 5377 | } |
| 5378 | |
| 5379 | static PyObject* |
| 5380 | DBEnv_rep_get_request(DBEnvObject* self) |
| 5381 | { |
| 5382 | int err; |
| 5383 | u_int32_t minimum, maximum; |
| 5384 | |
| 5385 | CHECK_ENV_NOT_CLOSED(self); |
| 5386 | MYDB_BEGIN_ALLOW_THREADS; |
| 5387 | err = self->db_env->rep_get_request(self->db_env, &minimum, &maximum); |
| 5388 | MYDB_END_ALLOW_THREADS; |
| 5389 | RETURN_IF_ERR(); |
| 5390 | #if (PY_VERSION_HEX >= 0x02040000) |
| 5391 | return Py_BuildValue("II", minimum, maximum); |
| 5392 | #else |
| 5393 | return Py_BuildValue("ii", minimum, maximum); |
| 5394 | #endif |
| 5395 | } |
| 5396 | #endif |
| 5397 | |
| 5398 | #if (DBVER >= 45) |
| 5399 | static PyObject* |
| 5400 | DBEnv_rep_set_limit(DBEnvObject* self, PyObject* args) |
| 5401 | { |
| 5402 | int err; |
| 5403 | int limit; |
| 5404 | |
| 5405 | if (!PyArg_ParseTuple(args,"i:rep_set_limit", &limit)) |
| 5406 | return NULL; |
| 5407 | CHECK_ENV_NOT_CLOSED(self); |
| 5408 | |
| 5409 | MYDB_BEGIN_ALLOW_THREADS; |
| 5410 | err = self->db_env->rep_set_limit(self->db_env, 0, limit); |
| 5411 | MYDB_END_ALLOW_THREADS; |
| 5412 | RETURN_IF_ERR(); |
| 5413 | RETURN_NONE(); |
| 5414 | } |
| 5415 | |
| 5416 | static PyObject* |
| 5417 | DBEnv_rep_get_limit(DBEnvObject* self) |
| 5418 | { |
| 5419 | int err; |
| 5420 | u_int32_t gbytes, bytes; |
| 5421 | |
| 5422 | CHECK_ENV_NOT_CLOSED(self); |
| 5423 | MYDB_BEGIN_ALLOW_THREADS; |
| 5424 | err = self->db_env->rep_get_limit(self->db_env, &gbytes, &bytes); |
| 5425 | MYDB_END_ALLOW_THREADS; |
| 5426 | RETURN_IF_ERR(); |
| 5427 | return NUMBER_FromLong(bytes); |
| 5428 | } |
| 5429 | #endif |
| 5430 | |
| 5431 | #if (DBVER >= 44) |
| 5432 | static PyObject* |
| 5433 | DBEnv_rep_set_config(DBEnvObject* self, PyObject* args) |
| 5434 | { |
| 5435 | int err; |
| 5436 | int which; |
| 5437 | int onoff; |
| 5438 | |
| 5439 | if (!PyArg_ParseTuple(args,"ii:rep_set_config", &which, &onoff)) |
| 5440 | return NULL; |
| 5441 | CHECK_ENV_NOT_CLOSED(self); |
| 5442 | |
| 5443 | MYDB_BEGIN_ALLOW_THREADS; |
| 5444 | err = self->db_env->rep_set_config(self->db_env, which, onoff); |
| 5445 | MYDB_END_ALLOW_THREADS; |
| 5446 | RETURN_IF_ERR(); |
| 5447 | RETURN_NONE(); |
| 5448 | } |
| 5449 | |
| 5450 | static PyObject* |
| 5451 | DBEnv_rep_get_config(DBEnvObject* self, PyObject* args) |
| 5452 | { |
| 5453 | int err; |
| 5454 | int which; |
| 5455 | int onoff; |
| 5456 | |
| 5457 | if (!PyArg_ParseTuple(args, "i:rep_get_config", &which)) { |
| 5458 | return NULL; |
| 5459 | } |
| 5460 | CHECK_ENV_NOT_CLOSED(self); |
| 5461 | MYDB_BEGIN_ALLOW_THREADS; |
| 5462 | err = self->db_env->rep_get_config(self->db_env, which, &onoff); |
| 5463 | MYDB_END_ALLOW_THREADS; |
| 5464 | RETURN_IF_ERR(); |
| 5465 | return PyBool_FromLong(onoff); |
| 5466 | } |
| 5467 | #endif |
| 5468 | |
| 5469 | #if (DBVER >= 46) |
| 5470 | static PyObject* |
| 5471 | DBEnv_rep_elect(DBEnvObject* self, PyObject* args) |
| 5472 | { |
| 5473 | int err; |
| 5474 | u_int32_t nsites, nvotes; |
| 5475 | |
| 5476 | if (!PyArg_ParseTuple(args, "II:rep_elect", &nsites, &nvotes)) { |
| 5477 | return NULL; |
| 5478 | } |
| 5479 | CHECK_ENV_NOT_CLOSED(self); |
| 5480 | MYDB_BEGIN_ALLOW_THREADS; |
| 5481 | err = self->db_env->rep_elect(self->db_env, nvotes, nvotes, 0); |
| 5482 | MYDB_END_ALLOW_THREADS; |
| 5483 | RETURN_IF_ERR(); |
| 5484 | RETURN_NONE(); |
| 5485 | } |
| 5486 | #endif |
| 5487 | |
| 5488 | static PyObject* |
| 5489 | DBEnv_rep_start(DBEnvObject* self, PyObject* args, PyObject* kwargs) |
| 5490 | { |
| 5491 | int err; |
| 5492 | PyObject *cdata_py = Py_None; |
| 5493 | DBT cdata; |
| 5494 | int flags; |
| 5495 | static char* kwnames[] = {"flags","cdata", NULL}; |
| 5496 | |
| 5497 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 5498 | "i|O:rep_start", kwnames, &flags, &cdata_py)) |
| 5499 | { |
| 5500 | return NULL; |
| 5501 | } |
| 5502 | CHECK_ENV_NOT_CLOSED(self); |
| 5503 | |
| 5504 | if (!make_dbt(cdata_py, &cdata)) |
| 5505 | return NULL; |
| 5506 | |
| 5507 | MYDB_BEGIN_ALLOW_THREADS; |
| 5508 | err = self->db_env->rep_start(self->db_env, cdata.size ? &cdata : NULL, |
| 5509 | flags); |
| 5510 | MYDB_END_ALLOW_THREADS; |
| 5511 | RETURN_IF_ERR(); |
| 5512 | RETURN_NONE(); |
| 5513 | } |
| 5514 | |
| 5515 | #if (DBVER >= 44) |
| 5516 | static PyObject* |
| 5517 | DBEnv_rep_sync(DBEnvObject* self) |
| 5518 | { |
| 5519 | int err; |
| 5520 | |
| 5521 | CHECK_ENV_NOT_CLOSED(self); |
| 5522 | MYDB_BEGIN_ALLOW_THREADS; |
| 5523 | err = self->db_env->rep_sync(self->db_env, 0); |
| 5524 | MYDB_END_ALLOW_THREADS; |
| 5525 | RETURN_IF_ERR(); |
| 5526 | RETURN_NONE(); |
| 5527 | } |
| 5528 | #endif |
| 5529 | |
| 5530 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5531 | #if (DBVER >= 45) |
| 5532 | static PyObject* |
| 5533 | DBEnv_rep_set_nsites(DBEnvObject* self, PyObject* args) |
| 5534 | { |
| 5535 | int err; |
| 5536 | int nsites; |
| 5537 | |
| 5538 | if (!PyArg_ParseTuple(args, "i:rep_set_nsites", &nsites)) { |
| 5539 | return NULL; |
| 5540 | } |
| 5541 | CHECK_ENV_NOT_CLOSED(self); |
| 5542 | MYDB_BEGIN_ALLOW_THREADS; |
| 5543 | err = self->db_env->rep_set_nsites(self->db_env, nsites); |
| 5544 | MYDB_END_ALLOW_THREADS; |
| 5545 | RETURN_IF_ERR(); |
| 5546 | RETURN_NONE(); |
| 5547 | } |
| 5548 | |
| 5549 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5550 | DBEnv_rep_get_nsites(DBEnvObject* self) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5551 | { |
| 5552 | int err; |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 5553 | #if (DBVER >= 47) |
| 5554 | u_int32_t nsites; |
| 5555 | #else |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5556 | int nsites; |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 5557 | #endif |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5558 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5559 | CHECK_ENV_NOT_CLOSED(self); |
| 5560 | MYDB_BEGIN_ALLOW_THREADS; |
| 5561 | err = self->db_env->rep_get_nsites(self->db_env, &nsites); |
| 5562 | MYDB_END_ALLOW_THREADS; |
| 5563 | RETURN_IF_ERR(); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5564 | return NUMBER_FromLong(nsites); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5565 | } |
| 5566 | |
| 5567 | static PyObject* |
| 5568 | DBEnv_rep_set_priority(DBEnvObject* self, PyObject* args) |
| 5569 | { |
| 5570 | int err; |
| 5571 | int priority; |
| 5572 | |
| 5573 | if (!PyArg_ParseTuple(args, "i:rep_set_priority", &priority)) { |
| 5574 | return NULL; |
| 5575 | } |
| 5576 | CHECK_ENV_NOT_CLOSED(self); |
| 5577 | MYDB_BEGIN_ALLOW_THREADS; |
| 5578 | err = self->db_env->rep_set_priority(self->db_env, priority); |
| 5579 | MYDB_END_ALLOW_THREADS; |
| 5580 | RETURN_IF_ERR(); |
| 5581 | RETURN_NONE(); |
| 5582 | } |
| 5583 | |
| 5584 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5585 | DBEnv_rep_get_priority(DBEnvObject* self) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5586 | { |
| 5587 | int err; |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 5588 | #if (DBVER >= 47) |
| 5589 | u_int32_t priority; |
| 5590 | #else |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5591 | int priority; |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 5592 | #endif |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5593 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5594 | CHECK_ENV_NOT_CLOSED(self); |
| 5595 | MYDB_BEGIN_ALLOW_THREADS; |
| 5596 | err = self->db_env->rep_get_priority(self->db_env, &priority); |
| 5597 | MYDB_END_ALLOW_THREADS; |
| 5598 | RETURN_IF_ERR(); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5599 | return NUMBER_FromLong(priority); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5600 | } |
| 5601 | |
| 5602 | static PyObject* |
| 5603 | DBEnv_rep_set_timeout(DBEnvObject* self, PyObject* args) |
| 5604 | { |
| 5605 | int err; |
| 5606 | int which, timeout; |
| 5607 | |
| 5608 | if (!PyArg_ParseTuple(args, "ii:rep_set_timeout", &which, &timeout)) { |
| 5609 | return NULL; |
| 5610 | } |
| 5611 | CHECK_ENV_NOT_CLOSED(self); |
| 5612 | MYDB_BEGIN_ALLOW_THREADS; |
| 5613 | err = self->db_env->rep_set_timeout(self->db_env, which, timeout); |
| 5614 | MYDB_END_ALLOW_THREADS; |
| 5615 | RETURN_IF_ERR(); |
| 5616 | RETURN_NONE(); |
| 5617 | } |
| 5618 | |
| 5619 | static PyObject* |
| 5620 | DBEnv_rep_get_timeout(DBEnvObject* self, PyObject* args) |
| 5621 | { |
| 5622 | int err; |
| 5623 | int which; |
| 5624 | u_int32_t timeout; |
| 5625 | |
| 5626 | if (!PyArg_ParseTuple(args, "i:rep_get_timeout", &which)) { |
| 5627 | return NULL; |
| 5628 | } |
| 5629 | CHECK_ENV_NOT_CLOSED(self); |
| 5630 | MYDB_BEGIN_ALLOW_THREADS; |
| 5631 | err = self->db_env->rep_get_timeout(self->db_env, which, &timeout); |
| 5632 | MYDB_END_ALLOW_THREADS; |
| 5633 | RETURN_IF_ERR(); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5634 | return NUMBER_FromLong(timeout); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5635 | } |
| 5636 | #endif |
| 5637 | |
| 5638 | /* --------------------------------------------------------------------- */ |
| 5639 | /* REPLICATION METHODS: Replication Manager */ |
| 5640 | |
| 5641 | #if (DBVER >= 45) |
| 5642 | static PyObject* |
| 5643 | DBEnv_repmgr_start(DBEnvObject* self, PyObject* args, PyObject* |
| 5644 | kwargs) |
| 5645 | { |
| 5646 | int err; |
| 5647 | int nthreads, flags; |
| 5648 | static char* kwnames[] = {"nthreads","flags", NULL}; |
| 5649 | |
| 5650 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 5651 | "ii:repmgr_start", kwnames, &nthreads, &flags)) |
| 5652 | { |
| 5653 | return NULL; |
| 5654 | } |
| 5655 | CHECK_ENV_NOT_CLOSED(self); |
| 5656 | MYDB_BEGIN_ALLOW_THREADS; |
| 5657 | err = self->db_env->repmgr_start(self->db_env, nthreads, flags); |
| 5658 | MYDB_END_ALLOW_THREADS; |
| 5659 | RETURN_IF_ERR(); |
| 5660 | RETURN_NONE(); |
| 5661 | } |
| 5662 | |
| 5663 | static PyObject* |
| 5664 | DBEnv_repmgr_set_local_site(DBEnvObject* self, PyObject* args, PyObject* |
| 5665 | kwargs) |
| 5666 | { |
| 5667 | int err; |
| 5668 | char *host; |
| 5669 | int port; |
| 5670 | int flags = 0; |
| 5671 | static char* kwnames[] = {"host", "port", "flags", NULL}; |
| 5672 | |
| 5673 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 5674 | "si|i:repmgr_set_local_site", kwnames, &host, &port, &flags)) |
| 5675 | { |
| 5676 | return NULL; |
| 5677 | } |
| 5678 | CHECK_ENV_NOT_CLOSED(self); |
| 5679 | MYDB_BEGIN_ALLOW_THREADS; |
| 5680 | err = self->db_env->repmgr_set_local_site(self->db_env, host, port, flags); |
| 5681 | MYDB_END_ALLOW_THREADS; |
| 5682 | RETURN_IF_ERR(); |
| 5683 | RETURN_NONE(); |
| 5684 | } |
| 5685 | |
| 5686 | static PyObject* |
| 5687 | DBEnv_repmgr_add_remote_site(DBEnvObject* self, PyObject* args, PyObject* |
| 5688 | kwargs) |
| 5689 | { |
| 5690 | int err; |
| 5691 | char *host; |
| 5692 | int port; |
| 5693 | int flags = 0; |
| 5694 | int eidp; |
| 5695 | static char* kwnames[] = {"host", "port", "flags", NULL}; |
| 5696 | |
| 5697 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 5698 | "si|i:repmgr_add_remote_site", kwnames, &host, &port, &flags)) |
| 5699 | { |
| 5700 | return NULL; |
| 5701 | } |
| 5702 | CHECK_ENV_NOT_CLOSED(self); |
| 5703 | MYDB_BEGIN_ALLOW_THREADS; |
| 5704 | err = self->db_env->repmgr_add_remote_site(self->db_env, host, port, &eidp, flags); |
| 5705 | MYDB_END_ALLOW_THREADS; |
| 5706 | RETURN_IF_ERR(); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5707 | return NUMBER_FromLong(eidp); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5708 | } |
| 5709 | |
| 5710 | static PyObject* |
| 5711 | DBEnv_repmgr_set_ack_policy(DBEnvObject* self, PyObject* args) |
| 5712 | { |
| 5713 | int err; |
| 5714 | int ack_policy; |
| 5715 | |
| 5716 | if (!PyArg_ParseTuple(args, "i:repmgr_set_ack_policy", &ack_policy)) |
| 5717 | { |
| 5718 | return NULL; |
| 5719 | } |
| 5720 | CHECK_ENV_NOT_CLOSED(self); |
| 5721 | MYDB_BEGIN_ALLOW_THREADS; |
| 5722 | err = self->db_env->repmgr_set_ack_policy(self->db_env, ack_policy); |
| 5723 | MYDB_END_ALLOW_THREADS; |
| 5724 | RETURN_IF_ERR(); |
| 5725 | RETURN_NONE(); |
| 5726 | } |
| 5727 | |
| 5728 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5729 | DBEnv_repmgr_get_ack_policy(DBEnvObject* self) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5730 | { |
| 5731 | int err; |
| 5732 | int ack_policy; |
| 5733 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5734 | CHECK_ENV_NOT_CLOSED(self); |
| 5735 | MYDB_BEGIN_ALLOW_THREADS; |
| 5736 | err = self->db_env->repmgr_get_ack_policy(self->db_env, &ack_policy); |
| 5737 | MYDB_END_ALLOW_THREADS; |
| 5738 | RETURN_IF_ERR(); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5739 | return NUMBER_FromLong(ack_policy); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5740 | } |
| 5741 | |
| 5742 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5743 | DBEnv_repmgr_site_list(DBEnvObject* self) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5744 | { |
| 5745 | int err; |
| 5746 | unsigned int countp; |
| 5747 | DB_REPMGR_SITE *listp; |
| 5748 | PyObject *stats, *key, *tuple; |
| 5749 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5750 | CHECK_ENV_NOT_CLOSED(self); |
| 5751 | MYDB_BEGIN_ALLOW_THREADS; |
| 5752 | err = self->db_env->repmgr_site_list(self->db_env, &countp, &listp); |
| 5753 | MYDB_END_ALLOW_THREADS; |
| 5754 | RETURN_IF_ERR(); |
| 5755 | |
| 5756 | stats=PyDict_New(); |
| 5757 | if (stats == NULL) { |
| 5758 | free(listp); |
| 5759 | return NULL; |
| 5760 | } |
| 5761 | |
| 5762 | for(;countp--;) { |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5763 | key=NUMBER_FromLong(listp[countp].eid); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5764 | if(!key) { |
| 5765 | Py_DECREF(stats); |
| 5766 | free(listp); |
| 5767 | return NULL; |
| 5768 | } |
| 5769 | #if (PY_VERSION_HEX >= 0x02040000) |
| 5770 | tuple=Py_BuildValue("(sII)", listp[countp].host, |
| 5771 | listp[countp].port, listp[countp].status); |
| 5772 | #else |
| 5773 | tuple=Py_BuildValue("(sii)", listp[countp].host, |
| 5774 | listp[countp].port, listp[countp].status); |
| 5775 | #endif |
| 5776 | if(!tuple) { |
| 5777 | Py_DECREF(key); |
| 5778 | Py_DECREF(stats); |
| 5779 | free(listp); |
| 5780 | return NULL; |
| 5781 | } |
| 5782 | if(PyDict_SetItem(stats, key, tuple)) { |
| 5783 | Py_DECREF(key); |
| 5784 | Py_DECREF(tuple); |
| 5785 | Py_DECREF(stats); |
| 5786 | free(listp); |
| 5787 | return NULL; |
| 5788 | } |
Florent Xicluna | e7901c5 | 2010-03-01 20:45:01 +0000 | [diff] [blame] | 5789 | Py_DECREF(key); |
| 5790 | Py_DECREF(tuple); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5791 | } |
| 5792 | free(listp); |
| 5793 | return stats; |
| 5794 | } |
| 5795 | #endif |
| 5796 | |
| 5797 | #if (DBVER >= 46) |
| 5798 | static PyObject* |
| 5799 | DBEnv_repmgr_stat_print(DBEnvObject* self, PyObject* args, PyObject *kwargs) |
| 5800 | { |
| 5801 | int err; |
| 5802 | int flags=0; |
| 5803 | static char* kwnames[] = { "flags", NULL }; |
| 5804 | |
| 5805 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:repmgr_stat_print", |
| 5806 | kwnames, &flags)) |
| 5807 | { |
| 5808 | return NULL; |
| 5809 | } |
| 5810 | CHECK_ENV_NOT_CLOSED(self); |
| 5811 | MYDB_BEGIN_ALLOW_THREADS; |
| 5812 | err = self->db_env->repmgr_stat_print(self->db_env, flags); |
| 5813 | MYDB_END_ALLOW_THREADS; |
| 5814 | RETURN_IF_ERR(); |
| 5815 | RETURN_NONE(); |
| 5816 | } |
| 5817 | |
| 5818 | static PyObject* |
| 5819 | DBEnv_repmgr_stat(DBEnvObject* self, PyObject* args, PyObject *kwargs) |
| 5820 | { |
| 5821 | int err; |
| 5822 | int flags=0; |
| 5823 | DB_REPMGR_STAT *statp; |
| 5824 | PyObject *stats; |
| 5825 | static char* kwnames[] = { "flags", NULL }; |
| 5826 | |
| 5827 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:repmgr_stat", |
| 5828 | kwnames, &flags)) |
| 5829 | { |
| 5830 | return NULL; |
| 5831 | } |
| 5832 | CHECK_ENV_NOT_CLOSED(self); |
| 5833 | MYDB_BEGIN_ALLOW_THREADS; |
| 5834 | err = self->db_env->repmgr_stat(self->db_env, &statp, flags); |
| 5835 | MYDB_END_ALLOW_THREADS; |
| 5836 | RETURN_IF_ERR(); |
| 5837 | |
| 5838 | stats=PyDict_New(); |
| 5839 | if (stats == NULL) { |
| 5840 | free(statp); |
| 5841 | return NULL; |
| 5842 | } |
| 5843 | |
| 5844 | #define MAKE_ENTRY(name) _addIntToDict(stats, #name, statp->st_##name) |
| 5845 | |
| 5846 | MAKE_ENTRY(perm_failed); |
| 5847 | MAKE_ENTRY(msgs_queued); |
| 5848 | MAKE_ENTRY(msgs_dropped); |
| 5849 | MAKE_ENTRY(connection_drop); |
| 5850 | MAKE_ENTRY(connect_fail); |
| 5851 | |
| 5852 | #undef MAKE_ENTRY |
| 5853 | |
| 5854 | free(statp); |
| 5855 | return stats; |
| 5856 | } |
| 5857 | #endif |
| 5858 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5859 | |
| 5860 | /* --------------------------------------------------------------------- */ |
| 5861 | /* DBTxn methods */ |
| 5862 | |
| 5863 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5864 | static void _close_transaction_cursors(DBTxnObject* txn) |
| 5865 | { |
| 5866 | PyObject *dummy; |
| 5867 | |
| 5868 | while(txn->children_cursors) { |
| 5869 | PyErr_Warn(PyExc_RuntimeWarning, |
| 5870 | "Must close cursors before resolving a transaction."); |
| 5871 | dummy=DBC_close_internal(txn->children_cursors); |
| 5872 | Py_XDECREF(dummy); |
| 5873 | } |
| 5874 | } |
| 5875 | |
| 5876 | static void _promote_transaction_dbs_and_sequences(DBTxnObject *txn) |
| 5877 | { |
| 5878 | DBObject *db; |
| 5879 | #if (DBVER >= 43) |
| 5880 | DBSequenceObject *dbs; |
| 5881 | #endif |
| 5882 | |
| 5883 | while (txn->children_dbs) { |
| 5884 | db=txn->children_dbs; |
| 5885 | EXTRACT_FROM_DOUBLE_LINKED_LIST_TXN(db); |
| 5886 | if (txn->parent_txn) { |
| 5887 | INSERT_IN_DOUBLE_LINKED_LIST_TXN(txn->parent_txn->children_dbs,db); |
| 5888 | db->txn=txn->parent_txn; |
| 5889 | } else { |
| 5890 | /* The db is already linked to its environment, |
| 5891 | ** so nothing to do. |
| 5892 | */ |
| 5893 | db->txn=NULL; |
| 5894 | } |
| 5895 | } |
| 5896 | |
| 5897 | #if (DBVER >= 43) |
| 5898 | while (txn->children_sequences) { |
| 5899 | dbs=txn->children_sequences; |
| 5900 | EXTRACT_FROM_DOUBLE_LINKED_LIST_TXN(dbs); |
| 5901 | if (txn->parent_txn) { |
| 5902 | INSERT_IN_DOUBLE_LINKED_LIST_TXN(txn->parent_txn->children_sequences,dbs); |
| 5903 | dbs->txn=txn->parent_txn; |
| 5904 | } else { |
| 5905 | /* The sequence is already linked to its |
| 5906 | ** parent db. Nothing to do. |
| 5907 | */ |
| 5908 | dbs->txn=NULL; |
| 5909 | } |
| 5910 | } |
| 5911 | #endif |
| 5912 | } |
| 5913 | |
| 5914 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5915 | static PyObject* |
| 5916 | DBTxn_commit(DBTxnObject* self, PyObject* args) |
| 5917 | { |
| 5918 | int flags=0, err; |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 5919 | DB_TXN *txn; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5920 | |
| 5921 | if (!PyArg_ParseTuple(args, "|i:commit", &flags)) |
| 5922 | return NULL; |
| 5923 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5924 | _close_transaction_cursors(self); |
| 5925 | |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 5926 | if (!self->txn) { |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 5927 | PyObject *t = Py_BuildValue("(is)", 0, "DBTxn must not be used " |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5928 | "after txn_commit, txn_abort " |
| 5929 | "or txn_discard"); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5930 | if (t) { |
| 5931 | PyErr_SetObject(DBError, t); |
| 5932 | Py_DECREF(t); |
| 5933 | } |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 5934 | return NULL; |
| 5935 | } |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5936 | self->flag_prepare=0; |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 5937 | txn = self->txn; |
| 5938 | self->txn = NULL; /* this DB_TXN is no longer valid after this call */ |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5939 | |
| 5940 | EXTRACT_FROM_DOUBLE_LINKED_LIST(self); |
| 5941 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5942 | MYDB_BEGIN_ALLOW_THREADS; |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 5943 | err = txn->commit(txn, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5944 | MYDB_END_ALLOW_THREADS; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5945 | |
| 5946 | _promote_transaction_dbs_and_sequences(self); |
| 5947 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5948 | RETURN_IF_ERR(); |
| 5949 | RETURN_NONE(); |
| 5950 | } |
| 5951 | |
| 5952 | static PyObject* |
| 5953 | DBTxn_prepare(DBTxnObject* self, PyObject* args) |
| 5954 | { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5955 | int err; |
| 5956 | char* gid=NULL; |
| 5957 | int gid_size=0; |
| 5958 | |
| 5959 | if (!PyArg_ParseTuple(args, "s#:prepare", &gid, &gid_size)) |
| 5960 | return NULL; |
| 5961 | |
Matthias Klose | 54cc539 | 2010-03-15 12:46:18 +0000 | [diff] [blame] | 5962 | if (gid_size != DB_GID_SIZE) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5963 | PyErr_SetString(PyExc_TypeError, |
Matthias Klose | 54cc539 | 2010-03-15 12:46:18 +0000 | [diff] [blame] | 5964 | "gid must be DB_GID_SIZE bytes long"); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5965 | return NULL; |
| 5966 | } |
| 5967 | |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 5968 | if (!self->txn) { |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 5969 | PyObject *t = Py_BuildValue("(is)", 0,"DBTxn must not be used " |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5970 | "after txn_commit, txn_abort " |
| 5971 | "or txn_discard"); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5972 | if (t) { |
| 5973 | PyErr_SetObject(DBError, t); |
| 5974 | Py_DECREF(t); |
| 5975 | } |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 5976 | return NULL; |
| 5977 | } |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5978 | self->flag_prepare=1; /* Prepare state */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5979 | MYDB_BEGIN_ALLOW_THREADS; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5980 | err = self->txn->prepare(self->txn, (u_int8_t*)gid); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5981 | MYDB_END_ALLOW_THREADS; |
| 5982 | RETURN_IF_ERR(); |
| 5983 | RETURN_NONE(); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5984 | } |
| 5985 | |
| 5986 | |
| 5987 | static PyObject* |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5988 | DBTxn_abort_discard_internal(DBTxnObject* self, int discard) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5989 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5990 | PyObject *dummy; |
| 5991 | int err=0; |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 5992 | DB_TXN *txn; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5993 | |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 5994 | if (!self->txn) { |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 5995 | PyObject *t = Py_BuildValue("(is)", 0, "DBTxn must not be used " |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 5996 | "after txn_commit, txn_abort " |
| 5997 | "or txn_discard"); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 5998 | if (t) { |
| 5999 | PyErr_SetObject(DBError, t); |
| 6000 | Py_DECREF(t); |
| 6001 | } |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 6002 | return NULL; |
| 6003 | } |
| 6004 | txn = self->txn; |
| 6005 | self->txn = NULL; /* this DB_TXN is no longer valid after this call */ |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6006 | |
| 6007 | _close_transaction_cursors(self); |
| 6008 | #if (DBVER >= 43) |
| 6009 | while (self->children_sequences) { |
| 6010 | dummy=DBSequence_close_internal(self->children_sequences,0,0); |
| 6011 | Py_XDECREF(dummy); |
| 6012 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6013 | #endif |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6014 | while (self->children_dbs) { |
Jesus Cea | 5cd5f12 | 2008-09-23 18:54:08 +0000 | [diff] [blame] | 6015 | dummy=DB_close_internal(self->children_dbs, 0, 0); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6016 | Py_XDECREF(dummy); |
| 6017 | } |
| 6018 | |
| 6019 | EXTRACT_FROM_DOUBLE_LINKED_LIST(self); |
| 6020 | |
| 6021 | MYDB_BEGIN_ALLOW_THREADS; |
| 6022 | if (discard) { |
| 6023 | assert(!self->flag_prepare); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6024 | err = txn->discard(txn,0); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6025 | } else { |
| 6026 | /* |
| 6027 | ** If the transaction is in the "prepare" or "recover" state, |
| 6028 | ** we better do not implicitly abort it. |
| 6029 | */ |
| 6030 | if (!self->flag_prepare) { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6031 | err = txn->abort(txn); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6032 | } |
| 6033 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6034 | MYDB_END_ALLOW_THREADS; |
| 6035 | RETURN_IF_ERR(); |
| 6036 | RETURN_NONE(); |
| 6037 | } |
| 6038 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6039 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6040 | DBTxn_abort(DBTxnObject* self) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6041 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6042 | self->flag_prepare=0; |
| 6043 | _close_transaction_cursors(self); |
| 6044 | |
| 6045 | return DBTxn_abort_discard_internal(self,0); |
| 6046 | } |
| 6047 | |
| 6048 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6049 | DBTxn_discard(DBTxnObject* self) |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6050 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6051 | self->flag_prepare=0; |
| 6052 | _close_transaction_cursors(self); |
| 6053 | |
| 6054 | return DBTxn_abort_discard_internal(self,1); |
| 6055 | } |
| 6056 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6057 | |
| 6058 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6059 | DBTxn_id(DBTxnObject* self) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6060 | { |
| 6061 | int id; |
| 6062 | |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 6063 | if (!self->txn) { |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 6064 | PyObject *t = Py_BuildValue("(is)", 0, "DBTxn must not be used " |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6065 | "after txn_commit, txn_abort " |
| 6066 | "or txn_discard"); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6067 | if (t) { |
| 6068 | PyErr_SetObject(DBError, t); |
| 6069 | Py_DECREF(t); |
| 6070 | } |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 6071 | return NULL; |
| 6072 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6073 | MYDB_BEGIN_ALLOW_THREADS; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6074 | id = self->txn->id(self->txn); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6075 | MYDB_END_ALLOW_THREADS; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6076 | return NUMBER_FromLong(id); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6077 | } |
| 6078 | |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6079 | #if (DBVER >= 43) |
| 6080 | /* --------------------------------------------------------------------- */ |
| 6081 | /* DBSequence methods */ |
| 6082 | |
| 6083 | |
| 6084 | static PyObject* |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6085 | DBSequence_close_internal(DBSequenceObject* self, int flags, int do_not_close) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6086 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6087 | int err=0; |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6088 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6089 | if (self->sequence!=NULL) { |
| 6090 | EXTRACT_FROM_DOUBLE_LINKED_LIST(self); |
| 6091 | if (self->txn) { |
| 6092 | EXTRACT_FROM_DOUBLE_LINKED_LIST_TXN(self); |
| 6093 | self->txn=NULL; |
| 6094 | } |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6095 | |
Jesus Cea | 5cd5f12 | 2008-09-23 18:54:08 +0000 | [diff] [blame] | 6096 | /* |
| 6097 | ** "do_not_close" is used to dispose all related objects in the |
| 6098 | ** tree, without actually releasing the "root" object. |
| 6099 | ** This is done, for example, because function calls like |
| 6100 | ** "DBSequence.remove()" implicitly close the underlying handle. So |
| 6101 | ** the handle doesn't need to be closed, but related objects |
| 6102 | ** must be cleaned up. |
| 6103 | */ |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6104 | if (!do_not_close) { |
| 6105 | MYDB_BEGIN_ALLOW_THREADS |
| 6106 | err = self->sequence->close(self->sequence, flags); |
| 6107 | MYDB_END_ALLOW_THREADS |
| 6108 | } |
| 6109 | self->sequence = NULL; |
| 6110 | |
| 6111 | RETURN_IF_ERR(); |
| 6112 | } |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6113 | |
| 6114 | RETURN_NONE(); |
| 6115 | } |
| 6116 | |
| 6117 | static PyObject* |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6118 | DBSequence_close(DBSequenceObject* self, PyObject* args) |
| 6119 | { |
| 6120 | int flags=0; |
| 6121 | if (!PyArg_ParseTuple(args,"|i:close", &flags)) |
| 6122 | return NULL; |
| 6123 | |
| 6124 | return DBSequence_close_internal(self,flags,0); |
| 6125 | } |
| 6126 | |
| 6127 | static PyObject* |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6128 | DBSequence_get(DBSequenceObject* self, PyObject* args, PyObject* kwargs) |
| 6129 | { |
| 6130 | int err, flags = 0; |
| 6131 | int delta = 1; |
| 6132 | db_seq_t value; |
| 6133 | PyObject *txnobj = NULL; |
| 6134 | DB_TXN *txn = NULL; |
| 6135 | static char* kwnames[] = {"delta", "txn", "flags", NULL }; |
| 6136 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iOi:get", kwnames, &delta, &txnobj, &flags)) |
| 6137 | return NULL; |
| 6138 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 6139 | |
| 6140 | if (!checkTxnObj(txnobj, &txn)) |
| 6141 | return NULL; |
| 6142 | |
| 6143 | MYDB_BEGIN_ALLOW_THREADS |
| 6144 | err = self->sequence->get(self->sequence, txn, delta, &value, flags); |
| 6145 | MYDB_END_ALLOW_THREADS |
| 6146 | |
| 6147 | RETURN_IF_ERR(); |
| 6148 | return PyLong_FromLongLong(value); |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6149 | } |
| 6150 | |
| 6151 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6152 | DBSequence_get_dbp(DBSequenceObject* self) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6153 | { |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6154 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 6155 | Py_INCREF(self->mydb); |
| 6156 | return (PyObject* )self->mydb; |
| 6157 | } |
| 6158 | |
| 6159 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6160 | DBSequence_get_key(DBSequenceObject* self) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6161 | { |
| 6162 | int err; |
| 6163 | DBT key; |
Neal Norwitz | 088beae | 2007-10-12 03:01:54 +0000 | [diff] [blame] | 6164 | PyObject *retval = NULL; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6165 | |
Gregory P. Smith | e70be5c | 2007-10-06 07:48:10 +0000 | [diff] [blame] | 6166 | key.flags = DB_DBT_MALLOC; |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6167 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 6168 | MYDB_BEGIN_ALLOW_THREADS |
| 6169 | err = self->sequence->get_key(self->sequence, &key); |
| 6170 | MYDB_END_ALLOW_THREADS |
| 6171 | |
Gregory P. Smith | e70be5c | 2007-10-06 07:48:10 +0000 | [diff] [blame] | 6172 | if (!err) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6173 | retval = Build_PyString(key.data, key.size); |
Gregory P. Smith | e70be5c | 2007-10-06 07:48:10 +0000 | [diff] [blame] | 6174 | |
| 6175 | FREE_DBT(key); |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6176 | RETURN_IF_ERR(); |
| 6177 | |
Gregory P. Smith | e70be5c | 2007-10-06 07:48:10 +0000 | [diff] [blame] | 6178 | return retval; |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6179 | } |
| 6180 | |
| 6181 | static PyObject* |
| 6182 | DBSequence_init_value(DBSequenceObject* self, PyObject* args) |
| 6183 | { |
| 6184 | int err; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6185 | PY_LONG_LONG value; |
| 6186 | db_seq_t value2; |
Neal Norwitz | dd2a6bf | 2006-06-06 07:23:01 +0000 | [diff] [blame] | 6187 | if (!PyArg_ParseTuple(args,"L:init_value", &value)) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6188 | return NULL; |
| 6189 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 6190 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6191 | value2=value; /* If truncation, compiler should show a warning */ |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6192 | MYDB_BEGIN_ALLOW_THREADS |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6193 | err = self->sequence->initial_value(self->sequence, value2); |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6194 | MYDB_END_ALLOW_THREADS |
| 6195 | |
| 6196 | RETURN_IF_ERR(); |
| 6197 | |
| 6198 | RETURN_NONE(); |
| 6199 | } |
| 6200 | |
| 6201 | static PyObject* |
| 6202 | DBSequence_open(DBSequenceObject* self, PyObject* args, PyObject* kwargs) |
| 6203 | { |
| 6204 | int err, flags = 0; |
| 6205 | PyObject* keyobj; |
| 6206 | PyObject *txnobj = NULL; |
| 6207 | DB_TXN *txn = NULL; |
| 6208 | DBT key; |
| 6209 | |
| 6210 | static char* kwnames[] = {"key", "txn", "flags", NULL }; |
Neal Norwitz | dd2a6bf | 2006-06-06 07:23:01 +0000 | [diff] [blame] | 6211 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|Oi:open", kwnames, &keyobj, &txnobj, &flags)) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6212 | return NULL; |
| 6213 | |
| 6214 | if (!checkTxnObj(txnobj, &txn)) |
| 6215 | return NULL; |
| 6216 | |
| 6217 | if (!make_key_dbt(self->mydb, keyobj, &key, NULL)) |
| 6218 | return NULL; |
| 6219 | |
| 6220 | MYDB_BEGIN_ALLOW_THREADS |
| 6221 | err = self->sequence->open(self->sequence, txn, &key, flags); |
| 6222 | MYDB_END_ALLOW_THREADS |
| 6223 | |
Jesus Cea | ac25fab | 2008-09-03 17:50:32 +0000 | [diff] [blame] | 6224 | FREE_DBT(key); |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6225 | RETURN_IF_ERR(); |
| 6226 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6227 | if (txn) { |
| 6228 | INSERT_IN_DOUBLE_LINKED_LIST_TXN(((DBTxnObject *)txnobj)->children_sequences,self); |
| 6229 | self->txn=(DBTxnObject *)txnobj; |
| 6230 | } |
| 6231 | |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6232 | RETURN_NONE(); |
| 6233 | } |
| 6234 | |
| 6235 | static PyObject* |
| 6236 | DBSequence_remove(DBSequenceObject* self, PyObject* args, PyObject* kwargs) |
| 6237 | { |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6238 | PyObject *dummy; |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6239 | int err, flags = 0; |
| 6240 | PyObject *txnobj = NULL; |
| 6241 | DB_TXN *txn = NULL; |
| 6242 | |
| 6243 | static char* kwnames[] = {"txn", "flags", NULL }; |
Neal Norwitz | dd2a6bf | 2006-06-06 07:23:01 +0000 | [diff] [blame] | 6244 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oi:remove", kwnames, &txnobj, &flags)) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6245 | return NULL; |
| 6246 | |
| 6247 | if (!checkTxnObj(txnobj, &txn)) |
| 6248 | return NULL; |
| 6249 | |
| 6250 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 6251 | |
| 6252 | MYDB_BEGIN_ALLOW_THREADS |
| 6253 | err = self->sequence->remove(self->sequence, txn, flags); |
| 6254 | MYDB_END_ALLOW_THREADS |
| 6255 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6256 | dummy=DBSequence_close_internal(self,flags,1); |
| 6257 | Py_XDECREF(dummy); |
| 6258 | |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6259 | RETURN_IF_ERR(); |
| 6260 | RETURN_NONE(); |
| 6261 | } |
| 6262 | |
| 6263 | static PyObject* |
| 6264 | DBSequence_set_cachesize(DBSequenceObject* self, PyObject* args) |
| 6265 | { |
| 6266 | int err, size; |
Neal Norwitz | dd2a6bf | 2006-06-06 07:23:01 +0000 | [diff] [blame] | 6267 | if (!PyArg_ParseTuple(args,"i:set_cachesize", &size)) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6268 | return NULL; |
| 6269 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 6270 | |
| 6271 | MYDB_BEGIN_ALLOW_THREADS |
| 6272 | err = self->sequence->set_cachesize(self->sequence, size); |
| 6273 | MYDB_END_ALLOW_THREADS |
| 6274 | |
| 6275 | RETURN_IF_ERR(); |
| 6276 | RETURN_NONE(); |
| 6277 | } |
| 6278 | |
| 6279 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6280 | DBSequence_get_cachesize(DBSequenceObject* self) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6281 | { |
| 6282 | int err, size; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6283 | |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6284 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 6285 | |
| 6286 | MYDB_BEGIN_ALLOW_THREADS |
| 6287 | err = self->sequence->get_cachesize(self->sequence, &size); |
| 6288 | MYDB_END_ALLOW_THREADS |
| 6289 | |
| 6290 | RETURN_IF_ERR(); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6291 | return NUMBER_FromLong(size); |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6292 | } |
| 6293 | |
| 6294 | static PyObject* |
| 6295 | DBSequence_set_flags(DBSequenceObject* self, PyObject* args) |
| 6296 | { |
| 6297 | int err, flags = 0; |
Neal Norwitz | dd2a6bf | 2006-06-06 07:23:01 +0000 | [diff] [blame] | 6298 | if (!PyArg_ParseTuple(args,"i:set_flags", &flags)) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6299 | return NULL; |
| 6300 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 6301 | |
| 6302 | MYDB_BEGIN_ALLOW_THREADS |
| 6303 | err = self->sequence->set_flags(self->sequence, flags); |
| 6304 | MYDB_END_ALLOW_THREADS |
| 6305 | |
| 6306 | RETURN_IF_ERR(); |
| 6307 | RETURN_NONE(); |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6308 | } |
| 6309 | |
| 6310 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6311 | DBSequence_get_flags(DBSequenceObject* self) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6312 | { |
| 6313 | unsigned int flags; |
| 6314 | int err; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6315 | |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6316 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 6317 | |
| 6318 | MYDB_BEGIN_ALLOW_THREADS |
| 6319 | err = self->sequence->get_flags(self->sequence, &flags); |
| 6320 | MYDB_END_ALLOW_THREADS |
| 6321 | |
| 6322 | RETURN_IF_ERR(); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6323 | return NUMBER_FromLong((int)flags); |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6324 | } |
| 6325 | |
| 6326 | static PyObject* |
| 6327 | DBSequence_set_range(DBSequenceObject* self, PyObject* args) |
| 6328 | { |
| 6329 | int err; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6330 | PY_LONG_LONG min, max; |
| 6331 | db_seq_t min2, max2; |
Tim Peters | bb21b2c | 2006-06-06 15:50:17 +0000 | [diff] [blame] | 6332 | if (!PyArg_ParseTuple(args,"(LL):set_range", &min, &max)) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6333 | return NULL; |
| 6334 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 6335 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6336 | min2=min; /* If truncation, compiler should show a warning */ |
| 6337 | max2=max; |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6338 | MYDB_BEGIN_ALLOW_THREADS |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6339 | err = self->sequence->set_range(self->sequence, min2, max2); |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6340 | MYDB_END_ALLOW_THREADS |
| 6341 | |
| 6342 | RETURN_IF_ERR(); |
| 6343 | RETURN_NONE(); |
| 6344 | } |
| 6345 | |
| 6346 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6347 | DBSequence_get_range(DBSequenceObject* self) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6348 | { |
| 6349 | int err; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6350 | PY_LONG_LONG min, max; |
| 6351 | db_seq_t min2, max2; |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6352 | |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6353 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 6354 | |
| 6355 | MYDB_BEGIN_ALLOW_THREADS |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6356 | err = self->sequence->get_range(self->sequence, &min2, &max2); |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6357 | MYDB_END_ALLOW_THREADS |
| 6358 | |
| 6359 | RETURN_IF_ERR(); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6360 | min=min2; /* If truncation, compiler should show a warning */ |
| 6361 | max=max2; |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6362 | return Py_BuildValue("(LL)", min, max); |
| 6363 | } |
| 6364 | |
| 6365 | static PyObject* |
| 6366 | DBSequence_stat(DBSequenceObject* self, PyObject* args, PyObject* kwargs) |
| 6367 | { |
| 6368 | int err, flags = 0; |
| 6369 | DB_SEQUENCE_STAT* sp = NULL; |
| 6370 | PyObject* dict_stat; |
| 6371 | static char* kwnames[] = {"flags", NULL }; |
| 6372 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:stat", kwnames, &flags)) |
| 6373 | return NULL; |
| 6374 | CHECK_SEQUENCE_NOT_CLOSED(self); |
| 6375 | |
| 6376 | MYDB_BEGIN_ALLOW_THREADS; |
| 6377 | err = self->sequence->stat(self->sequence, &sp, flags); |
| 6378 | MYDB_END_ALLOW_THREADS; |
| 6379 | RETURN_IF_ERR(); |
| 6380 | |
| 6381 | if ((dict_stat = PyDict_New()) == NULL) { |
| 6382 | free(sp); |
| 6383 | return NULL; |
| 6384 | } |
| 6385 | |
| 6386 | |
| 6387 | #define MAKE_INT_ENTRY(name) _addIntToDict(dict_stat, #name, sp->st_##name) |
| 6388 | #define MAKE_LONG_LONG_ENTRY(name) _addDb_seq_tToDict(dict_stat, #name, sp->st_##name) |
| 6389 | |
| 6390 | MAKE_INT_ENTRY(wait); |
| 6391 | MAKE_INT_ENTRY(nowait); |
| 6392 | MAKE_LONG_LONG_ENTRY(current); |
| 6393 | MAKE_LONG_LONG_ENTRY(value); |
| 6394 | MAKE_LONG_LONG_ENTRY(last_value); |
| 6395 | MAKE_LONG_LONG_ENTRY(min); |
| 6396 | MAKE_LONG_LONG_ENTRY(max); |
| 6397 | MAKE_INT_ENTRY(cache_size); |
| 6398 | MAKE_INT_ENTRY(flags); |
| 6399 | |
| 6400 | #undef MAKE_INT_ENTRY |
| 6401 | #undef MAKE_LONG_LONG_ENTRY |
| 6402 | |
| 6403 | free(sp); |
| 6404 | return dict_stat; |
| 6405 | } |
| 6406 | #endif |
| 6407 | |
| 6408 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6409 | /* --------------------------------------------------------------------- */ |
| 6410 | /* Method definition tables and type objects */ |
| 6411 | |
| 6412 | static PyMethodDef DB_methods[] = { |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 6413 | {"append", (PyCFunction)DB_append, METH_VARARGS|METH_KEYWORDS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6414 | {"associate", (PyCFunction)DB_associate, METH_VARARGS|METH_KEYWORDS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6415 | {"close", (PyCFunction)DB_close, METH_VARARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6416 | {"consume", (PyCFunction)DB_consume, METH_VARARGS|METH_KEYWORDS}, |
| 6417 | {"consume_wait", (PyCFunction)DB_consume_wait, METH_VARARGS|METH_KEYWORDS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6418 | {"cursor", (PyCFunction)DB_cursor, METH_VARARGS|METH_KEYWORDS}, |
| 6419 | {"delete", (PyCFunction)DB_delete, METH_VARARGS|METH_KEYWORDS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6420 | {"fd", (PyCFunction)DB_fd, METH_NOARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6421 | {"get", (PyCFunction)DB_get, METH_VARARGS|METH_KEYWORDS}, |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 6422 | {"pget", (PyCFunction)DB_pget, METH_VARARGS|METH_KEYWORDS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6423 | {"get_both", (PyCFunction)DB_get_both, METH_VARARGS|METH_KEYWORDS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6424 | {"get_byteswapped", (PyCFunction)DB_get_byteswapped,METH_NOARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6425 | {"get_size", (PyCFunction)DB_get_size, METH_VARARGS|METH_KEYWORDS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6426 | {"get_type", (PyCFunction)DB_get_type, METH_NOARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6427 | {"join", (PyCFunction)DB_join, METH_VARARGS}, |
| 6428 | {"key_range", (PyCFunction)DB_key_range, METH_VARARGS|METH_KEYWORDS}, |
Jesus Cea | 4907d27 | 2008-08-31 14:00:51 +0000 | [diff] [blame] | 6429 | {"has_key", (PyCFunction)DB_has_key, METH_VARARGS|METH_KEYWORDS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6430 | {"items", (PyCFunction)DB_items, METH_VARARGS}, |
| 6431 | {"keys", (PyCFunction)DB_keys, METH_VARARGS}, |
| 6432 | {"open", (PyCFunction)DB_open, METH_VARARGS|METH_KEYWORDS}, |
| 6433 | {"put", (PyCFunction)DB_put, METH_VARARGS|METH_KEYWORDS}, |
| 6434 | {"remove", (PyCFunction)DB_remove, METH_VARARGS|METH_KEYWORDS}, |
| 6435 | {"rename", (PyCFunction)DB_rename, METH_VARARGS}, |
| 6436 | {"set_bt_minkey", (PyCFunction)DB_set_bt_minkey, METH_VARARGS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6437 | {"set_bt_compare", (PyCFunction)DB_set_bt_compare, METH_O}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6438 | {"set_cachesize", (PyCFunction)DB_set_cachesize, METH_VARARGS}, |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 6439 | #if (DBVER >= 41) |
| 6440 | {"set_encrypt", (PyCFunction)DB_set_encrypt, METH_VARARGS|METH_KEYWORDS}, |
| 6441 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6442 | {"set_flags", (PyCFunction)DB_set_flags, METH_VARARGS}, |
| 6443 | {"set_h_ffactor", (PyCFunction)DB_set_h_ffactor, METH_VARARGS}, |
| 6444 | {"set_h_nelem", (PyCFunction)DB_set_h_nelem, METH_VARARGS}, |
| 6445 | {"set_lorder", (PyCFunction)DB_set_lorder, METH_VARARGS}, |
| 6446 | {"set_pagesize", (PyCFunction)DB_set_pagesize, METH_VARARGS}, |
| 6447 | {"set_re_delim", (PyCFunction)DB_set_re_delim, METH_VARARGS}, |
| 6448 | {"set_re_len", (PyCFunction)DB_set_re_len, METH_VARARGS}, |
| 6449 | {"set_re_pad", (PyCFunction)DB_set_re_pad, METH_VARARGS}, |
| 6450 | {"set_re_source", (PyCFunction)DB_set_re_source, METH_VARARGS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6451 | {"set_q_extentsize",(PyCFunction)DB_set_q_extentsize, METH_VARARGS}, |
| 6452 | {"set_private", (PyCFunction)DB_set_private, METH_O}, |
| 6453 | {"get_private", (PyCFunction)DB_get_private, METH_NOARGS}, |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 6454 | {"stat", (PyCFunction)DB_stat, METH_VARARGS|METH_KEYWORDS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6455 | {"sync", (PyCFunction)DB_sync, METH_VARARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6456 | {"truncate", (PyCFunction)DB_truncate, METH_VARARGS|METH_KEYWORDS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6457 | {"type", (PyCFunction)DB_get_type, METH_NOARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6458 | {"upgrade", (PyCFunction)DB_upgrade, METH_VARARGS}, |
| 6459 | {"values", (PyCFunction)DB_values, METH_VARARGS}, |
| 6460 | {"verify", (PyCFunction)DB_verify, METH_VARARGS|METH_KEYWORDS}, |
| 6461 | {"set_get_returns_none",(PyCFunction)DB_set_get_returns_none, METH_VARARGS}, |
| 6462 | {NULL, NULL} /* sentinel */ |
| 6463 | }; |
| 6464 | |
| 6465 | |
| 6466 | static PyMappingMethods DB_mapping = { |
Martin v. Löwis | 70ee3cc | 2006-06-12 04:26:31 +0000 | [diff] [blame] | 6467 | DB_length, /*mp_length*/ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6468 | (binaryfunc)DB_subscript, /*mp_subscript*/ |
| 6469 | (objobjargproc)DB_ass_sub, /*mp_ass_subscript*/ |
| 6470 | }; |
| 6471 | |
| 6472 | |
| 6473 | static PyMethodDef DBCursor_methods[] = { |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6474 | {"close", (PyCFunction)DBC_close, METH_NOARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6475 | {"count", (PyCFunction)DBC_count, METH_VARARGS}, |
| 6476 | {"current", (PyCFunction)DBC_current, METH_VARARGS|METH_KEYWORDS}, |
| 6477 | {"delete", (PyCFunction)DBC_delete, METH_VARARGS}, |
| 6478 | {"dup", (PyCFunction)DBC_dup, METH_VARARGS}, |
| 6479 | {"first", (PyCFunction)DBC_first, METH_VARARGS|METH_KEYWORDS}, |
| 6480 | {"get", (PyCFunction)DBC_get, METH_VARARGS|METH_KEYWORDS}, |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 6481 | {"pget", (PyCFunction)DBC_pget, METH_VARARGS|METH_KEYWORDS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6482 | {"get_recno", (PyCFunction)DBC_get_recno, METH_NOARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6483 | {"last", (PyCFunction)DBC_last, METH_VARARGS|METH_KEYWORDS}, |
| 6484 | {"next", (PyCFunction)DBC_next, METH_VARARGS|METH_KEYWORDS}, |
| 6485 | {"prev", (PyCFunction)DBC_prev, METH_VARARGS|METH_KEYWORDS}, |
| 6486 | {"put", (PyCFunction)DBC_put, METH_VARARGS|METH_KEYWORDS}, |
| 6487 | {"set", (PyCFunction)DBC_set, METH_VARARGS|METH_KEYWORDS}, |
| 6488 | {"set_range", (PyCFunction)DBC_set_range, METH_VARARGS|METH_KEYWORDS}, |
| 6489 | {"get_both", (PyCFunction)DBC_get_both, METH_VARARGS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6490 | {"get_current_size",(PyCFunction)DBC_get_current_size, METH_NOARGS}, |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 6491 | {"set_both", (PyCFunction)DBC_set_both, METH_VARARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6492 | {"set_recno", (PyCFunction)DBC_set_recno, METH_VARARGS|METH_KEYWORDS}, |
| 6493 | {"consume", (PyCFunction)DBC_consume, METH_VARARGS|METH_KEYWORDS}, |
| 6494 | {"next_dup", (PyCFunction)DBC_next_dup, METH_VARARGS|METH_KEYWORDS}, |
| 6495 | {"next_nodup", (PyCFunction)DBC_next_nodup, METH_VARARGS|METH_KEYWORDS}, |
| 6496 | {"prev_nodup", (PyCFunction)DBC_prev_nodup, METH_VARARGS|METH_KEYWORDS}, |
| 6497 | {"join_item", (PyCFunction)DBC_join_item, METH_VARARGS}, |
| 6498 | {NULL, NULL} /* sentinel */ |
| 6499 | }; |
| 6500 | |
| 6501 | |
| 6502 | static PyMethodDef DBEnv_methods[] = { |
| 6503 | {"close", (PyCFunction)DBEnv_close, METH_VARARGS}, |
| 6504 | {"open", (PyCFunction)DBEnv_open, METH_VARARGS}, |
| 6505 | {"remove", (PyCFunction)DBEnv_remove, METH_VARARGS}, |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 6506 | #if (DBVER >= 41) |
| 6507 | {"dbremove", (PyCFunction)DBEnv_dbremove, METH_VARARGS|METH_KEYWORDS}, |
| 6508 | {"dbrename", (PyCFunction)DBEnv_dbrename, METH_VARARGS|METH_KEYWORDS}, |
| 6509 | {"set_encrypt", (PyCFunction)DBEnv_set_encrypt, METH_VARARGS|METH_KEYWORDS}, |
| 6510 | #endif |
Gregory P. Smith | fe11d3e | 2003-03-27 17:23:29 +0000 | [diff] [blame] | 6511 | {"set_timeout", (PyCFunction)DBEnv_set_timeout, METH_VARARGS|METH_KEYWORDS}, |
Gregory P. Smith | 6676f6e | 2003-08-28 21:50:30 +0000 | [diff] [blame] | 6512 | {"set_shm_key", (PyCFunction)DBEnv_set_shm_key, METH_VARARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6513 | {"set_cachesize", (PyCFunction)DBEnv_set_cachesize, METH_VARARGS}, |
| 6514 | {"set_data_dir", (PyCFunction)DBEnv_set_data_dir, METH_VARARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6515 | {"set_flags", (PyCFunction)DBEnv_set_flags, METH_VARARGS}, |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 6516 | #if (DBVER >= 47) |
| 6517 | {"log_set_config", (PyCFunction)DBEnv_log_set_config, METH_VARARGS}, |
| 6518 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6519 | {"set_lg_bsize", (PyCFunction)DBEnv_set_lg_bsize, METH_VARARGS}, |
| 6520 | {"set_lg_dir", (PyCFunction)DBEnv_set_lg_dir, METH_VARARGS}, |
| 6521 | {"set_lg_max", (PyCFunction)DBEnv_set_lg_max, METH_VARARGS}, |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6522 | #if (DBVER >= 42) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6523 | {"get_lg_max", (PyCFunction)DBEnv_get_lg_max, METH_NOARGS}, |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6524 | #endif |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 6525 | {"set_lg_regionmax",(PyCFunction)DBEnv_set_lg_regionmax, METH_VARARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6526 | {"set_lk_detect", (PyCFunction)DBEnv_set_lk_detect, METH_VARARGS}, |
Gregory P. Smith | 8b96a35 | 2007-01-05 01:59:42 +0000 | [diff] [blame] | 6527 | #if (DBVER < 45) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6528 | {"set_lk_max", (PyCFunction)DBEnv_set_lk_max, METH_VARARGS}, |
Gregory P. Smith | 8b96a35 | 2007-01-05 01:59:42 +0000 | [diff] [blame] | 6529 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6530 | {"set_lk_max_locks", (PyCFunction)DBEnv_set_lk_max_locks, METH_VARARGS}, |
| 6531 | {"set_lk_max_lockers", (PyCFunction)DBEnv_set_lk_max_lockers, METH_VARARGS}, |
| 6532 | {"set_lk_max_objects", (PyCFunction)DBEnv_set_lk_max_objects, METH_VARARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6533 | {"set_mp_mmapsize", (PyCFunction)DBEnv_set_mp_mmapsize, METH_VARARGS}, |
| 6534 | {"set_tmp_dir", (PyCFunction)DBEnv_set_tmp_dir, METH_VARARGS}, |
| 6535 | {"txn_begin", (PyCFunction)DBEnv_txn_begin, METH_VARARGS|METH_KEYWORDS}, |
| 6536 | {"txn_checkpoint", (PyCFunction)DBEnv_txn_checkpoint, METH_VARARGS}, |
| 6537 | {"txn_stat", (PyCFunction)DBEnv_txn_stat, METH_VARARGS}, |
| 6538 | {"set_tx_max", (PyCFunction)DBEnv_set_tx_max, METH_VARARGS}, |
Gregory P. Smith | 8a47404 | 2006-01-27 07:05:40 +0000 | [diff] [blame] | 6539 | {"set_tx_timestamp", (PyCFunction)DBEnv_set_tx_timestamp, METH_VARARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6540 | {"lock_detect", (PyCFunction)DBEnv_lock_detect, METH_VARARGS}, |
| 6541 | {"lock_get", (PyCFunction)DBEnv_lock_get, METH_VARARGS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6542 | {"lock_id", (PyCFunction)DBEnv_lock_id, METH_NOARGS}, |
Gregory P. Smith | ac11e02 | 2007-11-05 02:56:31 +0000 | [diff] [blame] | 6543 | {"lock_id_free", (PyCFunction)DBEnv_lock_id_free, METH_VARARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6544 | {"lock_put", (PyCFunction)DBEnv_lock_put, METH_VARARGS}, |
| 6545 | {"lock_stat", (PyCFunction)DBEnv_lock_stat, METH_VARARGS}, |
| 6546 | {"log_archive", (PyCFunction)DBEnv_log_archive, METH_VARARGS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6547 | {"log_flush", (PyCFunction)DBEnv_log_flush, METH_NOARGS}, |
Gregory P. Smith | 76a82e8 | 2006-06-05 01:39:52 +0000 | [diff] [blame] | 6548 | {"log_stat", (PyCFunction)DBEnv_log_stat, METH_VARARGS}, |
Gregory P. Smith | db8a807 | 2006-06-05 01:56:15 +0000 | [diff] [blame] | 6549 | #if (DBVER >= 44) |
| 6550 | {"lsn_reset", (PyCFunction)DBEnv_lsn_reset, METH_VARARGS|METH_KEYWORDS}, |
| 6551 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6552 | {"set_get_returns_none",(PyCFunction)DBEnv_set_get_returns_none, METH_VARARGS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6553 | {"txn_recover", (PyCFunction)DBEnv_txn_recover, METH_NOARGS}, |
Matthias Klose | 54cc539 | 2010-03-15 12:46:18 +0000 | [diff] [blame] | 6554 | #if (DBVER < 48) |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 6555 | {"set_rpc_server", (PyCFunction)DBEnv_set_rpc_server, |
| 6556 | METH_VARARGS||METH_KEYWORDS}, |
Matthias Klose | 54cc539 | 2010-03-15 12:46:18 +0000 | [diff] [blame] | 6557 | #endif |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6558 | {"set_verbose", (PyCFunction)DBEnv_set_verbose, METH_VARARGS}, |
| 6559 | #if (DBVER >= 42) |
| 6560 | {"get_verbose", (PyCFunction)DBEnv_get_verbose, METH_VARARGS}, |
| 6561 | #endif |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6562 | {"set_private", (PyCFunction)DBEnv_set_private, METH_O}, |
| 6563 | {"get_private", (PyCFunction)DBEnv_get_private, METH_NOARGS}, |
| 6564 | {"rep_start", (PyCFunction)DBEnv_rep_start, |
| 6565 | METH_VARARGS|METH_KEYWORDS}, |
| 6566 | {"rep_set_transport", (PyCFunction)DBEnv_rep_set_transport, METH_VARARGS}, |
| 6567 | {"rep_process_message", (PyCFunction)DBEnv_rep_process_message, |
| 6568 | METH_VARARGS}, |
| 6569 | #if (DBVER >= 46) |
| 6570 | {"rep_elect", (PyCFunction)DBEnv_rep_elect, METH_VARARGS}, |
| 6571 | #endif |
| 6572 | #if (DBVER >= 44) |
| 6573 | {"rep_set_config", (PyCFunction)DBEnv_rep_set_config, METH_VARARGS}, |
| 6574 | {"rep_get_config", (PyCFunction)DBEnv_rep_get_config, METH_VARARGS}, |
| 6575 | {"rep_sync", (PyCFunction)DBEnv_rep_sync, METH_NOARGS}, |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6576 | #endif |
| 6577 | #if (DBVER >= 45) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6578 | {"rep_set_limit", (PyCFunction)DBEnv_rep_set_limit, METH_VARARGS}, |
| 6579 | {"rep_get_limit", (PyCFunction)DBEnv_rep_get_limit, METH_NOARGS}, |
| 6580 | #endif |
| 6581 | #if (DBVER >= 47) |
| 6582 | {"rep_set_request", (PyCFunction)DBEnv_rep_set_request, METH_VARARGS}, |
| 6583 | {"rep_get_request", (PyCFunction)DBEnv_rep_get_request, METH_NOARGS}, |
| 6584 | #endif |
| 6585 | #if (DBVER >= 45) |
| 6586 | {"set_event_notify", (PyCFunction)DBEnv_set_event_notify, METH_O}, |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6587 | #endif |
| 6588 | #if (DBVER >= 45) |
| 6589 | {"rep_set_nsites", (PyCFunction)DBEnv_rep_set_nsites, METH_VARARGS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6590 | {"rep_get_nsites", (PyCFunction)DBEnv_rep_get_nsites, METH_NOARGS}, |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6591 | {"rep_set_priority", (PyCFunction)DBEnv_rep_set_priority, METH_VARARGS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6592 | {"rep_get_priority", (PyCFunction)DBEnv_rep_get_priority, METH_NOARGS}, |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6593 | {"rep_set_timeout", (PyCFunction)DBEnv_rep_set_timeout, METH_VARARGS}, |
| 6594 | {"rep_get_timeout", (PyCFunction)DBEnv_rep_get_timeout, METH_VARARGS}, |
| 6595 | #endif |
| 6596 | #if (DBVER >= 45) |
| 6597 | {"repmgr_start", (PyCFunction)DBEnv_repmgr_start, |
| 6598 | METH_VARARGS|METH_KEYWORDS}, |
| 6599 | {"repmgr_set_local_site", (PyCFunction)DBEnv_repmgr_set_local_site, |
| 6600 | METH_VARARGS|METH_KEYWORDS}, |
| 6601 | {"repmgr_add_remote_site", (PyCFunction)DBEnv_repmgr_add_remote_site, |
| 6602 | METH_VARARGS|METH_KEYWORDS}, |
| 6603 | {"repmgr_set_ack_policy", (PyCFunction)DBEnv_repmgr_set_ack_policy, |
| 6604 | METH_VARARGS}, |
| 6605 | {"repmgr_get_ack_policy", (PyCFunction)DBEnv_repmgr_get_ack_policy, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6606 | METH_NOARGS}, |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6607 | {"repmgr_site_list", (PyCFunction)DBEnv_repmgr_site_list, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6608 | METH_NOARGS}, |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6609 | #endif |
| 6610 | #if (DBVER >= 46) |
| 6611 | {"repmgr_stat", (PyCFunction)DBEnv_repmgr_stat, |
| 6612 | METH_VARARGS|METH_KEYWORDS}, |
| 6613 | {"repmgr_stat_print", (PyCFunction)DBEnv_repmgr_stat_print, |
| 6614 | METH_VARARGS|METH_KEYWORDS}, |
| 6615 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6616 | {NULL, NULL} /* sentinel */ |
| 6617 | }; |
| 6618 | |
| 6619 | |
| 6620 | static PyMethodDef DBTxn_methods[] = { |
| 6621 | {"commit", (PyCFunction)DBTxn_commit, METH_VARARGS}, |
| 6622 | {"prepare", (PyCFunction)DBTxn_prepare, METH_VARARGS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6623 | {"discard", (PyCFunction)DBTxn_discard, METH_NOARGS}, |
| 6624 | {"abort", (PyCFunction)DBTxn_abort, METH_NOARGS}, |
| 6625 | {"id", (PyCFunction)DBTxn_id, METH_NOARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6626 | {NULL, NULL} /* sentinel */ |
| 6627 | }; |
| 6628 | |
| 6629 | |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6630 | #if (DBVER >= 43) |
| 6631 | static PyMethodDef DBSequence_methods[] = { |
| 6632 | {"close", (PyCFunction)DBSequence_close, METH_VARARGS}, |
| 6633 | {"get", (PyCFunction)DBSequence_get, METH_VARARGS|METH_KEYWORDS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6634 | {"get_dbp", (PyCFunction)DBSequence_get_dbp, METH_NOARGS}, |
| 6635 | {"get_key", (PyCFunction)DBSequence_get_key, METH_NOARGS}, |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6636 | {"init_value", (PyCFunction)DBSequence_init_value, METH_VARARGS}, |
| 6637 | {"open", (PyCFunction)DBSequence_open, METH_VARARGS|METH_KEYWORDS}, |
| 6638 | {"remove", (PyCFunction)DBSequence_remove, METH_VARARGS|METH_KEYWORDS}, |
| 6639 | {"set_cachesize", (PyCFunction)DBSequence_set_cachesize, METH_VARARGS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6640 | {"get_cachesize", (PyCFunction)DBSequence_get_cachesize, METH_NOARGS}, |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6641 | {"set_flags", (PyCFunction)DBSequence_set_flags, METH_VARARGS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6642 | {"get_flags", (PyCFunction)DBSequence_get_flags, METH_NOARGS}, |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6643 | {"set_range", (PyCFunction)DBSequence_set_range, METH_VARARGS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6644 | {"get_range", (PyCFunction)DBSequence_get_range, METH_NOARGS}, |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6645 | {"stat", (PyCFunction)DBSequence_stat, METH_VARARGS|METH_KEYWORDS}, |
| 6646 | {NULL, NULL} /* sentinel */ |
| 6647 | }; |
| 6648 | #endif |
| 6649 | |
| 6650 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6651 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6652 | DBEnv_db_home_get(DBEnvObject* self) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6653 | { |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6654 | const char *home = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6655 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6656 | CHECK_ENV_NOT_CLOSED(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6657 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6658 | #if (DBVER >= 42) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6659 | self->db_env->get_home(self->db_env, &home); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6660 | #else |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6661 | home=self->db_env->db_home; |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 6662 | #endif |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6663 | |
| 6664 | if (home == NULL) { |
| 6665 | RETURN_NONE(); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6666 | } |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6667 | return PyBytes_FromString(home); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6668 | } |
| 6669 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6670 | static PyGetSetDef DBEnv_getsets[] = { |
| 6671 | {"db_home", (getter)DBEnv_db_home_get, NULL,}, |
| 6672 | {NULL} |
| 6673 | }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6674 | |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6675 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6676 | statichere PyTypeObject DB_Type = { |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6677 | #if (PY_VERSION_HEX < 0x03000000) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6678 | PyObject_HEAD_INIT(NULL) |
| 6679 | 0, /*ob_size*/ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6680 | #else |
| 6681 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6682 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6683 | "DB", /*tp_name*/ |
| 6684 | sizeof(DBObject), /*tp_basicsize*/ |
| 6685 | 0, /*tp_itemsize*/ |
| 6686 | /* methods */ |
| 6687 | (destructor)DB_dealloc, /*tp_dealloc*/ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6688 | 0, /*tp_print*/ |
| 6689 | 0, /*tp_getattr*/ |
| 6690 | 0, /*tp_setattr*/ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6691 | 0, /*tp_compare*/ |
| 6692 | 0, /*tp_repr*/ |
| 6693 | 0, /*tp_as_number*/ |
| 6694 | 0, /*tp_as_sequence*/ |
| 6695 | &DB_mapping,/*tp_as_mapping*/ |
| 6696 | 0, /*tp_hash*/ |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 6697 | 0, /* tp_call */ |
| 6698 | 0, /* tp_str */ |
| 6699 | 0, /* tp_getattro */ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6700 | 0, /* tp_setattro */ |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 6701 | 0, /* tp_as_buffer */ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6702 | #if (PY_VERSION_HEX < 0x03000000) |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 6703 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6704 | #else |
| 6705 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 6706 | #endif |
| 6707 | 0, /* tp_doc */ |
| 6708 | 0, /* tp_traverse */ |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 6709 | 0, /* tp_clear */ |
| 6710 | 0, /* tp_richcompare */ |
| 6711 | offsetof(DBObject, in_weakreflist), /* tp_weaklistoffset */ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6712 | 0, /*tp_iter*/ |
| 6713 | 0, /*tp_iternext*/ |
| 6714 | DB_methods, /*tp_methods*/ |
| 6715 | 0, /*tp_members*/ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6716 | }; |
| 6717 | |
| 6718 | |
| 6719 | statichere PyTypeObject DBCursor_Type = { |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6720 | #if (PY_VERSION_HEX < 0x03000000) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6721 | PyObject_HEAD_INIT(NULL) |
| 6722 | 0, /*ob_size*/ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6723 | #else |
| 6724 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6725 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6726 | "DBCursor", /*tp_name*/ |
| 6727 | sizeof(DBCursorObject), /*tp_basicsize*/ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6728 | 0, /*tp_itemsize*/ |
| 6729 | /* methods */ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6730 | (destructor)DBCursor_dealloc,/*tp_dealloc*/ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6731 | 0, /*tp_print*/ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6732 | 0, /*tp_getattr*/ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6733 | 0, /*tp_setattr*/ |
| 6734 | 0, /*tp_compare*/ |
| 6735 | 0, /*tp_repr*/ |
| 6736 | 0, /*tp_as_number*/ |
| 6737 | 0, /*tp_as_sequence*/ |
| 6738 | 0, /*tp_as_mapping*/ |
| 6739 | 0, /*tp_hash*/ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6740 | 0, /*tp_call*/ |
| 6741 | 0, /*tp_str*/ |
| 6742 | 0, /*tp_getattro*/ |
| 6743 | 0, /*tp_setattro*/ |
| 6744 | 0, /*tp_as_buffer*/ |
| 6745 | #if (PY_VERSION_HEX < 0x03000000) |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 6746 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6747 | #else |
| 6748 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 6749 | #endif |
| 6750 | 0, /* tp_doc */ |
| 6751 | 0, /* tp_traverse */ |
| 6752 | 0, /* tp_clear */ |
| 6753 | 0, /* tp_richcompare */ |
| 6754 | offsetof(DBCursorObject, in_weakreflist), /* tp_weaklistoffset */ |
| 6755 | 0, /*tp_iter*/ |
| 6756 | 0, /*tp_iternext*/ |
| 6757 | DBCursor_methods, /*tp_methods*/ |
| 6758 | 0, /*tp_members*/ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6759 | }; |
| 6760 | |
| 6761 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6762 | statichere PyTypeObject DBEnv_Type = { |
| 6763 | #if (PY_VERSION_HEX < 0x03000000) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6764 | PyObject_HEAD_INIT(NULL) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6765 | 0, /*ob_size*/ |
| 6766 | #else |
| 6767 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6768 | #endif |
| 6769 | "DBEnv", /*tp_name*/ |
| 6770 | sizeof(DBEnvObject), /*tp_basicsize*/ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6771 | 0, /*tp_itemsize*/ |
| 6772 | /* methods */ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6773 | (destructor)DBEnv_dealloc, /*tp_dealloc*/ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6774 | 0, /*tp_print*/ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6775 | 0, /*tp_getattr*/ |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6776 | 0, /*tp_setattr*/ |
| 6777 | 0, /*tp_compare*/ |
| 6778 | 0, /*tp_repr*/ |
| 6779 | 0, /*tp_as_number*/ |
| 6780 | 0, /*tp_as_sequence*/ |
| 6781 | 0, /*tp_as_mapping*/ |
| 6782 | 0, /*tp_hash*/ |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6783 | 0, /* tp_call */ |
| 6784 | 0, /* tp_str */ |
| 6785 | 0, /* tp_getattro */ |
| 6786 | 0, /* tp_setattro */ |
| 6787 | 0, /* tp_as_buffer */ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6788 | #if (PY_VERSION_HEX < 0x03000000) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6789 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6790 | #else |
| 6791 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 6792 | #endif |
| 6793 | 0, /* tp_doc */ |
| 6794 | 0, /* tp_traverse */ |
| 6795 | 0, /* tp_clear */ |
| 6796 | 0, /* tp_richcompare */ |
| 6797 | offsetof(DBEnvObject, in_weakreflist), /* tp_weaklistoffset */ |
| 6798 | 0, /* tp_iter */ |
| 6799 | 0, /* tp_iternext */ |
| 6800 | DBEnv_methods, /* tp_methods */ |
| 6801 | 0, /* tp_members */ |
| 6802 | DBEnv_getsets, /* tp_getsets */ |
| 6803 | }; |
| 6804 | |
| 6805 | statichere PyTypeObject DBTxn_Type = { |
| 6806 | #if (PY_VERSION_HEX < 0x03000000) |
| 6807 | PyObject_HEAD_INIT(NULL) |
| 6808 | 0, /*ob_size*/ |
| 6809 | #else |
| 6810 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6811 | #endif |
| 6812 | "DBTxn", /*tp_name*/ |
| 6813 | sizeof(DBTxnObject), /*tp_basicsize*/ |
| 6814 | 0, /*tp_itemsize*/ |
| 6815 | /* methods */ |
| 6816 | (destructor)DBTxn_dealloc, /*tp_dealloc*/ |
| 6817 | 0, /*tp_print*/ |
| 6818 | 0, /*tp_getattr*/ |
| 6819 | 0, /*tp_setattr*/ |
| 6820 | 0, /*tp_compare*/ |
| 6821 | 0, /*tp_repr*/ |
| 6822 | 0, /*tp_as_number*/ |
| 6823 | 0, /*tp_as_sequence*/ |
| 6824 | 0, /*tp_as_mapping*/ |
| 6825 | 0, /*tp_hash*/ |
| 6826 | 0, /* tp_call */ |
| 6827 | 0, /* tp_str */ |
| 6828 | 0, /* tp_getattro */ |
| 6829 | 0, /* tp_setattro */ |
| 6830 | 0, /* tp_as_buffer */ |
| 6831 | #if (PY_VERSION_HEX < 0x03000000) |
| 6832 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
| 6833 | #else |
| 6834 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 6835 | #endif |
| 6836 | 0, /* tp_doc */ |
| 6837 | 0, /* tp_traverse */ |
| 6838 | 0, /* tp_clear */ |
| 6839 | 0, /* tp_richcompare */ |
| 6840 | offsetof(DBTxnObject, in_weakreflist), /* tp_weaklistoffset */ |
| 6841 | 0, /*tp_iter*/ |
| 6842 | 0, /*tp_iternext*/ |
| 6843 | DBTxn_methods, /*tp_methods*/ |
| 6844 | 0, /*tp_members*/ |
| 6845 | }; |
| 6846 | |
| 6847 | |
| 6848 | statichere PyTypeObject DBLock_Type = { |
| 6849 | #if (PY_VERSION_HEX < 0x03000000) |
| 6850 | PyObject_HEAD_INIT(NULL) |
| 6851 | 0, /*ob_size*/ |
| 6852 | #else |
| 6853 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6854 | #endif |
| 6855 | "DBLock", /*tp_name*/ |
| 6856 | sizeof(DBLockObject), /*tp_basicsize*/ |
| 6857 | 0, /*tp_itemsize*/ |
| 6858 | /* methods */ |
| 6859 | (destructor)DBLock_dealloc, /*tp_dealloc*/ |
| 6860 | 0, /*tp_print*/ |
| 6861 | 0, /*tp_getattr*/ |
| 6862 | 0, /*tp_setattr*/ |
| 6863 | 0, /*tp_compare*/ |
| 6864 | 0, /*tp_repr*/ |
| 6865 | 0, /*tp_as_number*/ |
| 6866 | 0, /*tp_as_sequence*/ |
| 6867 | 0, /*tp_as_mapping*/ |
| 6868 | 0, /*tp_hash*/ |
| 6869 | 0, /* tp_call */ |
| 6870 | 0, /* tp_str */ |
| 6871 | 0, /* tp_getattro */ |
| 6872 | 0, /* tp_setattro */ |
| 6873 | 0, /* tp_as_buffer */ |
| 6874 | #if (PY_VERSION_HEX < 0x03000000) |
| 6875 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
| 6876 | #else |
| 6877 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 6878 | #endif |
| 6879 | 0, /* tp_doc */ |
| 6880 | 0, /* tp_traverse */ |
| 6881 | 0, /* tp_clear */ |
| 6882 | 0, /* tp_richcompare */ |
| 6883 | offsetof(DBLockObject, in_weakreflist), /* tp_weaklistoffset */ |
| 6884 | }; |
| 6885 | |
| 6886 | #if (DBVER >= 43) |
| 6887 | statichere PyTypeObject DBSequence_Type = { |
| 6888 | #if (PY_VERSION_HEX < 0x03000000) |
| 6889 | PyObject_HEAD_INIT(NULL) |
| 6890 | 0, /*ob_size*/ |
| 6891 | #else |
| 6892 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6893 | #endif |
| 6894 | "DBSequence", /*tp_name*/ |
| 6895 | sizeof(DBSequenceObject), /*tp_basicsize*/ |
| 6896 | 0, /*tp_itemsize*/ |
| 6897 | /* methods */ |
| 6898 | (destructor)DBSequence_dealloc, /*tp_dealloc*/ |
| 6899 | 0, /*tp_print*/ |
| 6900 | 0, /*tp_getattr*/ |
| 6901 | 0, /*tp_setattr*/ |
| 6902 | 0, /*tp_compare*/ |
| 6903 | 0, /*tp_repr*/ |
| 6904 | 0, /*tp_as_number*/ |
| 6905 | 0, /*tp_as_sequence*/ |
| 6906 | 0, /*tp_as_mapping*/ |
| 6907 | 0, /*tp_hash*/ |
| 6908 | 0, /* tp_call */ |
| 6909 | 0, /* tp_str */ |
| 6910 | 0, /* tp_getattro */ |
| 6911 | 0, /* tp_setattro */ |
| 6912 | 0, /* tp_as_buffer */ |
| 6913 | #if (PY_VERSION_HEX < 0x03000000) |
| 6914 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
| 6915 | #else |
| 6916 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 6917 | #endif |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6918 | 0, /* tp_doc */ |
| 6919 | 0, /* tp_traverse */ |
| 6920 | 0, /* tp_clear */ |
| 6921 | 0, /* tp_richcompare */ |
| 6922 | offsetof(DBSequenceObject, in_weakreflist), /* tp_weaklistoffset */ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6923 | 0, /*tp_iter*/ |
| 6924 | 0, /*tp_iternext*/ |
| 6925 | DBSequence_methods, /*tp_methods*/ |
| 6926 | 0, /*tp_members*/ |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6927 | }; |
| 6928 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6929 | |
| 6930 | /* --------------------------------------------------------------------- */ |
| 6931 | /* Module-level functions */ |
| 6932 | |
| 6933 | static PyObject* |
| 6934 | DB_construct(PyObject* self, PyObject* args, PyObject* kwargs) |
| 6935 | { |
| 6936 | PyObject* dbenvobj = NULL; |
| 6937 | int flags = 0; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 6938 | static char* kwnames[] = { "dbEnv", "flags", NULL}; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6939 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 6940 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oi:DB", kwnames, |
| 6941 | &dbenvobj, &flags)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6942 | return NULL; |
| 6943 | if (dbenvobj == Py_None) |
| 6944 | dbenvobj = NULL; |
| 6945 | else if (dbenvobj && !DBEnvObject_Check(dbenvobj)) { |
| 6946 | makeTypeError("DBEnv", dbenvobj); |
| 6947 | return NULL; |
| 6948 | } |
| 6949 | |
| 6950 | return (PyObject* )newDBObject((DBEnvObject*)dbenvobj, flags); |
| 6951 | } |
| 6952 | |
| 6953 | |
| 6954 | static PyObject* |
| 6955 | DBEnv_construct(PyObject* self, PyObject* args) |
| 6956 | { |
| 6957 | int flags = 0; |
| 6958 | if (!PyArg_ParseTuple(args, "|i:DbEnv", &flags)) return NULL; |
| 6959 | return (PyObject* )newDBEnvObject(flags); |
| 6960 | } |
| 6961 | |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6962 | #if (DBVER >= 43) |
| 6963 | static PyObject* |
| 6964 | DBSequence_construct(PyObject* self, PyObject* args, PyObject* kwargs) |
| 6965 | { |
Neal Norwitz | b4fcf8d | 2006-06-11 05:44:18 +0000 | [diff] [blame] | 6966 | PyObject* dbobj; |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6967 | int flags = 0; |
| 6968 | static char* kwnames[] = { "db", "flags", NULL}; |
| 6969 | |
| 6970 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:DBSequence", kwnames, &dbobj, &flags)) |
| 6971 | return NULL; |
Neal Norwitz | b4fcf8d | 2006-06-11 05:44:18 +0000 | [diff] [blame] | 6972 | if (!DBObject_Check(dbobj)) { |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6973 | makeTypeError("DB", dbobj); |
| 6974 | return NULL; |
| 6975 | } |
| 6976 | return (PyObject* )newDBSequenceObject((DBObject*)dbobj, flags); |
| 6977 | } |
| 6978 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6979 | |
| 6980 | static char bsddb_version_doc[] = |
| 6981 | "Returns a tuple of major, minor, and patch release numbers of the\n\ |
| 6982 | underlying DB library."; |
| 6983 | |
| 6984 | static PyObject* |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6985 | bsddb_version(PyObject* self) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6986 | { |
| 6987 | int major, minor, patch; |
| 6988 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6989 | db_version(&major, &minor, &patch); |
| 6990 | return Py_BuildValue("(iii)", major, minor, patch); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6991 | } |
| 6992 | |
| 6993 | |
| 6994 | /* List of functions defined in the module */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6995 | static PyMethodDef bsddb_methods[] = { |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6996 | {"DB", (PyCFunction)DB_construct, METH_VARARGS | METH_KEYWORDS }, |
| 6997 | {"DBEnv", (PyCFunction)DBEnv_construct, METH_VARARGS}, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 6998 | #if (DBVER >= 43) |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 6999 | {"DBSequence", (PyCFunction)DBSequence_construct, METH_VARARGS | METH_KEYWORDS }, |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7000 | #endif |
| 7001 | {"version", (PyCFunction)bsddb_version, METH_NOARGS, bsddb_version_doc}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7002 | {NULL, NULL} /* sentinel */ |
| 7003 | }; |
| 7004 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7005 | |
Gregory P. Smith | 3925053 | 2007-10-09 06:02:21 +0000 | [diff] [blame] | 7006 | /* API structure */ |
| 7007 | static BSDDB_api bsddb_api; |
| 7008 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7009 | |
| 7010 | /* --------------------------------------------------------------------- */ |
| 7011 | /* Module initialization */ |
| 7012 | |
| 7013 | |
| 7014 | /* Convenience routine to export an integer value. |
| 7015 | * Errors are silently ignored, for better or for worse... |
| 7016 | */ |
| 7017 | #define ADD_INT(dict, NAME) _addIntToDict(dict, #NAME, NAME) |
| 7018 | |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 7019 | #define MODULE_NAME_MAX_LEN 11 |
| 7020 | static char _bsddbModuleName[MODULE_NAME_MAX_LEN+1] = "_bsddb"; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7021 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7022 | #if (PY_VERSION_HEX >= 0x03000000) |
| 7023 | static struct PyModuleDef bsddbmodule = { |
| 7024 | PyModuleDef_HEAD_INIT, |
| 7025 | _bsddbModuleName, /* Name of module */ |
| 7026 | NULL, /* module documentation, may be NULL */ |
| 7027 | -1, /* size of per-interpreter state of the module, |
| 7028 | or -1 if the module keeps state in global variables. */ |
| 7029 | bsddb_methods, |
| 7030 | NULL, /* Reload */ |
| 7031 | NULL, /* Traverse */ |
| 7032 | NULL, /* Clear */ |
| 7033 | NULL /* Free */ |
| 7034 | }; |
| 7035 | #endif |
| 7036 | |
| 7037 | |
| 7038 | #if (PY_VERSION_HEX < 0x03000000) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7039 | DL_EXPORT(void) init_bsddb(void) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7040 | #else |
| 7041 | PyMODINIT_FUNC PyInit__bsddb(void) /* Note the two underscores */ |
| 7042 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7043 | { |
| 7044 | PyObject* m; |
| 7045 | PyObject* d; |
Christian Heimes | 593daf5 | 2008-05-26 12:51:38 +0000 | [diff] [blame] | 7046 | PyObject* pybsddb_version_s = PyBytes_FromString( PY_BSDDB_VERSION ); |
| 7047 | PyObject* db_version_s = PyBytes_FromString( DB_VERSION_STRING ); |
| 7048 | PyObject* cvsid_s = PyBytes_FromString( rcs_id ); |
Gregory P. Smith | 3925053 | 2007-10-09 06:02:21 +0000 | [diff] [blame] | 7049 | PyObject* py_api; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7050 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7051 | /* Initialize object types */ |
| 7052 | if ((PyType_Ready(&DB_Type) < 0) |
| 7053 | || (PyType_Ready(&DBCursor_Type) < 0) |
| 7054 | || (PyType_Ready(&DBEnv_Type) < 0) |
| 7055 | || (PyType_Ready(&DBTxn_Type) < 0) |
| 7056 | || (PyType_Ready(&DBLock_Type) < 0) |
| 7057 | #if (DBVER >= 43) |
| 7058 | || (PyType_Ready(&DBSequence_Type) < 0) |
| 7059 | #endif |
| 7060 | ) { |
| 7061 | #if (PY_VERSION_HEX < 0x03000000) |
| 7062 | return; |
| 7063 | #else |
| 7064 | return NULL; |
| 7065 | #endif |
| 7066 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7067 | |
Mark Hammond | a69d409 | 2003-04-22 23:13:27 +0000 | [diff] [blame] | 7068 | #if defined(WITH_THREAD) && !defined(MYDB_USE_GILSTATE) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7069 | /* Save the current interpreter, so callbacks can do the right thing. */ |
Nicholas Bastin | 2786d90 | 2004-03-25 02:16:23 +0000 | [diff] [blame] | 7070 | _db_interpreterState = PyThreadState_GET()->interp; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7071 | #endif |
| 7072 | |
| 7073 | /* Create the module and add the functions */ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7074 | #if (PY_VERSION_HEX < 0x03000000) |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 7075 | m = Py_InitModule(_bsddbModuleName, bsddb_methods); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7076 | #else |
| 7077 | m=PyModule_Create(&bsddbmodule); |
| 7078 | #endif |
| 7079 | if (m == NULL) { |
| 7080 | #if (PY_VERSION_HEX < 0x03000000) |
| 7081 | return; |
| 7082 | #else |
| 7083 | return NULL; |
| 7084 | #endif |
| 7085 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7086 | |
| 7087 | /* Add some symbolic constants to the module */ |
| 7088 | d = PyModule_GetDict(m); |
| 7089 | PyDict_SetItemString(d, "__version__", pybsddb_version_s); |
| 7090 | PyDict_SetItemString(d, "cvsid", cvsid_s); |
| 7091 | PyDict_SetItemString(d, "DB_VERSION_STRING", db_version_s); |
| 7092 | Py_DECREF(pybsddb_version_s); |
| 7093 | pybsddb_version_s = NULL; |
| 7094 | Py_DECREF(cvsid_s); |
| 7095 | cvsid_s = NULL; |
| 7096 | Py_DECREF(db_version_s); |
| 7097 | db_version_s = NULL; |
| 7098 | |
| 7099 | ADD_INT(d, DB_VERSION_MAJOR); |
| 7100 | ADD_INT(d, DB_VERSION_MINOR); |
| 7101 | ADD_INT(d, DB_VERSION_PATCH); |
| 7102 | |
| 7103 | ADD_INT(d, DB_MAX_PAGES); |
| 7104 | ADD_INT(d, DB_MAX_RECORDS); |
| 7105 | |
Matthias Klose | 54cc539 | 2010-03-15 12:46:18 +0000 | [diff] [blame] | 7106 | #if (DBVER < 48) |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 7107 | #if (DBVER >= 42) |
| 7108 | ADD_INT(d, DB_RPCCLIENT); |
| 7109 | #else |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7110 | ADD_INT(d, DB_CLIENT); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 7111 | /* allow apps to be written using DB_RPCCLIENT on older Berkeley DB */ |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 7112 | _addIntToDict(d, "DB_RPCCLIENT", DB_CLIENT); |
| 7113 | #endif |
Matthias Klose | 54cc539 | 2010-03-15 12:46:18 +0000 | [diff] [blame] | 7114 | #endif |
| 7115 | |
| 7116 | #if (DBVER < 48) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7117 | ADD_INT(d, DB_XA_CREATE); |
Matthias Klose | 54cc539 | 2010-03-15 12:46:18 +0000 | [diff] [blame] | 7118 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7119 | |
| 7120 | ADD_INT(d, DB_CREATE); |
| 7121 | ADD_INT(d, DB_NOMMAP); |
| 7122 | ADD_INT(d, DB_THREAD); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 7123 | #if (DBVER >= 45) |
| 7124 | ADD_INT(d, DB_MULTIVERSION); |
| 7125 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7126 | |
| 7127 | ADD_INT(d, DB_FORCE); |
| 7128 | ADD_INT(d, DB_INIT_CDB); |
| 7129 | ADD_INT(d, DB_INIT_LOCK); |
| 7130 | ADD_INT(d, DB_INIT_LOG); |
| 7131 | ADD_INT(d, DB_INIT_MPOOL); |
| 7132 | ADD_INT(d, DB_INIT_TXN); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7133 | ADD_INT(d, DB_JOINENV); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7134 | |
Matthias Klose | 54cc539 | 2010-03-15 12:46:18 +0000 | [diff] [blame] | 7135 | #if (DBVER >= 48) |
| 7136 | ADD_INT(d, DB_GID_SIZE); |
| 7137 | #else |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 7138 | ADD_INT(d, DB_XIDDATASIZE); |
Matthias Klose | 54cc539 | 2010-03-15 12:46:18 +0000 | [diff] [blame] | 7139 | /* Allow new code to work in old BDB releases */ |
| 7140 | _addIntToDict(d, "DB_GID_SIZE", DB_XIDDATASIZE); |
| 7141 | #endif |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 7142 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7143 | ADD_INT(d, DB_RECOVER); |
| 7144 | ADD_INT(d, DB_RECOVER_FATAL); |
| 7145 | ADD_INT(d, DB_TXN_NOSYNC); |
| 7146 | ADD_INT(d, DB_USE_ENVIRON); |
| 7147 | ADD_INT(d, DB_USE_ENVIRON_ROOT); |
| 7148 | |
| 7149 | ADD_INT(d, DB_LOCKDOWN); |
| 7150 | ADD_INT(d, DB_PRIVATE); |
| 7151 | ADD_INT(d, DB_SYSTEM_MEM); |
| 7152 | |
| 7153 | ADD_INT(d, DB_TXN_SYNC); |
| 7154 | ADD_INT(d, DB_TXN_NOWAIT); |
| 7155 | |
| 7156 | ADD_INT(d, DB_EXCL); |
| 7157 | ADD_INT(d, DB_FCNTL_LOCKING); |
| 7158 | ADD_INT(d, DB_ODDFILESIZE); |
| 7159 | ADD_INT(d, DB_RDWRMASTER); |
| 7160 | ADD_INT(d, DB_RDONLY); |
| 7161 | ADD_INT(d, DB_TRUNCATE); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7162 | ADD_INT(d, DB_EXTENT); |
| 7163 | ADD_INT(d, DB_CDB_ALLDB); |
| 7164 | ADD_INT(d, DB_VERIFY); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7165 | ADD_INT(d, DB_UPGRADE); |
| 7166 | |
| 7167 | ADD_INT(d, DB_AGGRESSIVE); |
| 7168 | ADD_INT(d, DB_NOORDERCHK); |
| 7169 | ADD_INT(d, DB_ORDERCHKONLY); |
| 7170 | ADD_INT(d, DB_PR_PAGE); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7171 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7172 | ADD_INT(d, DB_PR_RECOVERYTEST); |
| 7173 | ADD_INT(d, DB_SALVAGE); |
| 7174 | |
| 7175 | ADD_INT(d, DB_LOCK_NORUN); |
| 7176 | ADD_INT(d, DB_LOCK_DEFAULT); |
| 7177 | ADD_INT(d, DB_LOCK_OLDEST); |
| 7178 | ADD_INT(d, DB_LOCK_RANDOM); |
| 7179 | ADD_INT(d, DB_LOCK_YOUNGEST); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7180 | ADD_INT(d, DB_LOCK_MAXLOCKS); |
| 7181 | ADD_INT(d, DB_LOCK_MINLOCKS); |
| 7182 | ADD_INT(d, DB_LOCK_MINWRITE); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7183 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 7184 | ADD_INT(d, DB_LOCK_EXPIRE); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 7185 | #if (DBVER >= 43) |
| 7186 | ADD_INT(d, DB_LOCK_MAXWRITE); |
| 7187 | #endif |
| 7188 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 7189 | _addIntToDict(d, "DB_LOCK_CONFLICT", 0); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7190 | |
| 7191 | ADD_INT(d, DB_LOCK_DUMP); |
| 7192 | ADD_INT(d, DB_LOCK_GET); |
| 7193 | ADD_INT(d, DB_LOCK_INHERIT); |
| 7194 | ADD_INT(d, DB_LOCK_PUT); |
| 7195 | ADD_INT(d, DB_LOCK_PUT_ALL); |
| 7196 | ADD_INT(d, DB_LOCK_PUT_OBJ); |
| 7197 | |
| 7198 | ADD_INT(d, DB_LOCK_NG); |
| 7199 | ADD_INT(d, DB_LOCK_READ); |
| 7200 | ADD_INT(d, DB_LOCK_WRITE); |
| 7201 | ADD_INT(d, DB_LOCK_NOWAIT); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7202 | ADD_INT(d, DB_LOCK_WAIT); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7203 | ADD_INT(d, DB_LOCK_IWRITE); |
| 7204 | ADD_INT(d, DB_LOCK_IREAD); |
| 7205 | ADD_INT(d, DB_LOCK_IWR); |
Gregory P. Smith | 29602d2 | 2006-01-24 09:46:48 +0000 | [diff] [blame] | 7206 | #if (DBVER < 44) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7207 | ADD_INT(d, DB_LOCK_DIRTY); |
Gregory P. Smith | 29602d2 | 2006-01-24 09:46:48 +0000 | [diff] [blame] | 7208 | #else |
| 7209 | ADD_INT(d, DB_LOCK_READ_UNCOMMITTED); /* renamed in 4.4 */ |
| 7210 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7211 | ADD_INT(d, DB_LOCK_WWRITE); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7212 | |
| 7213 | ADD_INT(d, DB_LOCK_RECORD); |
| 7214 | ADD_INT(d, DB_LOCK_UPGRADE); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7215 | ADD_INT(d, DB_LOCK_SWITCH); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7216 | ADD_INT(d, DB_LOCK_UPGRADE_WRITE); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7217 | |
| 7218 | ADD_INT(d, DB_LOCK_NOWAIT); |
| 7219 | ADD_INT(d, DB_LOCK_RECORD); |
| 7220 | ADD_INT(d, DB_LOCK_UPGRADE); |
| 7221 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7222 | ADD_INT(d, DB_LSTAT_ABORTED); |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 7223 | #if (DBVER < 43) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7224 | ADD_INT(d, DB_LSTAT_ERR); |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 7225 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7226 | ADD_INT(d, DB_LSTAT_FREE); |
| 7227 | ADD_INT(d, DB_LSTAT_HELD); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7228 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7229 | ADD_INT(d, DB_LSTAT_PENDING); |
| 7230 | ADD_INT(d, DB_LSTAT_WAITING); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7231 | |
| 7232 | ADD_INT(d, DB_ARCH_ABS); |
| 7233 | ADD_INT(d, DB_ARCH_DATA); |
| 7234 | ADD_INT(d, DB_ARCH_LOG); |
Gregory P. Smith | 3dd2002 | 2006-06-05 00:31:01 +0000 | [diff] [blame] | 7235 | #if (DBVER >= 42) |
| 7236 | ADD_INT(d, DB_ARCH_REMOVE); |
| 7237 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7238 | |
| 7239 | ADD_INT(d, DB_BTREE); |
| 7240 | ADD_INT(d, DB_HASH); |
| 7241 | ADD_INT(d, DB_RECNO); |
| 7242 | ADD_INT(d, DB_QUEUE); |
| 7243 | ADD_INT(d, DB_UNKNOWN); |
| 7244 | |
| 7245 | ADD_INT(d, DB_DUP); |
| 7246 | ADD_INT(d, DB_DUPSORT); |
| 7247 | ADD_INT(d, DB_RECNUM); |
| 7248 | ADD_INT(d, DB_RENUMBER); |
| 7249 | ADD_INT(d, DB_REVSPLITOFF); |
| 7250 | ADD_INT(d, DB_SNAPSHOT); |
| 7251 | |
| 7252 | ADD_INT(d, DB_JOIN_NOSORT); |
| 7253 | |
| 7254 | ADD_INT(d, DB_AFTER); |
| 7255 | ADD_INT(d, DB_APPEND); |
| 7256 | ADD_INT(d, DB_BEFORE); |
Gregory P. Smith | 8b96a35 | 2007-01-05 01:59:42 +0000 | [diff] [blame] | 7257 | #if (DBVER < 45) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7258 | ADD_INT(d, DB_CACHED_COUNTS); |
Gregory P. Smith | 8b96a35 | 2007-01-05 01:59:42 +0000 | [diff] [blame] | 7259 | #endif |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 7260 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 7261 | #if (DBVER >= 41) |
| 7262 | _addIntToDict(d, "DB_CHECKPOINT", 0); |
| 7263 | #else |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7264 | ADD_INT(d, DB_CHECKPOINT); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 7265 | ADD_INT(d, DB_CURLSN); |
| 7266 | #endif |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7267 | #if (DBVER <= 41) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7268 | ADD_INT(d, DB_COMMIT); |
| 7269 | #endif |
| 7270 | ADD_INT(d, DB_CONSUME); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7271 | ADD_INT(d, DB_CONSUME_WAIT); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7272 | ADD_INT(d, DB_CURRENT); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7273 | ADD_INT(d, DB_FAST_STAT); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7274 | ADD_INT(d, DB_FIRST); |
| 7275 | ADD_INT(d, DB_FLUSH); |
| 7276 | ADD_INT(d, DB_GET_BOTH); |
| 7277 | ADD_INT(d, DB_GET_RECNO); |
| 7278 | ADD_INT(d, DB_JOIN_ITEM); |
| 7279 | ADD_INT(d, DB_KEYFIRST); |
| 7280 | ADD_INT(d, DB_KEYLAST); |
| 7281 | ADD_INT(d, DB_LAST); |
| 7282 | ADD_INT(d, DB_NEXT); |
| 7283 | ADD_INT(d, DB_NEXT_DUP); |
| 7284 | ADD_INT(d, DB_NEXT_NODUP); |
| 7285 | ADD_INT(d, DB_NODUPDATA); |
| 7286 | ADD_INT(d, DB_NOOVERWRITE); |
| 7287 | ADD_INT(d, DB_NOSYNC); |
| 7288 | ADD_INT(d, DB_POSITION); |
| 7289 | ADD_INT(d, DB_PREV); |
| 7290 | ADD_INT(d, DB_PREV_NODUP); |
Gregory P. Smith | 8b96a35 | 2007-01-05 01:59:42 +0000 | [diff] [blame] | 7291 | #if (DBVER < 45) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7292 | ADD_INT(d, DB_RECORDCOUNT); |
Gregory P. Smith | 8b96a35 | 2007-01-05 01:59:42 +0000 | [diff] [blame] | 7293 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7294 | ADD_INT(d, DB_SET); |
| 7295 | ADD_INT(d, DB_SET_RANGE); |
| 7296 | ADD_INT(d, DB_SET_RECNO); |
| 7297 | ADD_INT(d, DB_WRITECURSOR); |
| 7298 | |
| 7299 | ADD_INT(d, DB_OPFLAGS_MASK); |
| 7300 | ADD_INT(d, DB_RMW); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7301 | ADD_INT(d, DB_DIRTY_READ); |
| 7302 | ADD_INT(d, DB_MULTIPLE); |
| 7303 | ADD_INT(d, DB_MULTIPLE_KEY); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7304 | |
Gregory P. Smith | 29602d2 | 2006-01-24 09:46:48 +0000 | [diff] [blame] | 7305 | #if (DBVER >= 44) |
| 7306 | ADD_INT(d, DB_READ_UNCOMMITTED); /* replaces DB_DIRTY_READ in 4.4 */ |
| 7307 | ADD_INT(d, DB_READ_COMMITTED); |
| 7308 | #endif |
| 7309 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7310 | ADD_INT(d, DB_DONOTINDEX); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7311 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 7312 | #if (DBVER >= 41) |
| 7313 | _addIntToDict(d, "DB_INCOMPLETE", 0); |
| 7314 | #else |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7315 | ADD_INT(d, DB_INCOMPLETE); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 7316 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7317 | ADD_INT(d, DB_KEYEMPTY); |
| 7318 | ADD_INT(d, DB_KEYEXIST); |
| 7319 | ADD_INT(d, DB_LOCK_DEADLOCK); |
| 7320 | ADD_INT(d, DB_LOCK_NOTGRANTED); |
| 7321 | ADD_INT(d, DB_NOSERVER); |
| 7322 | ADD_INT(d, DB_NOSERVER_HOME); |
| 7323 | ADD_INT(d, DB_NOSERVER_ID); |
| 7324 | ADD_INT(d, DB_NOTFOUND); |
| 7325 | ADD_INT(d, DB_OLD_VERSION); |
| 7326 | ADD_INT(d, DB_RUNRECOVERY); |
| 7327 | ADD_INT(d, DB_VERIFY_BAD); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7328 | ADD_INT(d, DB_PAGE_NOTFOUND); |
| 7329 | ADD_INT(d, DB_SECONDARY_BAD); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7330 | ADD_INT(d, DB_STAT_CLEAR); |
| 7331 | ADD_INT(d, DB_REGION_INIT); |
| 7332 | ADD_INT(d, DB_NOLOCKING); |
| 7333 | ADD_INT(d, DB_YIELDCPU); |
| 7334 | ADD_INT(d, DB_PANIC_ENVIRONMENT); |
| 7335 | ADD_INT(d, DB_NOPANIC); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7336 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 7337 | #if (DBVER >= 41) |
| 7338 | ADD_INT(d, DB_OVERWRITE); |
| 7339 | #endif |
| 7340 | |
Gregory P. Smith | aae141a | 2007-11-01 21:08:14 +0000 | [diff] [blame] | 7341 | #ifdef DB_REGISTER |
| 7342 | ADD_INT(d, DB_REGISTER); |
| 7343 | #endif |
| 7344 | |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 7345 | #if (DBVER >= 42) |
| 7346 | ADD_INT(d, DB_TIME_NOTGRANTED); |
| 7347 | ADD_INT(d, DB_TXN_NOT_DURABLE); |
| 7348 | ADD_INT(d, DB_TXN_WRITE_NOSYNC); |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 7349 | ADD_INT(d, DB_DIRECT_DB); |
| 7350 | ADD_INT(d, DB_INIT_REP); |
| 7351 | ADD_INT(d, DB_ENCRYPT); |
| 7352 | ADD_INT(d, DB_CHKSUM); |
| 7353 | #endif |
| 7354 | |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 7355 | #if (DBVER >= 42) && (DBVER < 47) |
| 7356 | ADD_INT(d, DB_LOG_AUTOREMOVE); |
| 7357 | ADD_INT(d, DB_DIRECT_LOG); |
| 7358 | #endif |
| 7359 | |
| 7360 | #if (DBVER >= 47) |
| 7361 | ADD_INT(d, DB_LOG_DIRECT); |
| 7362 | ADD_INT(d, DB_LOG_DSYNC); |
| 7363 | ADD_INT(d, DB_LOG_IN_MEMORY); |
| 7364 | ADD_INT(d, DB_LOG_AUTO_REMOVE); |
| 7365 | ADD_INT(d, DB_LOG_ZERO); |
| 7366 | #endif |
| 7367 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 7368 | #if (DBVER >= 44) |
| 7369 | ADD_INT(d, DB_DSYNC_DB); |
| 7370 | #endif |
| 7371 | |
| 7372 | #if (DBVER >= 45) |
| 7373 | ADD_INT(d, DB_TXN_SNAPSHOT); |
| 7374 | #endif |
| 7375 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 7376 | ADD_INT(d, DB_VERB_DEADLOCK); |
| 7377 | #if (DBVER >= 46) |
| 7378 | ADD_INT(d, DB_VERB_FILEOPS); |
| 7379 | ADD_INT(d, DB_VERB_FILEOPS_ALL); |
| 7380 | #endif |
| 7381 | ADD_INT(d, DB_VERB_RECOVERY); |
| 7382 | #if (DBVER >= 44) |
| 7383 | ADD_INT(d, DB_VERB_REGISTER); |
| 7384 | #endif |
| 7385 | ADD_INT(d, DB_VERB_REPLICATION); |
| 7386 | ADD_INT(d, DB_VERB_WAITSFOR); |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 7387 | |
| 7388 | #if (DBVER >= 45) |
| 7389 | ADD_INT(d, DB_EVENT_PANIC); |
| 7390 | ADD_INT(d, DB_EVENT_REP_CLIENT); |
| 7391 | #if (DBVER >= 46) |
| 7392 | ADD_INT(d, DB_EVENT_REP_ELECTED); |
| 7393 | #endif |
| 7394 | ADD_INT(d, DB_EVENT_REP_MASTER); |
| 7395 | ADD_INT(d, DB_EVENT_REP_NEWMASTER); |
| 7396 | #if (DBVER >= 46) |
| 7397 | ADD_INT(d, DB_EVENT_REP_PERM_FAILED); |
| 7398 | #endif |
| 7399 | ADD_INT(d, DB_EVENT_REP_STARTUPDONE); |
| 7400 | ADD_INT(d, DB_EVENT_WRITE_FAILED); |
| 7401 | #endif |
| 7402 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7403 | ADD_INT(d, DB_REP_DUPMASTER); |
| 7404 | ADD_INT(d, DB_REP_HOLDELECTION); |
| 7405 | #if (DBVER >= 44) |
| 7406 | ADD_INT(d, DB_REP_IGNORE); |
| 7407 | ADD_INT(d, DB_REP_JOIN_FAILURE); |
| 7408 | #endif |
| 7409 | #if (DBVER >= 42) |
| 7410 | ADD_INT(d, DB_REP_ISPERM); |
| 7411 | ADD_INT(d, DB_REP_NOTPERM); |
| 7412 | #endif |
| 7413 | ADD_INT(d, DB_REP_NEWSITE); |
| 7414 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 7415 | ADD_INT(d, DB_REP_MASTER); |
| 7416 | ADD_INT(d, DB_REP_CLIENT); |
| 7417 | #if (DBVER >= 45) |
| 7418 | ADD_INT(d, DB_REP_ELECTION); |
| 7419 | |
| 7420 | ADD_INT(d, DB_REP_ACK_TIMEOUT); |
| 7421 | ADD_INT(d, DB_REP_CONNECTION_RETRY); |
| 7422 | ADD_INT(d, DB_REP_ELECTION_TIMEOUT); |
| 7423 | ADD_INT(d, DB_REP_ELECTION_RETRY); |
| 7424 | #endif |
| 7425 | #if (DBVER >= 46) |
| 7426 | ADD_INT(d, DB_REP_CHECKPOINT_DELAY); |
| 7427 | ADD_INT(d, DB_REP_FULL_ELECTION_TIMEOUT); |
| 7428 | #endif |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 7429 | |
| 7430 | #if (DBVER >= 45) |
| 7431 | ADD_INT(d, DB_REPMGR_PEER); |
| 7432 | ADD_INT(d, DB_REPMGR_ACKS_ALL); |
| 7433 | ADD_INT(d, DB_REPMGR_ACKS_ALL_PEERS); |
| 7434 | ADD_INT(d, DB_REPMGR_ACKS_NONE); |
| 7435 | ADD_INT(d, DB_REPMGR_ACKS_ONE); |
| 7436 | ADD_INT(d, DB_REPMGR_ACKS_ONE_PEER); |
| 7437 | ADD_INT(d, DB_REPMGR_ACKS_QUORUM); |
| 7438 | ADD_INT(d, DB_REPMGR_CONNECTED); |
| 7439 | ADD_INT(d, DB_REPMGR_DISCONNECTED); |
| 7440 | ADD_INT(d, DB_STAT_CLEAR); |
| 7441 | ADD_INT(d, DB_STAT_ALL); |
| 7442 | #endif |
| 7443 | |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 7444 | #if (DBVER >= 43) |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 7445 | ADD_INT(d, DB_BUFFER_SMALL); |
Gregory P. Smith | f0547d0 | 2006-06-05 17:38:04 +0000 | [diff] [blame] | 7446 | ADD_INT(d, DB_SEQ_DEC); |
| 7447 | ADD_INT(d, DB_SEQ_INC); |
| 7448 | ADD_INT(d, DB_SEQ_WRAP); |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 7449 | #endif |
| 7450 | |
Jesus Cea | ca3939c | 2008-05-22 15:27:38 +0000 | [diff] [blame] | 7451 | #if (DBVER >= 43) && (DBVER < 47) |
| 7452 | ADD_INT(d, DB_LOG_INMEMORY); |
| 7453 | ADD_INT(d, DB_DSYNC_LOG); |
| 7454 | #endif |
| 7455 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 7456 | #if (DBVER >= 41) |
| 7457 | ADD_INT(d, DB_ENCRYPT_AES); |
| 7458 | ADD_INT(d, DB_AUTO_COMMIT); |
| 7459 | #else |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 7460 | /* allow Berkeley DB 4.1 aware apps to run on older versions */ |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 7461 | _addIntToDict(d, "DB_AUTO_COMMIT", 0); |
| 7462 | #endif |
| 7463 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7464 | ADD_INT(d, EINVAL); |
| 7465 | ADD_INT(d, EACCES); |
| 7466 | ADD_INT(d, ENOSPC); |
| 7467 | ADD_INT(d, ENOMEM); |
| 7468 | ADD_INT(d, EAGAIN); |
| 7469 | ADD_INT(d, EBUSY); |
| 7470 | ADD_INT(d, EEXIST); |
| 7471 | ADD_INT(d, ENOENT); |
| 7472 | ADD_INT(d, EPERM); |
| 7473 | |
Barry Warsaw | 1baa982 | 2003-03-31 19:51:29 +0000 | [diff] [blame] | 7474 | ADD_INT(d, DB_SET_LOCK_TIMEOUT); |
| 7475 | ADD_INT(d, DB_SET_TXN_TIMEOUT); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7476 | |
Gregory P. Smith | 7f5b6f4 | 2006-04-08 07:10:51 +0000 | [diff] [blame] | 7477 | /* The exception name must be correct for pickled exception * |
| 7478 | * objects to unpickle properly. */ |
| 7479 | #ifdef PYBSDDB_STANDALONE /* different value needed for standalone pybsddb */ |
| 7480 | #define PYBSDDB_EXCEPTION_BASE "bsddb3.db." |
| 7481 | #else |
| 7482 | #define PYBSDDB_EXCEPTION_BASE "bsddb.db." |
| 7483 | #endif |
| 7484 | |
| 7485 | /* All the rest of the exceptions derive only from DBError */ |
| 7486 | #define MAKE_EX(name) name = PyErr_NewException(PYBSDDB_EXCEPTION_BASE #name, DBError, NULL); \ |
| 7487 | PyDict_SetItemString(d, #name, name) |
| 7488 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7489 | /* The base exception class is DBError */ |
Gregory P. Smith | 7f5b6f4 | 2006-04-08 07:10:51 +0000 | [diff] [blame] | 7490 | DBError = NULL; /* used in MAKE_EX so that it derives from nothing */ |
| 7491 | MAKE_EX(DBError); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7492 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7493 | #if (PY_VERSION_HEX < 0x03000000) |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 7494 | /* Some magic to make DBNotFoundError and DBKeyEmptyError derive |
| 7495 | * from both DBError and KeyError, since the API only supports |
| 7496 | * using one base class. */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7497 | PyDict_SetItemString(d, "KeyError", PyExc_KeyError); |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 7498 | PyRun_String("class DBNotFoundError(DBError, KeyError): pass\n" |
| 7499 | "class DBKeyEmptyError(DBError, KeyError): pass", |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7500 | Py_file_input, d, d); |
| 7501 | DBNotFoundError = PyDict_GetItemString(d, "DBNotFoundError"); |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 7502 | DBKeyEmptyError = PyDict_GetItemString(d, "DBKeyEmptyError"); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7503 | PyDict_DelItemString(d, "KeyError"); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7504 | #else |
| 7505 | /* Since Python 2.5, PyErr_NewException() accepts a tuple, to be able to |
| 7506 | ** derive from several classes. We use this new API only for Python 3.0, |
| 7507 | ** though. |
| 7508 | */ |
| 7509 | { |
| 7510 | PyObject* bases; |
| 7511 | |
| 7512 | bases = PyTuple_Pack(2, DBError, PyExc_KeyError); |
| 7513 | |
| 7514 | #define MAKE_EX2(name) name = PyErr_NewException(PYBSDDB_EXCEPTION_BASE #name, bases, NULL); \ |
| 7515 | PyDict_SetItemString(d, #name, name) |
| 7516 | MAKE_EX2(DBNotFoundError); |
| 7517 | MAKE_EX2(DBKeyEmptyError); |
| 7518 | |
| 7519 | #undef MAKE_EX2 |
| 7520 | |
| 7521 | Py_XDECREF(bases); |
| 7522 | } |
| 7523 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7524 | |
| 7525 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7526 | #if !INCOMPLETE_IS_WARNING |
| 7527 | MAKE_EX(DBIncompleteError); |
| 7528 | #endif |
Gregory P. Smith | e276717 | 2003-11-02 08:06:29 +0000 | [diff] [blame] | 7529 | MAKE_EX(DBCursorClosedError); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7530 | MAKE_EX(DBKeyEmptyError); |
| 7531 | MAKE_EX(DBKeyExistError); |
| 7532 | MAKE_EX(DBLockDeadlockError); |
| 7533 | MAKE_EX(DBLockNotGrantedError); |
| 7534 | MAKE_EX(DBOldVersionError); |
| 7535 | MAKE_EX(DBRunRecoveryError); |
| 7536 | MAKE_EX(DBVerifyBadError); |
| 7537 | MAKE_EX(DBNoServerError); |
| 7538 | MAKE_EX(DBNoServerHomeError); |
| 7539 | MAKE_EX(DBNoServerIDError); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7540 | MAKE_EX(DBPageNotFoundError); |
| 7541 | MAKE_EX(DBSecondaryBadError); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7542 | |
| 7543 | MAKE_EX(DBInvalidArgError); |
| 7544 | MAKE_EX(DBAccessError); |
| 7545 | MAKE_EX(DBNoSpaceError); |
| 7546 | MAKE_EX(DBNoMemoryError); |
| 7547 | MAKE_EX(DBAgainError); |
| 7548 | MAKE_EX(DBBusyError); |
| 7549 | MAKE_EX(DBFileExistsError); |
| 7550 | MAKE_EX(DBNoSuchFileError); |
| 7551 | MAKE_EX(DBPermissionsError); |
| 7552 | |
Jesus Cea | ef9764f | 2008-05-13 18:45:46 +0000 | [diff] [blame] | 7553 | #if (DBVER >= 42) |
| 7554 | MAKE_EX(DBRepHandleDeadError); |
| 7555 | #endif |
| 7556 | |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7557 | MAKE_EX(DBRepUnavailError); |
| 7558 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7559 | #undef MAKE_EX |
| 7560 | |
Gregory P. Smith | 3925053 | 2007-10-09 06:02:21 +0000 | [diff] [blame] | 7561 | /* Initiliase the C API structure and add it to the module */ |
| 7562 | bsddb_api.db_type = &DB_Type; |
| 7563 | bsddb_api.dbcursor_type = &DBCursor_Type; |
| 7564 | bsddb_api.dbenv_type = &DBEnv_Type; |
| 7565 | bsddb_api.dbtxn_type = &DBTxn_Type; |
| 7566 | bsddb_api.dblock_type = &DBLock_Type; |
Neal Norwitz | f3ca169 | 2007-10-12 03:52:34 +0000 | [diff] [blame] | 7567 | #if (DBVER >= 43) |
Gregory P. Smith | 3925053 | 2007-10-09 06:02:21 +0000 | [diff] [blame] | 7568 | bsddb_api.dbsequence_type = &DBSequence_Type; |
Neal Norwitz | f3ca169 | 2007-10-12 03:52:34 +0000 | [diff] [blame] | 7569 | #endif |
Gregory P. Smith | 3925053 | 2007-10-09 06:02:21 +0000 | [diff] [blame] | 7570 | bsddb_api.makeDBError = makeDBError; |
| 7571 | |
| 7572 | py_api = PyCObject_FromVoidPtr((void*)&bsddb_api, NULL); |
| 7573 | PyDict_SetItemString(d, "api", py_api); |
| 7574 | Py_DECREF(py_api); |
| 7575 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7576 | /* Check for errors */ |
| 7577 | if (PyErr_Occurred()) { |
| 7578 | PyErr_Print(); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7579 | Py_FatalError("can't initialize module _bsddb/_pybsddb"); |
| 7580 | Py_DECREF(m); |
| 7581 | m = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7582 | } |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7583 | #if (PY_VERSION_HEX < 0x03000000) |
| 7584 | return; |
| 7585 | #else |
| 7586 | return m; |
| 7587 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 7588 | } |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 7589 | |
| 7590 | /* allow this module to be named _pybsddb so that it can be installed |
| 7591 | * and imported on top of python >= 2.3 that includes its own older |
| 7592 | * copy of the library named _bsddb without importing the old version. */ |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7593 | #if (PY_VERSION_HEX < 0x03000000) |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 7594 | DL_EXPORT(void) init_pybsddb(void) |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7595 | #else |
| 7596 | PyMODINIT_FUNC PyInit__pybsddb(void) /* Note the two underscores */ |
| 7597 | #endif |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 7598 | { |
| 7599 | strncpy(_bsddbModuleName, "_pybsddb", MODULE_NAME_MAX_LEN); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7600 | #if (PY_VERSION_HEX < 0x03000000) |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 7601 | init_bsddb(); |
Jesus Cea | c5a11fa | 2008-07-23 11:38:42 +0000 | [diff] [blame] | 7602 | #else |
| 7603 | return PyInit__bsddb(); /* Note the two underscores */ |
| 7604 | #endif |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 7605 | } |