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 |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 39 | * to compile with BerkeleyDB 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 |
| 42 | * on SWIG in a package by Gregory P. Smith <greg@electricrain.com> who |
| 43 | * based his work on a similar package by Robin Dunn <robin@alldunn.com> |
| 44 | * which wrapped Berkeley DB 2.7.x. |
| 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 | * |
| 51 | * Gregory P. Smith <greg@electricrain.com> is once again the maintainer. |
| 52 | * |
| 53 | * Use the pybsddb-users@lists.sf.net mailing list for all questions. |
Barry Warsaw | c74e4a5 | 2003-04-24 14:28:08 +0000 | [diff] [blame] | 54 | * Things can change faster than the header of this file is updated. This |
| 55 | * file is shared with the PyBSDDB project at SourceForge: |
| 56 | * |
| 57 | * http://pybsddb.sf.net |
| 58 | * |
| 59 | * This file should remain backward compatible with Python 2.1, but see PEP |
| 60 | * 291 for the most current backward compatibility requirements: |
| 61 | * |
| 62 | * http://www.python.org/peps/pep-0291.html |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 63 | * |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 64 | * This module contains 6 types: |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 65 | * |
| 66 | * DB (Database) |
| 67 | * DBCursor (Database Cursor) |
| 68 | * DBEnv (database environment) |
| 69 | * DBTxn (An explicit database transaction) |
| 70 | * DBLock (A lock handle) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 71 | * DBSequence (Sequence) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 72 | * |
| 73 | */ |
| 74 | |
| 75 | /* --------------------------------------------------------------------- */ |
| 76 | |
| 77 | /* |
| 78 | * Portions of this module, associated unit tests and build scripts are the |
| 79 | * result of a contract with The Written Word (http://thewrittenword.com/) |
| 80 | * Many thanks go out to them for causing me to raise the bar on quality and |
| 81 | * functionality, resulting in a better bsddb3 package for all of us to use. |
| 82 | * |
| 83 | * --Robin |
| 84 | */ |
| 85 | |
| 86 | /* --------------------------------------------------------------------- */ |
| 87 | |
Gregory P. Smith | a703a21 | 2003-11-03 01:04:41 +0000 | [diff] [blame] | 88 | #include <stddef.h> /* for offsetof() */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 89 | #include <Python.h> |
| 90 | #include <db.h> |
| 91 | |
| 92 | /* --------------------------------------------------------------------- */ |
| 93 | /* Various macro definitions */ |
| 94 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 95 | /* 40 = 4.0, 33 = 3.3; this will break if the second number is > 9 */ |
| 96 | #define DBVER (DB_VERSION_MAJOR * 10 + DB_VERSION_MINOR) |
Gregory P. Smith | a703a21 | 2003-11-03 01:04:41 +0000 | [diff] [blame] | 97 | #if DB_VERSION_MINOR > 9 |
| 98 | #error "eek! DBVER can't handle minor versions > 9" |
| 99 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 100 | |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 101 | #define PY_BSDDB_VERSION "4.5.0" |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 102 | static char *rcs_id = "$Id$"; |
| 103 | |
| 104 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 105 | #if (PY_VERSION_HEX < 0x02050000) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 106 | typedef int Py_ssize_t; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 107 | #endif |
| 108 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 109 | #ifdef WITH_THREAD |
| 110 | |
| 111 | /* These are for when calling Python --> C */ |
| 112 | #define MYDB_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS; |
| 113 | #define MYDB_END_ALLOW_THREADS Py_END_ALLOW_THREADS; |
| 114 | |
Mark Hammond | a69d409 | 2003-04-22 23:13:27 +0000 | [diff] [blame] | 115 | /* For 2.3, use the PyGILState_ calls */ |
| 116 | #if (PY_VERSION_HEX >= 0x02030000) |
| 117 | #define MYDB_USE_GILSTATE |
| 118 | #endif |
| 119 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 120 | /* and these are for calling C --> Python */ |
Mark Hammond | a69d409 | 2003-04-22 23:13:27 +0000 | [diff] [blame] | 121 | #if defined(MYDB_USE_GILSTATE) |
| 122 | #define MYDB_BEGIN_BLOCK_THREADS \ |
| 123 | PyGILState_STATE __savestate = PyGILState_Ensure(); |
| 124 | #define MYDB_END_BLOCK_THREADS \ |
| 125 | PyGILState_Release(__savestate); |
| 126 | #else /* MYDB_USE_GILSTATE */ |
| 127 | /* Pre GILState API - do it the long old way */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 128 | static PyInterpreterState* _db_interpreterState = NULL; |
| 129 | #define MYDB_BEGIN_BLOCK_THREADS { \ |
| 130 | PyThreadState* prevState; \ |
| 131 | PyThreadState* newState; \ |
| 132 | PyEval_AcquireLock(); \ |
| 133 | newState = PyThreadState_New(_db_interpreterState); \ |
| 134 | prevState = PyThreadState_Swap(newState); |
| 135 | |
| 136 | #define MYDB_END_BLOCK_THREADS \ |
| 137 | newState = PyThreadState_Swap(prevState); \ |
| 138 | PyThreadState_Clear(newState); \ |
| 139 | PyEval_ReleaseLock(); \ |
| 140 | PyThreadState_Delete(newState); \ |
| 141 | } |
Mark Hammond | a69d409 | 2003-04-22 23:13:27 +0000 | [diff] [blame] | 142 | #endif /* MYDB_USE_GILSTATE */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 143 | |
| 144 | #else |
Mark Hammond | a69d409 | 2003-04-22 23:13:27 +0000 | [diff] [blame] | 145 | /* Compiled without threads - avoid all this cruft */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 146 | #define MYDB_BEGIN_ALLOW_THREADS |
| 147 | #define MYDB_END_ALLOW_THREADS |
| 148 | #define MYDB_BEGIN_BLOCK_THREADS |
| 149 | #define MYDB_END_BLOCK_THREADS |
| 150 | |
| 151 | #endif |
| 152 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 153 | /* Should DB_INCOMPLETE be turned into a warning or an exception? */ |
| 154 | #define INCOMPLETE_IS_WARNING 1 |
| 155 | |
| 156 | /* --------------------------------------------------------------------- */ |
| 157 | /* Exceptions */ |
| 158 | |
| 159 | static PyObject* DBError; /* Base class, all others derive from this */ |
Gregory P. Smith | e276717 | 2003-11-02 08:06:29 +0000 | [diff] [blame] | 160 | 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] | 161 | static PyObject* DBKeyEmptyError; /* DB_KEYEMPTY: also derives from KeyError */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 162 | static PyObject* DBKeyExistError; /* DB_KEYEXIST */ |
| 163 | static PyObject* DBLockDeadlockError; /* DB_LOCK_DEADLOCK */ |
| 164 | static PyObject* DBLockNotGrantedError; /* DB_LOCK_NOTGRANTED */ |
| 165 | static PyObject* DBNotFoundError; /* DB_NOTFOUND: also derives from KeyError */ |
| 166 | static PyObject* DBOldVersionError; /* DB_OLD_VERSION */ |
| 167 | static PyObject* DBRunRecoveryError; /* DB_RUNRECOVERY */ |
| 168 | static PyObject* DBVerifyBadError; /* DB_VERIFY_BAD */ |
| 169 | static PyObject* DBNoServerError; /* DB_NOSERVER */ |
| 170 | static PyObject* DBNoServerHomeError; /* DB_NOSERVER_HOME */ |
| 171 | static PyObject* DBNoServerIDError; /* DB_NOSERVER_ID */ |
| 172 | #if (DBVER >= 33) |
| 173 | static PyObject* DBPageNotFoundError; /* DB_PAGE_NOTFOUND */ |
| 174 | static PyObject* DBSecondaryBadError; /* DB_SECONDARY_BAD */ |
| 175 | #endif |
| 176 | |
| 177 | #if !INCOMPLETE_IS_WARNING |
| 178 | static PyObject* DBIncompleteError; /* DB_INCOMPLETE */ |
| 179 | #endif |
| 180 | |
| 181 | static PyObject* DBInvalidArgError; /* EINVAL */ |
| 182 | static PyObject* DBAccessError; /* EACCES */ |
| 183 | static PyObject* DBNoSpaceError; /* ENOSPC */ |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 184 | static PyObject* DBNoMemoryError; /* DB_BUFFER_SMALL (ENOMEM when < 4.3) */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 185 | static PyObject* DBAgainError; /* EAGAIN */ |
| 186 | static PyObject* DBBusyError; /* EBUSY */ |
| 187 | static PyObject* DBFileExistsError; /* EEXIST */ |
| 188 | static PyObject* DBNoSuchFileError; /* ENOENT */ |
| 189 | static PyObject* DBPermissionsError; /* EPERM */ |
| 190 | |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 191 | #if (DBVER < 43) |
| 192 | #define DB_BUFFER_SMALL ENOMEM |
| 193 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 194 | |
| 195 | |
| 196 | /* --------------------------------------------------------------------- */ |
| 197 | /* Structure definitions */ |
| 198 | |
Gregory P. Smith | a703a21 | 2003-11-03 01:04:41 +0000 | [diff] [blame] | 199 | #if PYTHON_API_VERSION >= 1010 /* python >= 2.1 support weak references */ |
| 200 | #define HAVE_WEAKREF |
| 201 | #else |
| 202 | #undef HAVE_WEAKREF |
| 203 | #endif |
| 204 | |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 205 | /* if Python >= 2.1 better support warnings */ |
| 206 | #if PYTHON_API_VERSION >= 1010 |
| 207 | #define HAVE_WARNINGS |
| 208 | #else |
| 209 | #undef HAVE_WARNINGS |
| 210 | #endif |
| 211 | |
Neal Norwitz | b4a5581 | 2004-07-09 23:30:57 +0000 | [diff] [blame] | 212 | #if PYTHON_API_VERSION <= 1007 |
| 213 | /* 1.5 compatibility */ |
| 214 | #define PyObject_New PyObject_NEW |
| 215 | #define PyObject_Del PyMem_DEL |
| 216 | #endif |
| 217 | |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 218 | struct behaviourFlags { |
| 219 | /* What is the default behaviour when DB->get or DBCursor->get returns a |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 220 | DB_NOTFOUND || DB_KEYEMPTY error? Return None or raise an exception? */ |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 221 | unsigned int getReturnsNone : 1; |
| 222 | /* What is the default behaviour for DBCursor.set* methods when DBCursor->get |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 223 | * returns a DB_NOTFOUND || DB_KEYEMPTY error? Return None or raise? */ |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 224 | unsigned int cursorSetReturnsNone : 1; |
| 225 | }; |
| 226 | |
| 227 | #define DEFAULT_GET_RETURNS_NONE 1 |
Gregory P. Smith | a703a21 | 2003-11-03 01:04:41 +0000 | [diff] [blame] | 228 | #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] | 229 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 230 | typedef struct { |
| 231 | PyObject_HEAD |
| 232 | DB_ENV* db_env; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 233 | u_int32_t flags; /* saved flags from open() */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 234 | int closed; |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 235 | struct behaviourFlags moduleFlags; |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 236 | #ifdef HAVE_WEAKREF |
| 237 | PyObject *in_weakreflist; /* List of weak references */ |
| 238 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 239 | } DBEnvObject; |
| 240 | |
| 241 | |
| 242 | typedef struct { |
| 243 | PyObject_HEAD |
| 244 | DB* db; |
| 245 | DBEnvObject* myenvobj; /* PyObject containing the DB_ENV */ |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 246 | u_int32_t flags; /* saved flags from open() */ |
| 247 | u_int32_t setflags; /* saved flags from set_flags() */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 248 | int haveStat; |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 249 | struct behaviourFlags moduleFlags; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 250 | #if (DBVER >= 33) |
| 251 | PyObject* associateCallback; |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 252 | PyObject* btCompareCallback; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 253 | int primaryDBType; |
| 254 | #endif |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 255 | #ifdef HAVE_WEAKREF |
| 256 | PyObject *in_weakreflist; /* List of weak references */ |
| 257 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 258 | } DBObject; |
| 259 | |
| 260 | |
| 261 | typedef struct { |
| 262 | PyObject_HEAD |
| 263 | DBC* dbc; |
| 264 | DBObject* mydb; |
Gregory P. Smith | a703a21 | 2003-11-03 01:04:41 +0000 | [diff] [blame] | 265 | #ifdef HAVE_WEAKREF |
| 266 | PyObject *in_weakreflist; /* List of weak references */ |
| 267 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 268 | } DBCursorObject; |
| 269 | |
| 270 | |
| 271 | typedef struct { |
| 272 | PyObject_HEAD |
| 273 | DB_TXN* txn; |
Neal Norwitz | 62a2112 | 2006-01-25 05:21:55 +0000 | [diff] [blame] | 274 | PyObject *env; |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 275 | #ifdef HAVE_WEAKREF |
| 276 | PyObject *in_weakreflist; /* List of weak references */ |
| 277 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 278 | } DBTxnObject; |
| 279 | |
| 280 | |
| 281 | typedef struct { |
| 282 | PyObject_HEAD |
| 283 | DB_LOCK lock; |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 284 | #ifdef HAVE_WEAKREF |
| 285 | PyObject *in_weakreflist; /* List of weak references */ |
| 286 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 287 | } DBLockObject; |
| 288 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 289 | #if (DBVER >= 43) |
| 290 | typedef struct { |
| 291 | PyObject_HEAD |
| 292 | DB_SEQUENCE* sequence; |
| 293 | DBObject* mydb; |
| 294 | #ifdef HAVE_WEAKREF |
| 295 | PyObject *in_weakreflist; /* List of weak references */ |
| 296 | #endif |
| 297 | } DBSequenceObject; |
| 298 | static PyTypeObject DBSequence_Type; |
| 299 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 300 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 301 | static PyTypeObject DB_Type, DBCursor_Type, DBEnv_Type, DBTxn_Type, DBLock_Type; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 302 | |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame^] | 303 | #define DBObject_Check(v) (Py_Type(v) == &DB_Type) |
| 304 | #define DBCursorObject_Check(v) (Py_Type(v) == &DBCursor_Type) |
| 305 | #define DBEnvObject_Check(v) (Py_Type(v) == &DBEnv_Type) |
| 306 | #define DBTxnObject_Check(v) (Py_Type(v) == &DBTxn_Type) |
| 307 | #define DBLockObject_Check(v) (Py_Type(v) == &DBLock_Type) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 308 | #if (DBVER >= 43) |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame^] | 309 | #define DBSequenceObject_Check(v) (Py_Type(v) == &DBSequence_Type) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 310 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 311 | |
| 312 | |
| 313 | /* --------------------------------------------------------------------- */ |
| 314 | /* Utility macros and functions */ |
| 315 | |
| 316 | #define RETURN_IF_ERR() \ |
| 317 | if (makeDBError(err)) { \ |
| 318 | return NULL; \ |
| 319 | } |
| 320 | |
| 321 | #define RETURN_NONE() Py_INCREF(Py_None); return Py_None; |
| 322 | |
Gregory P. Smith | e276717 | 2003-11-02 08:06:29 +0000 | [diff] [blame] | 323 | #define _CHECK_OBJECT_NOT_CLOSED(nonNull, pyErrObj, name) \ |
| 324 | if ((nonNull) == NULL) { \ |
| 325 | PyObject *errTuple = NULL; \ |
| 326 | errTuple = Py_BuildValue("(is)", 0, #name " object has been closed"); \ |
| 327 | PyErr_SetObject((pyErrObj), errTuple); \ |
| 328 | Py_DECREF(errTuple); \ |
| 329 | return NULL; \ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Gregory P. Smith | e276717 | 2003-11-02 08:06:29 +0000 | [diff] [blame] | 332 | #define CHECK_DB_NOT_CLOSED(dbobj) \ |
| 333 | _CHECK_OBJECT_NOT_CLOSED(dbobj->db, DBError, DB) |
| 334 | |
| 335 | #define CHECK_ENV_NOT_CLOSED(env) \ |
| 336 | _CHECK_OBJECT_NOT_CLOSED(env->db_env, DBError, DBEnv) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 337 | |
| 338 | #define CHECK_CURSOR_NOT_CLOSED(curs) \ |
Gregory P. Smith | e276717 | 2003-11-02 08:06:29 +0000 | [diff] [blame] | 339 | _CHECK_OBJECT_NOT_CLOSED(curs->dbc, DBCursorClosedError, DBCursor) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 340 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 341 | #if (DBVER >= 43) |
| 342 | #define CHECK_SEQUENCE_NOT_CLOSED(curs) \ |
| 343 | _CHECK_OBJECT_NOT_CLOSED(curs->sequence, DBError, DBSequence) |
| 344 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 345 | |
| 346 | #define CHECK_DBFLAG(mydb, flag) (((mydb)->flags & (flag)) || \ |
| 347 | (((mydb)->myenvobj != NULL) && ((mydb)->myenvobj->flags & (flag)))) |
| 348 | |
| 349 | #define CLEAR_DBT(dbt) (memset(&(dbt), 0, sizeof(dbt))) |
| 350 | |
| 351 | #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] | 352 | dbt.data != NULL) { free(dbt.data); dbt.data = NULL; } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 353 | |
| 354 | |
| 355 | static int makeDBError(int err); |
| 356 | |
| 357 | |
| 358 | /* Return the access method type of the DBObject */ |
| 359 | static int _DB_get_type(DBObject* self) |
| 360 | { |
| 361 | #if (DBVER >= 33) |
| 362 | DBTYPE type; |
| 363 | int err; |
| 364 | err = self->db->get_type(self->db, &type); |
| 365 | if (makeDBError(err)) { |
| 366 | return -1; |
| 367 | } |
| 368 | return type; |
| 369 | #else |
| 370 | return self->db->get_type(self->db); |
| 371 | #endif |
| 372 | } |
| 373 | |
| 374 | |
| 375 | /* Create a DBT structure (containing key and data values) from Python |
| 376 | strings. Returns 1 on success, 0 on an error. */ |
| 377 | static int make_dbt(PyObject* obj, DBT* dbt) |
| 378 | { |
| 379 | CLEAR_DBT(*dbt); |
| 380 | if (obj == Py_None) { |
| 381 | /* no need to do anything, the structure has already been zeroed */ |
| 382 | } |
| 383 | else if (!PyArg_Parse(obj, "s#", &dbt->data, &dbt->size)) { |
| 384 | PyErr_SetString(PyExc_TypeError, |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 385 | "Data values must be of type string or None."); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 386 | return 0; |
| 387 | } |
| 388 | return 1; |
| 389 | } |
| 390 | |
| 391 | |
| 392 | /* Recno and Queue DBs can have integer keys. This function figures out |
| 393 | what's been given, verifies that it's allowed, and then makes the DBT. |
| 394 | |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 395 | Caller MUST call FREE_DBT(key) when done. */ |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 396 | static int |
| 397 | 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] | 398 | { |
| 399 | db_recno_t recno; |
| 400 | int type; |
| 401 | |
| 402 | CLEAR_DBT(*key); |
Gustavo Niemeyer | f073b75 | 2004-01-20 15:24:29 +0000 | [diff] [blame] | 403 | if (keyobj == Py_None) { |
Gustavo Niemeyer | 024f2de | 2004-01-20 15:14:55 +0000 | [diff] [blame] | 404 | type = _DB_get_type(self); |
Gustavo Niemeyer | 8974f72 | 2004-01-20 15:20:03 +0000 | [diff] [blame] | 405 | if (type == -1) |
| 406 | return 0; |
Gustavo Niemeyer | 024f2de | 2004-01-20 15:14:55 +0000 | [diff] [blame] | 407 | if (type == DB_RECNO || type == DB_QUEUE) { |
| 408 | PyErr_SetString( |
| 409 | PyExc_TypeError, |
| 410 | "None keys not allowed for Recno and Queue DB's"); |
| 411 | return 0; |
| 412 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 413 | /* no need to do anything, the structure has already been zeroed */ |
| 414 | } |
| 415 | |
| 416 | else if (PyString_Check(keyobj)) { |
| 417 | /* verify access method type */ |
| 418 | type = _DB_get_type(self); |
| 419 | if (type == -1) |
| 420 | return 0; |
| 421 | if (type == DB_RECNO || type == DB_QUEUE) { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 422 | PyErr_SetString( |
| 423 | PyExc_TypeError, |
| 424 | "String keys not allowed for Recno and Queue DB's"); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | key->data = PyString_AS_STRING(keyobj); |
| 429 | key->size = PyString_GET_SIZE(keyobj); |
| 430 | } |
| 431 | |
| 432 | else if (PyInt_Check(keyobj)) { |
| 433 | /* verify access method type */ |
| 434 | type = _DB_get_type(self); |
| 435 | if (type == -1) |
| 436 | return 0; |
| 437 | if (type == DB_BTREE && pflags != NULL) { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 438 | /* if BTREE then an Integer key is allowed with the |
| 439 | * DB_SET_RECNO flag */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 440 | *pflags |= DB_SET_RECNO; |
| 441 | } |
| 442 | else if (type != DB_RECNO && type != DB_QUEUE) { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 443 | PyErr_SetString( |
| 444 | PyExc_TypeError, |
| 445 | "Integer keys only allowed for Recno and Queue DB's"); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 446 | return 0; |
| 447 | } |
| 448 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 449 | /* Make a key out of the requested recno, use allocated space so DB |
| 450 | * will be able to realloc room for the real key if needed. */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 451 | recno = PyInt_AS_LONG(keyobj); |
| 452 | key->data = malloc(sizeof(db_recno_t)); |
| 453 | if (key->data == NULL) { |
| 454 | PyErr_SetString(PyExc_MemoryError, "Key memory allocation failed"); |
| 455 | return 0; |
| 456 | } |
| 457 | key->ulen = key->size = sizeof(db_recno_t); |
| 458 | memcpy(key->data, &recno, sizeof(db_recno_t)); |
| 459 | key->flags = DB_DBT_REALLOC; |
| 460 | } |
| 461 | else { |
| 462 | PyErr_Format(PyExc_TypeError, |
| 463 | "String or Integer object expected for key, %s found", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame^] | 464 | Py_Type(keyobj)->tp_name); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | return 1; |
| 469 | } |
| 470 | |
| 471 | |
| 472 | /* Add partial record access to an existing DBT data struct. |
| 473 | If dlen and doff are set, then the DB_DBT_PARTIAL flag will be set |
| 474 | and the data storage/retrieval will be done using dlen and doff. */ |
| 475 | static int add_partial_dbt(DBT* d, int dlen, int doff) { |
| 476 | /* if neither were set we do nothing (-1 is the default value) */ |
| 477 | if ((dlen == -1) && (doff == -1)) { |
| 478 | return 1; |
| 479 | } |
| 480 | |
| 481 | if ((dlen < 0) || (doff < 0)) { |
| 482 | PyErr_SetString(PyExc_TypeError, "dlen and doff must both be >= 0"); |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | d->flags = d->flags | DB_DBT_PARTIAL; |
| 487 | d->dlen = (unsigned int) dlen; |
| 488 | d->doff = (unsigned int) doff; |
| 489 | return 1; |
| 490 | } |
| 491 | |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 492 | /* a safe strcpy() without the zeroing behaviour and semantics of strncpy. */ |
| 493 | /* TODO: make this use the native libc strlcpy() when available (BSD) */ |
| 494 | unsigned int our_strlcpy(char* dest, const char* src, unsigned int n) |
| 495 | { |
| 496 | unsigned int srclen, copylen; |
| 497 | |
| 498 | srclen = strlen(src); |
| 499 | if (n <= 0) |
| 500 | return srclen; |
| 501 | copylen = (srclen > n-1) ? n-1 : srclen; |
| 502 | /* populate dest[0] thru dest[copylen-1] */ |
| 503 | memcpy(dest, src, copylen); |
| 504 | /* guarantee null termination */ |
| 505 | dest[copylen] = 0; |
| 506 | |
| 507 | return srclen; |
| 508 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 509 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 510 | /* Callback used to save away more information about errors from the DB |
| 511 | * library. */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 512 | static char _db_errmsg[1024]; |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 513 | #if (DBVER <= 42) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 514 | static void _db_errorCallback(const char* prefix, char* msg) |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 515 | #else |
| 516 | static void _db_errorCallback(const DB_ENV *db_env, |
| 517 | const char* prefix, const char* msg) |
| 518 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 519 | { |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 520 | our_strlcpy(_db_errmsg, msg, sizeof(_db_errmsg)); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | |
| 524 | /* make a nice exception object to raise for errors. */ |
| 525 | static int makeDBError(int err) |
| 526 | { |
| 527 | char errTxt[2048]; /* really big, just in case... */ |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 528 | PyObject *errObj = NULL; |
| 529 | PyObject *errTuple = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 530 | int exceptionRaised = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 531 | unsigned int bytes_left; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 532 | |
| 533 | switch (err) { |
| 534 | case 0: /* successful, no error */ break; |
| 535 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 536 | #if (DBVER < 41) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 537 | case DB_INCOMPLETE: |
| 538 | #if INCOMPLETE_IS_WARNING |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 539 | bytes_left = our_strlcpy(errTxt, db_strerror(err), sizeof(errTxt)); |
| 540 | /* Ensure that bytes_left never goes negative */ |
| 541 | if (_db_errmsg[0] && bytes_left < (sizeof(errTxt) - 4)) { |
| 542 | bytes_left = sizeof(errTxt) - bytes_left - 4 - 1; |
| 543 | assert(bytes_left >= 0); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 544 | strcat(errTxt, " -- "); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 545 | strncat(errTxt, _db_errmsg, bytes_left); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 546 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 547 | _db_errmsg[0] = 0; |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 548 | #ifdef HAVE_WARNINGS |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 549 | exceptionRaised = PyErr_Warn(PyExc_RuntimeWarning, errTxt); |
| 550 | #else |
| 551 | fprintf(stderr, errTxt); |
| 552 | fprintf(stderr, "\n"); |
| 553 | #endif |
| 554 | |
| 555 | #else /* do an exception instead */ |
| 556 | errObj = DBIncompleteError; |
| 557 | #endif |
| 558 | break; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 559 | #endif /* DBVER < 41 */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 560 | |
| 561 | case DB_KEYEMPTY: errObj = DBKeyEmptyError; break; |
| 562 | case DB_KEYEXIST: errObj = DBKeyExistError; break; |
| 563 | case DB_LOCK_DEADLOCK: errObj = DBLockDeadlockError; break; |
| 564 | case DB_LOCK_NOTGRANTED: errObj = DBLockNotGrantedError; break; |
| 565 | case DB_NOTFOUND: errObj = DBNotFoundError; break; |
| 566 | case DB_OLD_VERSION: errObj = DBOldVersionError; break; |
| 567 | case DB_RUNRECOVERY: errObj = DBRunRecoveryError; break; |
| 568 | case DB_VERIFY_BAD: errObj = DBVerifyBadError; break; |
| 569 | case DB_NOSERVER: errObj = DBNoServerError; break; |
| 570 | case DB_NOSERVER_HOME: errObj = DBNoServerHomeError; break; |
| 571 | case DB_NOSERVER_ID: errObj = DBNoServerIDError; break; |
| 572 | #if (DBVER >= 33) |
| 573 | case DB_PAGE_NOTFOUND: errObj = DBPageNotFoundError; break; |
| 574 | case DB_SECONDARY_BAD: errObj = DBSecondaryBadError; break; |
| 575 | #endif |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 576 | case DB_BUFFER_SMALL: errObj = DBNoMemoryError; break; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 577 | |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 578 | #if (DBVER >= 43) |
| 579 | /* ENOMEM and DB_BUFFER_SMALL were one and the same until 4.3 */ |
| 580 | case ENOMEM: errObj = PyExc_MemoryError; break; |
| 581 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 582 | case EINVAL: errObj = DBInvalidArgError; break; |
| 583 | case EACCES: errObj = DBAccessError; break; |
| 584 | case ENOSPC: errObj = DBNoSpaceError; break; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 585 | case EAGAIN: errObj = DBAgainError; break; |
| 586 | case EBUSY : errObj = DBBusyError; break; |
| 587 | case EEXIST: errObj = DBFileExistsError; break; |
| 588 | case ENOENT: errObj = DBNoSuchFileError; break; |
| 589 | case EPERM : errObj = DBPermissionsError; break; |
| 590 | |
| 591 | default: errObj = DBError; break; |
| 592 | } |
| 593 | |
| 594 | if (errObj != NULL) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 595 | bytes_left = our_strlcpy(errTxt, db_strerror(err), sizeof(errTxt)); |
| 596 | /* Ensure that bytes_left never goes negative */ |
| 597 | if (_db_errmsg[0] && bytes_left < (sizeof(errTxt) - 4)) { |
| 598 | bytes_left = sizeof(errTxt) - bytes_left - 4 - 1; |
| 599 | assert(bytes_left >= 0); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 600 | strcat(errTxt, " -- "); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 601 | strncat(errTxt, _db_errmsg, bytes_left); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 602 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 603 | _db_errmsg[0] = 0; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 604 | |
| 605 | errTuple = Py_BuildValue("(is)", err, errTxt); |
| 606 | PyErr_SetObject(errObj, errTuple); |
| 607 | Py_DECREF(errTuple); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | return ((errObj != NULL) || exceptionRaised); |
| 611 | } |
| 612 | |
| 613 | |
| 614 | |
| 615 | /* set a type exception */ |
| 616 | static void makeTypeError(char* expected, PyObject* found) |
| 617 | { |
| 618 | PyErr_Format(PyExc_TypeError, "Expected %s argument, %s found.", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame^] | 619 | expected, Py_Type(found)->tp_name); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | |
| 623 | /* verify that an obj is either None or a DBTxn, and set the txn pointer */ |
| 624 | static int checkTxnObj(PyObject* txnobj, DB_TXN** txn) |
| 625 | { |
| 626 | if (txnobj == Py_None || txnobj == NULL) { |
| 627 | *txn = NULL; |
| 628 | return 1; |
| 629 | } |
| 630 | if (DBTxnObject_Check(txnobj)) { |
| 631 | *txn = ((DBTxnObject*)txnobj)->txn; |
| 632 | return 1; |
| 633 | } |
| 634 | else |
| 635 | makeTypeError("DBTxn", txnobj); |
| 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | |
| 640 | /* Delete a key from a database |
| 641 | Returns 0 on success, -1 on an error. */ |
| 642 | static int _DB_delete(DBObject* self, DB_TXN *txn, DBT *key, int flags) |
| 643 | { |
| 644 | int err; |
| 645 | |
| 646 | MYDB_BEGIN_ALLOW_THREADS; |
| 647 | err = self->db->del(self->db, txn, key, 0); |
| 648 | MYDB_END_ALLOW_THREADS; |
| 649 | if (makeDBError(err)) { |
| 650 | return -1; |
| 651 | } |
| 652 | self->haveStat = 0; |
| 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | |
| 657 | /* Store a key into a database |
| 658 | Returns 0 on success, -1 on an error. */ |
| 659 | static int _DB_put(DBObject* self, DB_TXN *txn, DBT *key, DBT *data, int flags) |
| 660 | { |
| 661 | int err; |
| 662 | |
| 663 | MYDB_BEGIN_ALLOW_THREADS; |
| 664 | err = self->db->put(self->db, txn, key, data, flags); |
| 665 | MYDB_END_ALLOW_THREADS; |
| 666 | if (makeDBError(err)) { |
| 667 | return -1; |
| 668 | } |
| 669 | self->haveStat = 0; |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | /* Get a key/data pair from a cursor */ |
| 674 | static PyObject* _DBCursor_get(DBCursorObject* self, int extra_flags, |
| 675 | PyObject *args, PyObject *kwargs, char *format) |
| 676 | { |
| 677 | int err; |
| 678 | PyObject* retval = NULL; |
| 679 | DBT key, data; |
| 680 | int dlen = -1; |
| 681 | int doff = -1; |
| 682 | int flags = 0; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 683 | static char* kwnames[] = { "flags", "dlen", "doff", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 684 | |
| 685 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, format, kwnames, |
| 686 | &flags, &dlen, &doff)) |
| 687 | return NULL; |
| 688 | |
| 689 | CHECK_CURSOR_NOT_CLOSED(self); |
| 690 | |
| 691 | flags |= extra_flags; |
| 692 | CLEAR_DBT(key); |
| 693 | CLEAR_DBT(data); |
| 694 | if (CHECK_DBFLAG(self->mydb, DB_THREAD)) { |
| 695 | /* Tell BerkeleyDB to malloc the return value (thread safe) */ |
| 696 | data.flags = DB_DBT_MALLOC; |
| 697 | key.flags = DB_DBT_MALLOC; |
| 698 | } |
| 699 | if (!add_partial_dbt(&data, dlen, doff)) |
| 700 | return NULL; |
| 701 | |
| 702 | MYDB_BEGIN_ALLOW_THREADS; |
| 703 | err = self->dbc->c_get(self->dbc, &key, &data, flags); |
| 704 | MYDB_END_ALLOW_THREADS; |
| 705 | |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 706 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 707 | && self->mydb->moduleFlags.getReturnsNone) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 708 | Py_INCREF(Py_None); |
| 709 | retval = Py_None; |
| 710 | } |
| 711 | else if (makeDBError(err)) { |
| 712 | retval = NULL; |
| 713 | } |
| 714 | else { /* otherwise, success! */ |
| 715 | |
| 716 | /* if Recno or Queue, return the key as an Int */ |
| 717 | switch (_DB_get_type(self->mydb)) { |
| 718 | case -1: |
| 719 | retval = NULL; |
| 720 | break; |
| 721 | |
| 722 | case DB_RECNO: |
| 723 | case DB_QUEUE: |
| 724 | retval = Py_BuildValue("is#", *((db_recno_t*)key.data), |
| 725 | data.data, data.size); |
| 726 | break; |
| 727 | case DB_HASH: |
| 728 | case DB_BTREE: |
| 729 | default: |
| 730 | retval = Py_BuildValue("s#s#", key.data, key.size, |
| 731 | data.data, data.size); |
| 732 | break; |
| 733 | } |
| 734 | } |
| 735 | if (!err) { |
| 736 | FREE_DBT(key); |
| 737 | FREE_DBT(data); |
| 738 | } |
| 739 | return retval; |
| 740 | } |
| 741 | |
| 742 | |
| 743 | /* add an integer to a dictionary using the given name as a key */ |
| 744 | static void _addIntToDict(PyObject* dict, char *name, int value) |
| 745 | { |
| 746 | PyObject* v = PyInt_FromLong((long) value); |
| 747 | if (!v || PyDict_SetItemString(dict, name, v)) |
| 748 | PyErr_Clear(); |
| 749 | |
| 750 | Py_XDECREF(v); |
| 751 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 752 | |
| 753 | /* The same, when the value is a time_t */ |
| 754 | static void _addTimeTToDict(PyObject* dict, char *name, time_t value) |
| 755 | { |
| 756 | PyObject* v; |
| 757 | /* if the value fits in regular int, use that. */ |
| 758 | #ifdef HAVE_LONG_LONG |
| 759 | if (sizeof(time_t) > sizeof(long)) |
| 760 | v = PyLong_FromLongLong((PY_LONG_LONG) value); |
| 761 | else |
| 762 | #endif |
| 763 | v = PyInt_FromLong((long) value); |
| 764 | if (!v || PyDict_SetItemString(dict, name, v)) |
| 765 | PyErr_Clear(); |
| 766 | |
| 767 | Py_XDECREF(v); |
| 768 | } |
| 769 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 770 | #if (DBVER >= 43) |
| 771 | /* add an db_seq_t to a dictionary using the given name as a key */ |
| 772 | static void _addDb_seq_tToDict(PyObject* dict, char *name, db_seq_t value) |
| 773 | { |
| 774 | PyObject* v = PyLong_FromLongLong(value); |
| 775 | if (!v || PyDict_SetItemString(dict, name, v)) |
| 776 | PyErr_Clear(); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 777 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 778 | Py_XDECREF(v); |
| 779 | } |
| 780 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 781 | |
| 782 | |
| 783 | |
| 784 | /* --------------------------------------------------------------------- */ |
| 785 | /* Allocators and deallocators */ |
| 786 | |
| 787 | static DBObject* |
| 788 | newDBObject(DBEnvObject* arg, int flags) |
| 789 | { |
| 790 | DBObject* self; |
| 791 | DB_ENV* db_env = NULL; |
| 792 | int err; |
| 793 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 794 | self = PyObject_New(DBObject, &DB_Type); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 795 | if (self == NULL) |
| 796 | return NULL; |
| 797 | |
| 798 | self->haveStat = 0; |
| 799 | self->flags = 0; |
| 800 | self->setflags = 0; |
| 801 | self->myenvobj = NULL; |
| 802 | #if (DBVER >= 33) |
| 803 | self->associateCallback = NULL; |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 804 | self->btCompareCallback = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 805 | self->primaryDBType = 0; |
| 806 | #endif |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 807 | #ifdef HAVE_WEAKREF |
| 808 | self->in_weakreflist = NULL; |
| 809 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 810 | |
| 811 | /* keep a reference to our python DBEnv object */ |
| 812 | if (arg) { |
| 813 | Py_INCREF(arg); |
| 814 | self->myenvobj = arg; |
| 815 | db_env = arg->db_env; |
| 816 | } |
| 817 | |
| 818 | if (self->myenvobj) |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 819 | self->moduleFlags = self->myenvobj->moduleFlags; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 820 | else |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 821 | self->moduleFlags.getReturnsNone = DEFAULT_GET_RETURNS_NONE; |
| 822 | self->moduleFlags.cursorSetReturnsNone = DEFAULT_CURSOR_SET_RETURNS_NONE; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 823 | |
| 824 | MYDB_BEGIN_ALLOW_THREADS; |
| 825 | err = db_create(&self->db, db_env, flags); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 826 | if (self->db != NULL) { |
| 827 | self->db->set_errcall(self->db, _db_errorCallback); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 828 | #if (DBVER >= 33) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 829 | self->db->app_private = (void*)self; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 830 | #endif |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 831 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 832 | MYDB_END_ALLOW_THREADS; |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 833 | /* TODO add a weakref(self) to the self->myenvobj->open_child_weakrefs |
| 834 | * list so that a DBEnv can refuse to close without aborting any open |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 835 | * DBTxns and closing any open DBs first. */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 836 | if (makeDBError(err)) { |
| 837 | if (self->myenvobj) { |
| 838 | Py_DECREF(self->myenvobj); |
| 839 | self->myenvobj = NULL; |
| 840 | } |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 841 | PyObject_Del(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 842 | self = NULL; |
| 843 | } |
| 844 | return self; |
| 845 | } |
| 846 | |
| 847 | |
| 848 | static void |
| 849 | DB_dealloc(DBObject* self) |
| 850 | { |
| 851 | if (self->db != NULL) { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 852 | /* avoid closing a DB when its DBEnv has been closed out from under |
| 853 | * it */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 854 | if (!self->myenvobj || |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 855 | (self->myenvobj && self->myenvobj->db_env)) |
| 856 | { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 857 | MYDB_BEGIN_ALLOW_THREADS; |
| 858 | self->db->close(self->db, 0); |
| 859 | MYDB_END_ALLOW_THREADS; |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 860 | #ifdef HAVE_WARNINGS |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 861 | } else { |
| 862 | PyErr_Warn(PyExc_RuntimeWarning, |
| 863 | "DB could not be closed in destructor: DBEnv already closed"); |
| 864 | #endif |
| 865 | } |
| 866 | self->db = NULL; |
| 867 | } |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 868 | #ifdef HAVE_WEAKREF |
| 869 | if (self->in_weakreflist != NULL) { |
| 870 | PyObject_ClearWeakRefs((PyObject *) self); |
| 871 | } |
| 872 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 873 | if (self->myenvobj) { |
| 874 | Py_DECREF(self->myenvobj); |
| 875 | self->myenvobj = NULL; |
| 876 | } |
| 877 | #if (DBVER >= 33) |
| 878 | if (self->associateCallback != NULL) { |
| 879 | Py_DECREF(self->associateCallback); |
| 880 | self->associateCallback = NULL; |
| 881 | } |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 882 | if (self->btCompareCallback != NULL) { |
| 883 | Py_DECREF(self->btCompareCallback); |
| 884 | self->btCompareCallback = NULL; |
| 885 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 886 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 887 | PyObject_Del(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | |
| 891 | static DBCursorObject* |
| 892 | newDBCursorObject(DBC* dbc, DBObject* db) |
| 893 | { |
Neal Norwitz | b4a5581 | 2004-07-09 23:30:57 +0000 | [diff] [blame] | 894 | DBCursorObject* self = PyObject_New(DBCursorObject, &DBCursor_Type); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 895 | if (self == NULL) |
| 896 | return NULL; |
| 897 | |
| 898 | self->dbc = dbc; |
| 899 | self->mydb = db; |
Gregory P. Smith | a703a21 | 2003-11-03 01:04:41 +0000 | [diff] [blame] | 900 | #ifdef HAVE_WEAKREF |
| 901 | self->in_weakreflist = NULL; |
| 902 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 903 | Py_INCREF(self->mydb); |
| 904 | return self; |
| 905 | } |
| 906 | |
| 907 | |
| 908 | static void |
| 909 | DBCursor_dealloc(DBCursorObject* self) |
| 910 | { |
| 911 | int err; |
Gregory P. Smith | a703a21 | 2003-11-03 01:04:41 +0000 | [diff] [blame] | 912 | |
| 913 | #ifdef HAVE_WEAKREF |
| 914 | if (self->in_weakreflist != NULL) { |
| 915 | PyObject_ClearWeakRefs((PyObject *) self); |
| 916 | } |
| 917 | #endif |
| 918 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 919 | if (self->dbc != NULL) { |
| 920 | MYDB_BEGIN_ALLOW_THREADS; |
Martin v. Löwis | 35c38ea | 2003-07-15 19:12:54 +0000 | [diff] [blame] | 921 | /* If the underlying database has been closed, we don't |
| 922 | need to do anything. If the environment has been closed |
| 923 | we need to leak, as BerkeleyDB will crash trying to access |
| 924 | the environment. There was an exception when the |
| 925 | user closed the environment even though there still was |
| 926 | a database open. */ |
| 927 | if (self->mydb->db && self->mydb->myenvobj && |
| 928 | !self->mydb->myenvobj->closed) |
Gregory P. Smith | b6c9f78 | 2003-01-17 08:42:50 +0000 | [diff] [blame] | 929 | err = self->dbc->c_close(self->dbc); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 930 | self->dbc = NULL; |
| 931 | MYDB_END_ALLOW_THREADS; |
| 932 | } |
| 933 | Py_XDECREF( self->mydb ); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 934 | PyObject_Del(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | |
| 938 | static DBEnvObject* |
| 939 | newDBEnvObject(int flags) |
| 940 | { |
| 941 | int err; |
Neal Norwitz | b4a5581 | 2004-07-09 23:30:57 +0000 | [diff] [blame] | 942 | DBEnvObject* self = PyObject_New(DBEnvObject, &DBEnv_Type); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 943 | if (self == NULL) |
| 944 | return NULL; |
| 945 | |
| 946 | self->closed = 1; |
| 947 | self->flags = flags; |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 948 | self->moduleFlags.getReturnsNone = DEFAULT_GET_RETURNS_NONE; |
| 949 | self->moduleFlags.cursorSetReturnsNone = DEFAULT_CURSOR_SET_RETURNS_NONE; |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 950 | #ifdef HAVE_WEAKREF |
| 951 | self->in_weakreflist = NULL; |
| 952 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 953 | |
| 954 | MYDB_BEGIN_ALLOW_THREADS; |
| 955 | err = db_env_create(&self->db_env, flags); |
| 956 | MYDB_END_ALLOW_THREADS; |
| 957 | if (makeDBError(err)) { |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 958 | PyObject_Del(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 959 | self = NULL; |
| 960 | } |
| 961 | else { |
| 962 | self->db_env->set_errcall(self->db_env, _db_errorCallback); |
| 963 | } |
| 964 | return self; |
| 965 | } |
| 966 | |
| 967 | |
| 968 | static void |
| 969 | DBEnv_dealloc(DBEnvObject* self) |
| 970 | { |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 971 | #ifdef HAVE_WEAKREF |
| 972 | if (self->in_weakreflist != NULL) { |
| 973 | PyObject_ClearWeakRefs((PyObject *) self); |
| 974 | } |
| 975 | #endif |
| 976 | |
Gregory P. Smith | 4e414d8 | 2006-01-24 19:55:02 +0000 | [diff] [blame] | 977 | if (self->db_env && !self->closed) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 978 | MYDB_BEGIN_ALLOW_THREADS; |
| 979 | self->db_env->close(self->db_env, 0); |
| 980 | MYDB_END_ALLOW_THREADS; |
| 981 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 982 | PyObject_Del(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | |
| 986 | static DBTxnObject* |
| 987 | newDBTxnObject(DBEnvObject* myenv, DB_TXN *parent, int flags) |
| 988 | { |
| 989 | int err; |
Neal Norwitz | b4a5581 | 2004-07-09 23:30:57 +0000 | [diff] [blame] | 990 | DBTxnObject* self = PyObject_New(DBTxnObject, &DBTxn_Type); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 991 | if (self == NULL) |
| 992 | return NULL; |
Neal Norwitz | 62a2112 | 2006-01-25 05:21:55 +0000 | [diff] [blame] | 993 | Py_INCREF(myenv); |
| 994 | self->env = (PyObject*)myenv; |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 995 | #ifdef HAVE_WEAKREF |
| 996 | self->in_weakreflist = NULL; |
| 997 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 998 | |
| 999 | MYDB_BEGIN_ALLOW_THREADS; |
| 1000 | #if (DBVER >= 40) |
| 1001 | err = myenv->db_env->txn_begin(myenv->db_env, parent, &(self->txn), flags); |
| 1002 | #else |
| 1003 | err = txn_begin(myenv->db_env, parent, &(self->txn), flags); |
| 1004 | #endif |
| 1005 | MYDB_END_ALLOW_THREADS; |
| 1006 | if (makeDBError(err)) { |
Neal Norwitz | 62a2112 | 2006-01-25 05:21:55 +0000 | [diff] [blame] | 1007 | Py_DECREF(self->env); |
| 1008 | PyObject_Del(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1009 | self = NULL; |
| 1010 | } |
| 1011 | return self; |
| 1012 | } |
| 1013 | |
| 1014 | |
| 1015 | static void |
| 1016 | DBTxn_dealloc(DBTxnObject* self) |
| 1017 | { |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 1018 | #ifdef HAVE_WEAKREF |
| 1019 | if (self->in_weakreflist != NULL) { |
| 1020 | PyObject_ClearWeakRefs((PyObject *) self); |
| 1021 | } |
| 1022 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1023 | |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 1024 | #ifdef HAVE_WARNINGS |
| 1025 | if (self->txn) { |
| 1026 | /* it hasn't been finalized, abort it! */ |
| 1027 | MYDB_BEGIN_ALLOW_THREADS; |
| 1028 | #if (DBVER >= 40) |
| 1029 | self->txn->abort(self->txn); |
| 1030 | #else |
| 1031 | txn_abort(self->txn); |
| 1032 | #endif |
| 1033 | MYDB_END_ALLOW_THREADS; |
| 1034 | PyErr_Warn(PyExc_RuntimeWarning, |
| 1035 | "DBTxn aborted in destructor. No prior commit() or abort()."); |
| 1036 | } |
| 1037 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1038 | |
Neal Norwitz | 62a2112 | 2006-01-25 05:21:55 +0000 | [diff] [blame] | 1039 | Py_DECREF(self->env); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1040 | PyObject_Del(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | |
| 1044 | static DBLockObject* |
| 1045 | newDBLockObject(DBEnvObject* myenv, u_int32_t locker, DBT* obj, |
| 1046 | db_lockmode_t lock_mode, int flags) |
| 1047 | { |
| 1048 | int err; |
Neal Norwitz | b4a5581 | 2004-07-09 23:30:57 +0000 | [diff] [blame] | 1049 | DBLockObject* self = PyObject_New(DBLockObject, &DBLock_Type); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1050 | if (self == NULL) |
| 1051 | return NULL; |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 1052 | #ifdef HAVE_WEAKREF |
| 1053 | self->in_weakreflist = NULL; |
| 1054 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1055 | |
| 1056 | MYDB_BEGIN_ALLOW_THREADS; |
| 1057 | #if (DBVER >= 40) |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1058 | err = myenv->db_env->lock_get(myenv->db_env, locker, flags, obj, lock_mode, |
| 1059 | &self->lock); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1060 | #else |
| 1061 | err = lock_get(myenv->db_env, locker, flags, obj, lock_mode, &self->lock); |
| 1062 | #endif |
| 1063 | MYDB_END_ALLOW_THREADS; |
| 1064 | if (makeDBError(err)) { |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 1065 | PyObject_Del(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1066 | self = NULL; |
| 1067 | } |
| 1068 | |
| 1069 | return self; |
| 1070 | } |
| 1071 | |
| 1072 | |
| 1073 | static void |
| 1074 | DBLock_dealloc(DBLockObject* self) |
| 1075 | { |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 1076 | #ifdef HAVE_WEAKREF |
| 1077 | if (self->in_weakreflist != NULL) { |
| 1078 | PyObject_ClearWeakRefs((PyObject *) self); |
| 1079 | } |
| 1080 | #endif |
| 1081 | /* TODO: is this lock held? should we release it? */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1082 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1083 | PyObject_Del(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1087 | #if (DBVER >= 43) |
| 1088 | static DBSequenceObject* |
| 1089 | newDBSequenceObject(DBObject* mydb, int flags) |
| 1090 | { |
| 1091 | int err; |
| 1092 | DBSequenceObject* self = PyObject_New(DBSequenceObject, &DBSequence_Type); |
| 1093 | if (self == NULL) |
| 1094 | return NULL; |
| 1095 | Py_INCREF(mydb); |
| 1096 | self->mydb = mydb; |
| 1097 | #ifdef HAVE_WEAKREF |
| 1098 | self->in_weakreflist = NULL; |
| 1099 | #endif |
| 1100 | |
| 1101 | |
| 1102 | MYDB_BEGIN_ALLOW_THREADS; |
| 1103 | err = db_sequence_create(&self->sequence, self->mydb->db, flags); |
| 1104 | MYDB_END_ALLOW_THREADS; |
| 1105 | if (makeDBError(err)) { |
| 1106 | Py_DECREF(self->mydb); |
| 1107 | PyObject_Del(self); |
| 1108 | self = NULL; |
| 1109 | } |
| 1110 | |
| 1111 | return self; |
| 1112 | } |
| 1113 | |
| 1114 | |
| 1115 | static void |
| 1116 | DBSequence_dealloc(DBSequenceObject* self) |
| 1117 | { |
| 1118 | #ifdef HAVE_WEAKREF |
| 1119 | if (self->in_weakreflist != NULL) { |
| 1120 | PyObject_ClearWeakRefs((PyObject *) self); |
| 1121 | } |
| 1122 | #endif |
| 1123 | |
| 1124 | Py_DECREF(self->mydb); |
| 1125 | PyObject_Del(self); |
| 1126 | } |
| 1127 | #endif |
| 1128 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1129 | /* --------------------------------------------------------------------- */ |
| 1130 | /* DB methods */ |
| 1131 | |
| 1132 | static PyObject* |
| 1133 | DB_append(DBObject* self, PyObject* args) |
| 1134 | { |
| 1135 | PyObject* txnobj = NULL; |
| 1136 | PyObject* dataobj; |
| 1137 | db_recno_t recno; |
| 1138 | DBT key, data; |
| 1139 | DB_TXN *txn = NULL; |
| 1140 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1141 | if (!PyArg_UnpackTuple(args, "append", 1, 2, &dataobj, &txnobj)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1142 | return NULL; |
| 1143 | |
| 1144 | CHECK_DB_NOT_CLOSED(self); |
| 1145 | |
| 1146 | /* make a dummy key out of a recno */ |
| 1147 | recno = 0; |
| 1148 | CLEAR_DBT(key); |
| 1149 | key.data = &recno; |
| 1150 | key.size = sizeof(recno); |
| 1151 | key.ulen = key.size; |
| 1152 | key.flags = DB_DBT_USERMEM; |
| 1153 | |
| 1154 | if (!make_dbt(dataobj, &data)) return NULL; |
| 1155 | if (!checkTxnObj(txnobj, &txn)) return NULL; |
| 1156 | |
| 1157 | if (-1 == _DB_put(self, txn, &key, &data, DB_APPEND)) |
| 1158 | return NULL; |
| 1159 | |
| 1160 | return PyInt_FromLong(recno); |
| 1161 | } |
| 1162 | |
| 1163 | |
| 1164 | #if (DBVER >= 33) |
| 1165 | |
| 1166 | static int |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1167 | _db_associateCallback(DB* db, const DBT* priKey, const DBT* priData, |
| 1168 | DBT* secKey) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1169 | { |
| 1170 | int retval = DB_DONOTINDEX; |
| 1171 | DBObject* secondaryDB = (DBObject*)db->app_private; |
| 1172 | PyObject* callback = secondaryDB->associateCallback; |
| 1173 | int type = secondaryDB->primaryDBType; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1174 | PyObject* args; |
Thomas Wouters | 89ba381 | 2006-03-07 14:14:51 +0000 | [diff] [blame] | 1175 | PyObject* result = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1176 | |
| 1177 | |
| 1178 | if (callback != NULL) { |
| 1179 | MYDB_BEGIN_BLOCK_THREADS; |
| 1180 | |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 1181 | if (type == DB_RECNO || type == DB_QUEUE) |
| 1182 | args = Py_BuildValue("(ls#)", *((db_recno_t*)priKey->data), |
| 1183 | priData->data, priData->size); |
| 1184 | else |
| 1185 | args = Py_BuildValue("(s#s#)", priKey->data, priKey->size, |
| 1186 | priData->data, priData->size); |
Thomas Wouters | 098f694 | 2006-03-07 14:13:17 +0000 | [diff] [blame] | 1187 | if (args != NULL) { |
Thomas Wouters | 098f694 | 2006-03-07 14:13:17 +0000 | [diff] [blame] | 1188 | result = PyEval_CallObject(callback, args); |
| 1189 | } |
| 1190 | if (args == NULL || result == NULL) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1191 | PyErr_Print(); |
| 1192 | } |
| 1193 | else if (result == Py_None) { |
| 1194 | retval = DB_DONOTINDEX; |
| 1195 | } |
| 1196 | else if (PyInt_Check(result)) { |
| 1197 | retval = PyInt_AsLong(result); |
| 1198 | } |
| 1199 | else if (PyString_Check(result)) { |
| 1200 | char* data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1201 | Py_ssize_t size; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1202 | |
| 1203 | CLEAR_DBT(*secKey); |
| 1204 | #if PYTHON_API_VERSION <= 1007 |
| 1205 | /* 1.5 compatibility */ |
| 1206 | size = PyString_Size(result); |
| 1207 | data = PyString_AsString(result); |
| 1208 | #else |
| 1209 | PyString_AsStringAndSize(result, &data, &size); |
| 1210 | #endif |
| 1211 | secKey->flags = DB_DBT_APPMALLOC; /* DB will free */ |
| 1212 | secKey->data = malloc(size); /* TODO, check this */ |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1213 | if (secKey->data) { |
| 1214 | memcpy(secKey->data, data, size); |
| 1215 | secKey->size = size; |
| 1216 | retval = 0; |
| 1217 | } |
| 1218 | else { |
| 1219 | PyErr_SetString(PyExc_MemoryError, |
| 1220 | "malloc failed in _db_associateCallback"); |
| 1221 | PyErr_Print(); |
| 1222 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1223 | } |
| 1224 | else { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1225 | PyErr_SetString( |
| 1226 | PyExc_TypeError, |
| 1227 | "DB associate callback should return DB_DONOTINDEX or string."); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1228 | PyErr_Print(); |
| 1229 | } |
| 1230 | |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 1231 | Py_XDECREF(args); |
| 1232 | Py_XDECREF(result); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1233 | |
| 1234 | MYDB_END_BLOCK_THREADS; |
| 1235 | } |
| 1236 | return retval; |
| 1237 | } |
| 1238 | |
| 1239 | |
| 1240 | static PyObject* |
| 1241 | DB_associate(DBObject* self, PyObject* args, PyObject* kwargs) |
| 1242 | { |
| 1243 | int err, flags=0; |
| 1244 | DBObject* secondaryDB; |
| 1245 | PyObject* callback; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1246 | #if (DBVER >= 41) |
| 1247 | PyObject *txnobj = NULL; |
| 1248 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1249 | static char* kwnames[] = {"secondaryDB", "callback", "flags", "txn", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 1250 | NULL}; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1251 | #else |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1252 | static char* kwnames[] = {"secondaryDB", "callback", "flags", NULL}; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1253 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1254 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1255 | #if (DBVER >= 41) |
| 1256 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|iO:associate", kwnames, |
| 1257 | &secondaryDB, &callback, &flags, |
| 1258 | &txnobj)) { |
| 1259 | #else |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1260 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:associate", kwnames, |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1261 | &secondaryDB, &callback, &flags)) { |
| 1262 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1263 | return NULL; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | #if (DBVER >= 41) |
| 1267 | if (!checkTxnObj(txnobj, &txn)) return NULL; |
| 1268 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1269 | |
| 1270 | CHECK_DB_NOT_CLOSED(self); |
| 1271 | if (!DBObject_Check(secondaryDB)) { |
| 1272 | makeTypeError("DB", (PyObject*)secondaryDB); |
| 1273 | return NULL; |
| 1274 | } |
Gregory P. Smith | 91116b6 | 2005-06-06 10:28:06 +0000 | [diff] [blame] | 1275 | CHECK_DB_NOT_CLOSED(secondaryDB); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1276 | if (callback == Py_None) { |
| 1277 | callback = NULL; |
| 1278 | } |
| 1279 | else if (!PyCallable_Check(callback)) { |
| 1280 | makeTypeError("Callable", callback); |
| 1281 | return NULL; |
| 1282 | } |
| 1283 | |
| 1284 | /* Save a reference to the callback in the secondary DB. */ |
Gregory P. Smith | 692ca9a | 2005-06-06 09:55:06 +0000 | [diff] [blame] | 1285 | Py_XDECREF(secondaryDB->associateCallback); |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 1286 | Py_XINCREF(callback); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1287 | secondaryDB->associateCallback = callback; |
| 1288 | secondaryDB->primaryDBType = _DB_get_type(self); |
| 1289 | |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 1290 | /* PyEval_InitThreads is called here due to a quirk in python 1.5 |
| 1291 | * - 2.2.1 (at least) according to Russell Williamson <merel@wt.net>: |
| 1292 | * The global interepreter lock is not initialized until the first |
| 1293 | * thread is created using thread.start_new_thread() or fork() is |
| 1294 | * called. that would cause the ALLOW_THREADS here to segfault due |
| 1295 | * to a null pointer reference if no threads or child processes |
| 1296 | * have been created. This works around that and is a no-op if |
| 1297 | * threads have already been initialized. |
| 1298 | * (see pybsddb-users mailing list post on 2002-08-07) |
| 1299 | */ |
Gregory P. Smith | aa71f5f | 2003-01-17 07:56:16 +0000 | [diff] [blame] | 1300 | #ifdef WITH_THREAD |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 1301 | PyEval_InitThreads(); |
Gregory P. Smith | aa71f5f | 2003-01-17 07:56:16 +0000 | [diff] [blame] | 1302 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1303 | MYDB_BEGIN_ALLOW_THREADS; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1304 | #if (DBVER >= 41) |
| 1305 | err = self->db->associate(self->db, |
| 1306 | txn, |
| 1307 | secondaryDB->db, |
| 1308 | _db_associateCallback, |
| 1309 | flags); |
| 1310 | #else |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1311 | err = self->db->associate(self->db, |
| 1312 | secondaryDB->db, |
| 1313 | _db_associateCallback, |
| 1314 | flags); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1315 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1316 | MYDB_END_ALLOW_THREADS; |
| 1317 | |
| 1318 | if (err) { |
Gregory P. Smith | 692ca9a | 2005-06-06 09:55:06 +0000 | [diff] [blame] | 1319 | Py_XDECREF(secondaryDB->associateCallback); |
| 1320 | secondaryDB->associateCallback = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1321 | secondaryDB->primaryDBType = 0; |
| 1322 | } |
| 1323 | |
| 1324 | RETURN_IF_ERR(); |
| 1325 | RETURN_NONE(); |
| 1326 | } |
| 1327 | |
| 1328 | |
| 1329 | #endif |
| 1330 | |
| 1331 | |
| 1332 | static PyObject* |
| 1333 | DB_close(DBObject* self, PyObject* args) |
| 1334 | { |
| 1335 | int err, flags=0; |
| 1336 | if (!PyArg_ParseTuple(args,"|i:close", &flags)) |
| 1337 | return NULL; |
| 1338 | if (self->db != NULL) { |
| 1339 | if (self->myenvobj) |
| 1340 | CHECK_ENV_NOT_CLOSED(self->myenvobj); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1341 | err = self->db->close(self->db, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1342 | self->db = NULL; |
| 1343 | RETURN_IF_ERR(); |
| 1344 | } |
| 1345 | RETURN_NONE(); |
| 1346 | } |
| 1347 | |
| 1348 | |
| 1349 | #if (DBVER >= 32) |
| 1350 | static PyObject* |
| 1351 | _DB_consume(DBObject* self, PyObject* args, PyObject* kwargs, int consume_flag) |
| 1352 | { |
| 1353 | int err, flags=0, type; |
| 1354 | PyObject* txnobj = NULL; |
| 1355 | PyObject* retval = NULL; |
| 1356 | DBT key, data; |
| 1357 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1358 | static char* kwnames[] = { "txn", "flags", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1359 | |
| 1360 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oi:consume", kwnames, |
| 1361 | &txnobj, &flags)) |
| 1362 | return NULL; |
| 1363 | |
| 1364 | CHECK_DB_NOT_CLOSED(self); |
| 1365 | type = _DB_get_type(self); |
| 1366 | if (type == -1) |
| 1367 | return NULL; |
| 1368 | if (type != DB_QUEUE) { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1369 | PyErr_SetString(PyExc_TypeError, |
| 1370 | "Consume methods only allowed for Queue DB's"); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1371 | return NULL; |
| 1372 | } |
| 1373 | if (!checkTxnObj(txnobj, &txn)) |
| 1374 | return NULL; |
| 1375 | |
| 1376 | CLEAR_DBT(key); |
| 1377 | CLEAR_DBT(data); |
| 1378 | if (CHECK_DBFLAG(self, DB_THREAD)) { |
| 1379 | /* Tell BerkeleyDB to malloc the return value (thread safe) */ |
| 1380 | data.flags = DB_DBT_MALLOC; |
| 1381 | key.flags = DB_DBT_MALLOC; |
| 1382 | } |
| 1383 | |
| 1384 | MYDB_BEGIN_ALLOW_THREADS; |
| 1385 | err = self->db->get(self->db, txn, &key, &data, flags|consume_flag); |
| 1386 | MYDB_END_ALLOW_THREADS; |
| 1387 | |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 1388 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 1389 | && self->moduleFlags.getReturnsNone) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1390 | err = 0; |
| 1391 | Py_INCREF(Py_None); |
| 1392 | retval = Py_None; |
| 1393 | } |
| 1394 | else if (!err) { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1395 | retval = Py_BuildValue("s#s#", key.data, key.size, data.data, |
| 1396 | data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1397 | FREE_DBT(key); |
| 1398 | FREE_DBT(data); |
| 1399 | } |
| 1400 | |
| 1401 | RETURN_IF_ERR(); |
| 1402 | return retval; |
| 1403 | } |
| 1404 | |
| 1405 | static PyObject* |
| 1406 | DB_consume(DBObject* self, PyObject* args, PyObject* kwargs, int consume_flag) |
| 1407 | { |
| 1408 | return _DB_consume(self, args, kwargs, DB_CONSUME); |
| 1409 | } |
| 1410 | |
| 1411 | static PyObject* |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1412 | DB_consume_wait(DBObject* self, PyObject* args, PyObject* kwargs, |
| 1413 | int consume_flag) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1414 | { |
| 1415 | return _DB_consume(self, args, kwargs, DB_CONSUME_WAIT); |
| 1416 | } |
| 1417 | #endif |
| 1418 | |
| 1419 | |
| 1420 | |
| 1421 | static PyObject* |
| 1422 | DB_cursor(DBObject* self, PyObject* args, PyObject* kwargs) |
| 1423 | { |
| 1424 | int err, flags=0; |
| 1425 | DBC* dbc; |
| 1426 | PyObject* txnobj = NULL; |
| 1427 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1428 | static char* kwnames[] = { "txn", "flags", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1429 | |
| 1430 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oi:cursor", kwnames, |
| 1431 | &txnobj, &flags)) |
| 1432 | return NULL; |
| 1433 | CHECK_DB_NOT_CLOSED(self); |
| 1434 | if (!checkTxnObj(txnobj, &txn)) |
| 1435 | return NULL; |
| 1436 | |
| 1437 | MYDB_BEGIN_ALLOW_THREADS; |
| 1438 | err = self->db->cursor(self->db, txn, &dbc, flags); |
| 1439 | MYDB_END_ALLOW_THREADS; |
| 1440 | RETURN_IF_ERR(); |
| 1441 | return (PyObject*) newDBCursorObject(dbc, self); |
| 1442 | } |
| 1443 | |
| 1444 | |
| 1445 | static PyObject* |
| 1446 | DB_delete(DBObject* self, PyObject* args, PyObject* kwargs) |
| 1447 | { |
| 1448 | PyObject* txnobj = NULL; |
| 1449 | int flags = 0; |
| 1450 | PyObject* keyobj; |
| 1451 | DBT key; |
| 1452 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1453 | static char* kwnames[] = { "key", "txn", "flags", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1454 | |
| 1455 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|Oi:delete", kwnames, |
| 1456 | &keyobj, &txnobj, &flags)) |
| 1457 | return NULL; |
| 1458 | CHECK_DB_NOT_CLOSED(self); |
| 1459 | if (!make_key_dbt(self, keyobj, &key, NULL)) |
| 1460 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1461 | if (!checkTxnObj(txnobj, &txn)) { |
| 1462 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1463 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1464 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1465 | |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1466 | if (-1 == _DB_delete(self, txn, &key, 0)) { |
| 1467 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1468 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1469 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1470 | |
| 1471 | FREE_DBT(key); |
| 1472 | RETURN_NONE(); |
| 1473 | } |
| 1474 | |
| 1475 | |
| 1476 | static PyObject* |
| 1477 | DB_fd(DBObject* self, PyObject* args) |
| 1478 | { |
| 1479 | int err, the_fd; |
| 1480 | |
| 1481 | if (!PyArg_ParseTuple(args,":fd")) |
| 1482 | return NULL; |
| 1483 | CHECK_DB_NOT_CLOSED(self); |
| 1484 | |
| 1485 | MYDB_BEGIN_ALLOW_THREADS; |
| 1486 | err = self->db->fd(self->db, &the_fd); |
| 1487 | MYDB_END_ALLOW_THREADS; |
| 1488 | RETURN_IF_ERR(); |
| 1489 | return PyInt_FromLong(the_fd); |
| 1490 | } |
| 1491 | |
| 1492 | |
| 1493 | static PyObject* |
| 1494 | DB_get(DBObject* self, PyObject* args, PyObject* kwargs) |
| 1495 | { |
| 1496 | int err, flags=0; |
| 1497 | PyObject* txnobj = NULL; |
| 1498 | PyObject* keyobj; |
| 1499 | PyObject* dfltobj = NULL; |
| 1500 | PyObject* retval = NULL; |
| 1501 | int dlen = -1; |
| 1502 | int doff = -1; |
| 1503 | DBT key, data; |
| 1504 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1505 | static char* kwnames[] = {"key", "default", "txn", "flags", "dlen", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 1506 | "doff", NULL}; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1507 | |
| 1508 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OOiii:get", kwnames, |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1509 | &keyobj, &dfltobj, &txnobj, &flags, &dlen, |
| 1510 | &doff)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1511 | return NULL; |
| 1512 | |
| 1513 | CHECK_DB_NOT_CLOSED(self); |
| 1514 | if (!make_key_dbt(self, keyobj, &key, &flags)) |
| 1515 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1516 | if (!checkTxnObj(txnobj, &txn)) { |
| 1517 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1518 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1519 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1520 | |
| 1521 | CLEAR_DBT(data); |
| 1522 | if (CHECK_DBFLAG(self, DB_THREAD)) { |
| 1523 | /* Tell BerkeleyDB to malloc the return value (thread safe) */ |
| 1524 | data.flags = DB_DBT_MALLOC; |
| 1525 | } |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1526 | if (!add_partial_dbt(&data, dlen, doff)) { |
| 1527 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1528 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1529 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1530 | |
| 1531 | MYDB_BEGIN_ALLOW_THREADS; |
| 1532 | err = self->db->get(self->db, txn, &key, &data, flags); |
| 1533 | MYDB_END_ALLOW_THREADS; |
| 1534 | |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 1535 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) && (dfltobj != NULL)) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1536 | err = 0; |
| 1537 | Py_INCREF(dfltobj); |
| 1538 | retval = dfltobj; |
| 1539 | } |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 1540 | else if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 1541 | && self->moduleFlags.getReturnsNone) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1542 | err = 0; |
| 1543 | Py_INCREF(Py_None); |
| 1544 | retval = Py_None; |
| 1545 | } |
| 1546 | else if (!err) { |
| 1547 | if (flags & DB_SET_RECNO) /* return both key and data */ |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1548 | retval = Py_BuildValue("s#s#", key.data, key.size, data.data, |
| 1549 | data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1550 | else /* return just the data */ |
| 1551 | retval = PyString_FromStringAndSize((char*)data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1552 | FREE_DBT(data); |
| 1553 | } |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1554 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1555 | |
| 1556 | RETURN_IF_ERR(); |
| 1557 | return retval; |
| 1558 | } |
| 1559 | |
Gregory P. Smith | 8a6a59c | 2004-12-16 09:47:28 +0000 | [diff] [blame] | 1560 | #if (DBVER >= 33) |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1561 | static PyObject* |
| 1562 | DB_pget(DBObject* self, PyObject* args, PyObject* kwargs) |
| 1563 | { |
| 1564 | int err, flags=0; |
| 1565 | PyObject* txnobj = NULL; |
| 1566 | PyObject* keyobj; |
| 1567 | PyObject* dfltobj = NULL; |
| 1568 | PyObject* retval = NULL; |
| 1569 | int dlen = -1; |
| 1570 | int doff = -1; |
| 1571 | DBT key, pkey, data; |
| 1572 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1573 | static char* kwnames[] = {"key", "default", "txn", "flags", "dlen", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 1574 | "doff", NULL}; |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1575 | |
| 1576 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OOiii:pget", kwnames, |
| 1577 | &keyobj, &dfltobj, &txnobj, &flags, &dlen, |
| 1578 | &doff)) |
| 1579 | return NULL; |
| 1580 | |
| 1581 | CHECK_DB_NOT_CLOSED(self); |
| 1582 | if (!make_key_dbt(self, keyobj, &key, &flags)) |
| 1583 | return NULL; |
| 1584 | if (!checkTxnObj(txnobj, &txn)) { |
| 1585 | FREE_DBT(key); |
| 1586 | return NULL; |
| 1587 | } |
| 1588 | |
| 1589 | CLEAR_DBT(data); |
| 1590 | if (CHECK_DBFLAG(self, DB_THREAD)) { |
| 1591 | /* Tell BerkeleyDB to malloc the return value (thread safe) */ |
| 1592 | data.flags = DB_DBT_MALLOC; |
| 1593 | } |
| 1594 | if (!add_partial_dbt(&data, dlen, doff)) { |
| 1595 | FREE_DBT(key); |
| 1596 | return NULL; |
| 1597 | } |
| 1598 | |
| 1599 | CLEAR_DBT(pkey); |
| 1600 | pkey.flags = DB_DBT_MALLOC; |
| 1601 | |
| 1602 | MYDB_BEGIN_ALLOW_THREADS; |
| 1603 | err = self->db->pget(self->db, txn, &key, &pkey, &data, flags); |
| 1604 | MYDB_END_ALLOW_THREADS; |
| 1605 | |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 1606 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) && (dfltobj != NULL)) { |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1607 | err = 0; |
| 1608 | Py_INCREF(dfltobj); |
| 1609 | retval = dfltobj; |
| 1610 | } |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 1611 | else if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 1612 | && self->moduleFlags.getReturnsNone) { |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1613 | err = 0; |
| 1614 | Py_INCREF(Py_None); |
| 1615 | retval = Py_None; |
| 1616 | } |
| 1617 | else if (!err) { |
| 1618 | PyObject *pkeyObj; |
| 1619 | PyObject *dataObj; |
| 1620 | dataObj = PyString_FromStringAndSize(data.data, data.size); |
| 1621 | |
| 1622 | if (self->primaryDBType == DB_RECNO || |
| 1623 | self->primaryDBType == DB_QUEUE) |
Neal Norwitz | 40c6b47 | 2006-01-05 05:43:35 +0000 | [diff] [blame] | 1624 | pkeyObj = PyInt_FromLong(*(int *)pkey.data); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1625 | else |
| 1626 | pkeyObj = PyString_FromStringAndSize(pkey.data, pkey.size); |
| 1627 | |
| 1628 | if (flags & DB_SET_RECNO) /* return key , pkey and data */ |
| 1629 | { |
| 1630 | PyObject *keyObj; |
| 1631 | int type = _DB_get_type(self); |
| 1632 | if (type == DB_RECNO || type == DB_QUEUE) |
Neal Norwitz | 40c6b47 | 2006-01-05 05:43:35 +0000 | [diff] [blame] | 1633 | keyObj = PyInt_FromLong(*(int *)key.data); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1634 | else |
| 1635 | keyObj = PyString_FromStringAndSize(key.data, key.size); |
Gregory P. Smith | fd049a6 | 2006-01-30 00:22:08 +0000 | [diff] [blame] | 1636 | #if (PY_VERSION_HEX >= 0x02040000) |
Gregory P. Smith | 4e414d8 | 2006-01-24 19:55:02 +0000 | [diff] [blame] | 1637 | retval = PyTuple_Pack(3, keyObj, pkeyObj, dataObj); |
Gregory P. Smith | fd049a6 | 2006-01-30 00:22:08 +0000 | [diff] [blame] | 1638 | #else |
| 1639 | retval = Py_BuildValue("OOO", keyObj, pkeyObj, dataObj); |
| 1640 | #endif |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 1641 | Py_DECREF(keyObj); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1642 | } |
| 1643 | else /* return just the pkey and data */ |
| 1644 | { |
Gregory P. Smith | fd049a6 | 2006-01-30 00:22:08 +0000 | [diff] [blame] | 1645 | #if (PY_VERSION_HEX >= 0x02040000) |
Gregory P. Smith | 4e414d8 | 2006-01-24 19:55:02 +0000 | [diff] [blame] | 1646 | retval = PyTuple_Pack(2, pkeyObj, dataObj); |
Gregory P. Smith | fd049a6 | 2006-01-30 00:22:08 +0000 | [diff] [blame] | 1647 | #else |
| 1648 | retval = Py_BuildValue("OO", pkeyObj, dataObj); |
| 1649 | #endif |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1650 | } |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 1651 | Py_DECREF(dataObj); |
| 1652 | Py_DECREF(pkeyObj); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1653 | FREE_DBT(pkey); |
| 1654 | FREE_DBT(data); |
| 1655 | } |
| 1656 | FREE_DBT(key); |
| 1657 | |
| 1658 | RETURN_IF_ERR(); |
| 1659 | return retval; |
| 1660 | } |
Gregory P. Smith | 8a6a59c | 2004-12-16 09:47:28 +0000 | [diff] [blame] | 1661 | #endif |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 1662 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1663 | |
| 1664 | /* Return size of entry */ |
| 1665 | static PyObject* |
| 1666 | DB_get_size(DBObject* self, PyObject* args, PyObject* kwargs) |
| 1667 | { |
| 1668 | int err, flags=0; |
| 1669 | PyObject* txnobj = NULL; |
| 1670 | PyObject* keyobj; |
| 1671 | PyObject* retval = NULL; |
| 1672 | DBT key, data; |
| 1673 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1674 | static char* kwnames[] = { "key", "txn", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1675 | |
| 1676 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:get_size", kwnames, |
| 1677 | &keyobj, &txnobj)) |
| 1678 | return NULL; |
| 1679 | CHECK_DB_NOT_CLOSED(self); |
| 1680 | if (!make_key_dbt(self, keyobj, &key, &flags)) |
| 1681 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1682 | if (!checkTxnObj(txnobj, &txn)) { |
| 1683 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1684 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1685 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1686 | CLEAR_DBT(data); |
| 1687 | |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 1688 | /* We don't allocate any memory, forcing a DB_BUFFER_SMALL error and |
| 1689 | thus getting the record size. */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1690 | data.flags = DB_DBT_USERMEM; |
| 1691 | data.ulen = 0; |
| 1692 | MYDB_BEGIN_ALLOW_THREADS; |
| 1693 | err = self->db->get(self->db, txn, &key, &data, flags); |
| 1694 | MYDB_END_ALLOW_THREADS; |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 1695 | if (err == DB_BUFFER_SMALL) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1696 | retval = PyInt_FromLong((long)data.size); |
| 1697 | err = 0; |
| 1698 | } |
| 1699 | |
| 1700 | FREE_DBT(key); |
| 1701 | FREE_DBT(data); |
| 1702 | RETURN_IF_ERR(); |
| 1703 | return retval; |
| 1704 | } |
| 1705 | |
| 1706 | |
| 1707 | static PyObject* |
| 1708 | DB_get_both(DBObject* self, PyObject* args, PyObject* kwargs) |
| 1709 | { |
| 1710 | int err, flags=0; |
| 1711 | PyObject* txnobj = NULL; |
| 1712 | PyObject* keyobj; |
| 1713 | PyObject* dataobj; |
| 1714 | PyObject* retval = NULL; |
| 1715 | DBT key, data; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1716 | void *orig_data; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1717 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1718 | static char* kwnames[] = { "key", "data", "txn", "flags", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1719 | |
| 1720 | |
| 1721 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|Oi:get_both", kwnames, |
| 1722 | &keyobj, &dataobj, &txnobj, &flags)) |
| 1723 | return NULL; |
| 1724 | |
| 1725 | CHECK_DB_NOT_CLOSED(self); |
| 1726 | if (!make_key_dbt(self, keyobj, &key, NULL)) |
| 1727 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1728 | if ( !make_dbt(dataobj, &data) || |
| 1729 | !checkTxnObj(txnobj, &txn) ) |
| 1730 | { |
| 1731 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1732 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 1733 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1734 | |
| 1735 | flags |= DB_GET_BOTH; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1736 | orig_data = data.data; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1737 | |
| 1738 | if (CHECK_DBFLAG(self, DB_THREAD)) { |
| 1739 | /* Tell BerkeleyDB to malloc the return value (thread safe) */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1740 | /* 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] | 1741 | data.flags = DB_DBT_MALLOC; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | MYDB_BEGIN_ALLOW_THREADS; |
| 1745 | err = self->db->get(self->db, txn, &key, &data, flags); |
| 1746 | MYDB_END_ALLOW_THREADS; |
| 1747 | |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 1748 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 1749 | && self->moduleFlags.getReturnsNone) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1750 | err = 0; |
| 1751 | Py_INCREF(Py_None); |
| 1752 | retval = Py_None; |
| 1753 | } |
| 1754 | else if (!err) { |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1755 | /* XXX(nnorwitz): can we do: retval = dataobj; Py_INCREF(retval); */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1756 | retval = PyString_FromStringAndSize((char*)data.data, data.size); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1757 | |
| 1758 | /* Even though the flags require DB_DBT_MALLOC, data is not always |
| 1759 | allocated. 4.4: allocated, 4.5: *not* allocated. :-( */ |
| 1760 | if (data.data != orig_data) |
| 1761 | FREE_DBT(data); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
| 1764 | FREE_DBT(key); |
| 1765 | RETURN_IF_ERR(); |
| 1766 | return retval; |
| 1767 | } |
| 1768 | |
| 1769 | |
| 1770 | static PyObject* |
| 1771 | DB_get_byteswapped(DBObject* self, PyObject* args) |
| 1772 | { |
| 1773 | #if (DBVER >= 33) |
| 1774 | int err = 0; |
| 1775 | #endif |
| 1776 | int retval = -1; |
| 1777 | |
| 1778 | if (!PyArg_ParseTuple(args,":get_byteswapped")) |
| 1779 | return NULL; |
| 1780 | CHECK_DB_NOT_CLOSED(self); |
| 1781 | |
| 1782 | #if (DBVER >= 33) |
| 1783 | MYDB_BEGIN_ALLOW_THREADS; |
| 1784 | err = self->db->get_byteswapped(self->db, &retval); |
| 1785 | MYDB_END_ALLOW_THREADS; |
| 1786 | RETURN_IF_ERR(); |
| 1787 | #else |
| 1788 | MYDB_BEGIN_ALLOW_THREADS; |
| 1789 | retval = self->db->get_byteswapped(self->db); |
| 1790 | MYDB_END_ALLOW_THREADS; |
| 1791 | #endif |
| 1792 | return PyInt_FromLong(retval); |
| 1793 | } |
| 1794 | |
| 1795 | |
| 1796 | static PyObject* |
| 1797 | DB_get_type(DBObject* self, PyObject* args) |
| 1798 | { |
| 1799 | int type; |
| 1800 | |
| 1801 | if (!PyArg_ParseTuple(args,":get_type")) |
| 1802 | return NULL; |
| 1803 | CHECK_DB_NOT_CLOSED(self); |
| 1804 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1805 | type = _DB_get_type(self); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1806 | if (type == -1) |
| 1807 | return NULL; |
| 1808 | return PyInt_FromLong(type); |
| 1809 | } |
| 1810 | |
| 1811 | |
| 1812 | static PyObject* |
| 1813 | DB_join(DBObject* self, PyObject* args) |
| 1814 | { |
| 1815 | int err, flags=0; |
| 1816 | int length, x; |
| 1817 | PyObject* cursorsObj; |
| 1818 | DBC** cursors; |
| 1819 | DBC* dbc; |
| 1820 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1821 | if (!PyArg_ParseTuple(args,"O|i:join", &cursorsObj, &flags)) |
| 1822 | return NULL; |
| 1823 | |
| 1824 | CHECK_DB_NOT_CLOSED(self); |
| 1825 | |
| 1826 | if (!PySequence_Check(cursorsObj)) { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1827 | PyErr_SetString(PyExc_TypeError, |
| 1828 | "Sequence of DBCursor objects expected"); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1829 | return NULL; |
| 1830 | } |
| 1831 | |
| 1832 | length = PyObject_Length(cursorsObj); |
| 1833 | cursors = malloc((length+1) * sizeof(DBC*)); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1834 | if (!cursors) { |
| 1835 | PyErr_NoMemory(); |
| 1836 | return NULL; |
| 1837 | } |
| 1838 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1839 | cursors[length] = NULL; |
| 1840 | for (x=0; x<length; x++) { |
| 1841 | PyObject* item = PySequence_GetItem(cursorsObj, x); |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 1842 | if (item == NULL) { |
| 1843 | free(cursors); |
| 1844 | return NULL; |
| 1845 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1846 | if (!DBCursorObject_Check(item)) { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1847 | PyErr_SetString(PyExc_TypeError, |
| 1848 | "Sequence of DBCursor objects expected"); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1849 | free(cursors); |
| 1850 | return NULL; |
| 1851 | } |
| 1852 | cursors[x] = ((DBCursorObject*)item)->dbc; |
Thomas Wouters | b2820ae | 2006-03-12 00:01:38 +0000 | [diff] [blame] | 1853 | Py_DECREF(item); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | MYDB_BEGIN_ALLOW_THREADS; |
| 1857 | err = self->db->join(self->db, cursors, &dbc, flags); |
| 1858 | MYDB_END_ALLOW_THREADS; |
| 1859 | free(cursors); |
| 1860 | RETURN_IF_ERR(); |
| 1861 | |
Gregory P. Smith | 7441e65 | 2003-11-03 21:35:31 +0000 | [diff] [blame] | 1862 | /* FIXME: this is a buggy interface. The returned cursor |
| 1863 | contains internal references to the passed in cursors |
| 1864 | but does not hold python references to them or prevent |
| 1865 | them from being closed prematurely. This can cause |
| 1866 | python to crash when things are done in the wrong order. */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1867 | return (PyObject*) newDBCursorObject(dbc, self); |
| 1868 | } |
| 1869 | |
| 1870 | |
| 1871 | static PyObject* |
| 1872 | DB_key_range(DBObject* self, PyObject* args, PyObject* kwargs) |
| 1873 | { |
| 1874 | int err, flags=0; |
| 1875 | PyObject* txnobj = NULL; |
| 1876 | PyObject* keyobj; |
| 1877 | DBT key; |
| 1878 | DB_TXN *txn = NULL; |
| 1879 | DB_KEY_RANGE range; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1880 | static char* kwnames[] = { "key", "txn", "flags", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1881 | |
| 1882 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|Oi:key_range", kwnames, |
| 1883 | &keyobj, &txnobj, &flags)) |
| 1884 | return NULL; |
| 1885 | CHECK_DB_NOT_CLOSED(self); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1886 | if (!make_dbt(keyobj, &key)) |
| 1887 | /* 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] | 1888 | return NULL; |
| 1889 | if (!checkTxnObj(txnobj, &txn)) |
| 1890 | return NULL; |
| 1891 | |
| 1892 | MYDB_BEGIN_ALLOW_THREADS; |
| 1893 | err = self->db->key_range(self->db, txn, &key, &range, flags); |
| 1894 | MYDB_END_ALLOW_THREADS; |
| 1895 | |
| 1896 | RETURN_IF_ERR(); |
| 1897 | return Py_BuildValue("ddd", range.less, range.equal, range.greater); |
| 1898 | } |
| 1899 | |
| 1900 | |
| 1901 | static PyObject* |
| 1902 | DB_open(DBObject* self, PyObject* args, PyObject* kwargs) |
| 1903 | { |
| 1904 | int err, type = DB_UNKNOWN, flags=0, mode=0660; |
| 1905 | char* filename = NULL; |
| 1906 | char* dbname = NULL; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1907 | #if (DBVER >= 41) |
| 1908 | PyObject *txnobj = NULL; |
| 1909 | DB_TXN *txn = NULL; |
| 1910 | /* with dbname */ |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1911 | static char* kwnames[] = { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1912 | "filename", "dbname", "dbtype", "flags", "mode", "txn", NULL}; |
| 1913 | /* without dbname */ |
Tim Peters | 85b1052 | 2006-02-28 18:33:35 +0000 | [diff] [blame] | 1914 | static char* kwnames_basic[] = { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1915 | "filename", "dbtype", "flags", "mode", "txn", NULL}; |
| 1916 | #else |
| 1917 | /* with dbname */ |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1918 | static char* kwnames[] = { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1919 | "filename", "dbname", "dbtype", "flags", "mode", NULL}; |
| 1920 | /* without dbname */ |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 1921 | static char* kwnames_basic[] = { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1922 | "filename", "dbtype", "flags", "mode", NULL}; |
| 1923 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1924 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1925 | #if (DBVER >= 41) |
| 1926 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "z|ziiiO:open", kwnames, |
| 1927 | &filename, &dbname, &type, &flags, &mode, |
| 1928 | &txnobj)) |
| 1929 | #else |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1930 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "z|ziii:open", kwnames, |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1931 | &filename, &dbname, &type, &flags, |
| 1932 | &mode)) |
| 1933 | #endif |
| 1934 | { |
| 1935 | PyErr_Clear(); |
| 1936 | type = DB_UNKNOWN; flags = 0; mode = 0660; |
| 1937 | filename = NULL; dbname = NULL; |
| 1938 | #if (DBVER >= 41) |
| 1939 | if (!PyArg_ParseTupleAndKeywords(args, kwargs,"z|iiiO:open", |
| 1940 | kwnames_basic, |
| 1941 | &filename, &type, &flags, &mode, |
| 1942 | &txnobj)) |
| 1943 | return NULL; |
| 1944 | #else |
| 1945 | if (!PyArg_ParseTupleAndKeywords(args, kwargs,"z|iii:open", |
| 1946 | kwnames_basic, |
| 1947 | &filename, &type, &flags, &mode)) |
| 1948 | return NULL; |
| 1949 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1952 | #if (DBVER >= 41) |
| 1953 | if (!checkTxnObj(txnobj, &txn)) return NULL; |
| 1954 | #endif |
| 1955 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1956 | if (NULL == self->db) { |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 1957 | PyObject *t = Py_BuildValue("(is)", 0, |
| 1958 | "Cannot call open() twice for DB object"); |
| 1959 | PyErr_SetObject(DBError, t); |
| 1960 | Py_DECREF(t); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1961 | return NULL; |
| 1962 | } |
| 1963 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1964 | #if 0 && (DBVER >= 41) |
| 1965 | if ((!txn) && (txnobj != Py_None) && self->myenvobj |
| 1966 | && (self->myenvobj->flags & DB_INIT_TXN)) |
| 1967 | { |
| 1968 | /* If no 'txn' parameter was supplied (no DbTxn object and None was not |
| 1969 | * explicitly passed) but we are in a transaction ready environment: |
| 1970 | * add DB_AUTO_COMMIT to allow for older pybsddb apps using transactions |
| 1971 | * to work on BerkeleyDB 4.1 without needing to modify their |
| 1972 | * DBEnv or DB open calls. |
| 1973 | * TODO make this behaviour of the library configurable. |
| 1974 | */ |
| 1975 | flags |= DB_AUTO_COMMIT; |
| 1976 | } |
| 1977 | #endif |
| 1978 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1979 | MYDB_BEGIN_ALLOW_THREADS; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1980 | #if (DBVER >= 41) |
| 1981 | err = self->db->open(self->db, txn, filename, dbname, type, flags, mode); |
| 1982 | #else |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1983 | err = self->db->open(self->db, filename, dbname, type, flags, mode); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 1984 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1985 | MYDB_END_ALLOW_THREADS; |
| 1986 | if (makeDBError(err)) { |
Gregory P. Smith | b6c9f78 | 2003-01-17 08:42:50 +0000 | [diff] [blame] | 1987 | self->db->close(self->db, 0); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1988 | self->db = NULL; |
| 1989 | return NULL; |
| 1990 | } |
| 1991 | |
| 1992 | self->flags = flags; |
| 1993 | RETURN_NONE(); |
| 1994 | } |
| 1995 | |
| 1996 | |
| 1997 | static PyObject* |
| 1998 | DB_put(DBObject* self, PyObject* args, PyObject* kwargs) |
| 1999 | { |
| 2000 | int flags=0; |
| 2001 | PyObject* txnobj = NULL; |
| 2002 | int dlen = -1; |
| 2003 | int doff = -1; |
| 2004 | PyObject* keyobj, *dataobj, *retval; |
| 2005 | DBT key, data; |
| 2006 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2007 | static char* kwnames[] = { "key", "data", "txn", "flags", "dlen", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 2008 | "doff", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2009 | |
| 2010 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|Oiii:put", kwnames, |
| 2011 | &keyobj, &dataobj, &txnobj, &flags, &dlen, &doff)) |
| 2012 | return NULL; |
| 2013 | |
| 2014 | CHECK_DB_NOT_CLOSED(self); |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 2015 | if (!make_key_dbt(self, keyobj, &key, NULL)) |
| 2016 | return NULL; |
| 2017 | if ( !make_dbt(dataobj, &data) || |
| 2018 | !add_partial_dbt(&data, dlen, doff) || |
| 2019 | !checkTxnObj(txnobj, &txn) ) |
| 2020 | { |
| 2021 | FREE_DBT(key); |
| 2022 | return NULL; |
| 2023 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2024 | |
| 2025 | if (-1 == _DB_put(self, txn, &key, &data, flags)) { |
| 2026 | FREE_DBT(key); |
| 2027 | return NULL; |
| 2028 | } |
| 2029 | |
| 2030 | if (flags & DB_APPEND) |
| 2031 | retval = PyInt_FromLong(*((db_recno_t*)key.data)); |
| 2032 | else { |
| 2033 | retval = Py_None; |
| 2034 | Py_INCREF(retval); |
| 2035 | } |
| 2036 | FREE_DBT(key); |
| 2037 | return retval; |
| 2038 | } |
| 2039 | |
| 2040 | |
| 2041 | |
| 2042 | static PyObject* |
| 2043 | DB_remove(DBObject* self, PyObject* args, PyObject* kwargs) |
| 2044 | { |
| 2045 | char* filename; |
| 2046 | char* database = NULL; |
| 2047 | int err, flags=0; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2048 | static char* kwnames[] = { "filename", "dbname", "flags", NULL}; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2049 | |
| 2050 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|zi:remove", kwnames, |
| 2051 | &filename, &database, &flags)) |
| 2052 | return NULL; |
| 2053 | CHECK_DB_NOT_CLOSED(self); |
| 2054 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2055 | err = self->db->remove(self->db, filename, database, flags); |
Gregory P. Smith | f655dff | 2003-05-15 00:13:18 +0000 | [diff] [blame] | 2056 | self->db = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2057 | RETURN_IF_ERR(); |
| 2058 | RETURN_NONE(); |
| 2059 | } |
| 2060 | |
| 2061 | |
| 2062 | |
| 2063 | static PyObject* |
| 2064 | DB_rename(DBObject* self, PyObject* args) |
| 2065 | { |
| 2066 | char* filename; |
| 2067 | char* database; |
| 2068 | char* newname; |
| 2069 | int err, flags=0; |
| 2070 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2071 | if (!PyArg_ParseTuple(args, "sss|i:rename", &filename, &database, &newname, |
| 2072 | &flags)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2073 | return NULL; |
| 2074 | CHECK_DB_NOT_CLOSED(self); |
| 2075 | |
| 2076 | MYDB_BEGIN_ALLOW_THREADS; |
| 2077 | err = self->db->rename(self->db, filename, database, newname, flags); |
| 2078 | MYDB_END_ALLOW_THREADS; |
| 2079 | RETURN_IF_ERR(); |
| 2080 | RETURN_NONE(); |
| 2081 | } |
| 2082 | |
| 2083 | |
| 2084 | static PyObject* |
| 2085 | DB_set_bt_minkey(DBObject* self, PyObject* args) |
| 2086 | { |
| 2087 | int err, minkey; |
| 2088 | |
| 2089 | if (!PyArg_ParseTuple(args,"i:set_bt_minkey", &minkey )) |
| 2090 | return NULL; |
| 2091 | CHECK_DB_NOT_CLOSED(self); |
| 2092 | |
| 2093 | MYDB_BEGIN_ALLOW_THREADS; |
| 2094 | err = self->db->set_bt_minkey(self->db, minkey); |
| 2095 | MYDB_END_ALLOW_THREADS; |
| 2096 | RETURN_IF_ERR(); |
| 2097 | RETURN_NONE(); |
| 2098 | } |
| 2099 | |
Neal Norwitz | 8456235 | 2005-10-20 04:30:15 +0000 | [diff] [blame] | 2100 | #if (DBVER >= 33) |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2101 | static int |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2102 | _default_cmp(const DBT *leftKey, |
| 2103 | const DBT *rightKey) |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2104 | { |
| 2105 | int res; |
| 2106 | int lsize = leftKey->size, rsize = rightKey->size; |
| 2107 | |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2108 | res = memcmp(leftKey->data, rightKey->data, |
| 2109 | lsize < rsize ? lsize : rsize); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2110 | |
| 2111 | if (res == 0) { |
| 2112 | if (lsize < rsize) { |
| 2113 | res = -1; |
| 2114 | } |
| 2115 | else if (lsize > rsize) { |
| 2116 | res = 1; |
| 2117 | } |
| 2118 | } |
| 2119 | return res; |
| 2120 | } |
| 2121 | |
| 2122 | static int |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2123 | _db_compareCallback(DB* db, |
| 2124 | const DBT *leftKey, |
| 2125 | const DBT *rightKey) |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2126 | { |
| 2127 | int res = 0; |
| 2128 | PyObject *args; |
Thomas Wouters | b2820ae | 2006-03-12 00:01:38 +0000 | [diff] [blame] | 2129 | PyObject *result = NULL; |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2130 | DBObject *self = (DBObject *)db->app_private; |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2131 | |
| 2132 | if (self == NULL || self->btCompareCallback == NULL) { |
| 2133 | MYDB_BEGIN_BLOCK_THREADS; |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2134 | PyErr_SetString(PyExc_TypeError, |
| 2135 | (self == 0 |
| 2136 | ? "DB_bt_compare db is NULL." |
| 2137 | : "DB_bt_compare callback is NULL.")); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2138 | /* 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] | 2139 | PyErr_Print(); |
| 2140 | res = _default_cmp(leftKey, rightKey); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2141 | MYDB_END_BLOCK_THREADS; |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2142 | } else { |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2143 | MYDB_BEGIN_BLOCK_THREADS; |
| 2144 | |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2145 | args = Py_BuildValue("s#s#", leftKey->data, leftKey->size, |
| 2146 | rightKey->data, rightKey->size); |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2147 | if (args != NULL) { |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2148 | /* XXX(twouters) I highly doubt this INCREF is correct */ |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2149 | Py_INCREF(self); |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2150 | result = PyEval_CallObject(self->btCompareCallback, args); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2151 | } |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2152 | if (args == NULL || result == NULL) { |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2153 | /* 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] | 2154 | PyErr_Print(); |
| 2155 | res = _default_cmp(leftKey, rightKey); |
| 2156 | } else if (PyInt_Check(result)) { |
| 2157 | res = PyInt_AsLong(result); |
| 2158 | } else { |
| 2159 | PyErr_SetString(PyExc_TypeError, |
| 2160 | "DB_bt_compare callback MUST return an int."); |
| 2161 | /* we're in a callback within the DB code, we can't raise */ |
| 2162 | PyErr_Print(); |
| 2163 | res = _default_cmp(leftKey, rightKey); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2166 | Py_XDECREF(args); |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2167 | Py_XDECREF(result); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2168 | |
| 2169 | MYDB_END_BLOCK_THREADS; |
| 2170 | } |
| 2171 | return res; |
| 2172 | } |
| 2173 | |
| 2174 | static PyObject* |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2175 | DB_set_bt_compare(DBObject* self, PyObject* args) |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2176 | { |
| 2177 | int err; |
| 2178 | PyObject *comparator; |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2179 | PyObject *tuple, *result; |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2180 | |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2181 | if (!PyArg_ParseTuple(args, "O:set_bt_compare", &comparator)) |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2182 | return NULL; |
| 2183 | |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2184 | CHECK_DB_NOT_CLOSED(self); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2185 | |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2186 | if (!PyCallable_Check(comparator)) { |
| 2187 | makeTypeError("Callable", comparator); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2188 | return NULL; |
| 2189 | } |
| 2190 | |
| 2191 | /* |
| 2192 | * Perform a test call of the comparator function with two empty |
| 2193 | * string objects here. verify that it returns an int (0). |
| 2194 | * err if not. |
| 2195 | */ |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2196 | tuple = Py_BuildValue("(ss)", "", ""); |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2197 | result = PyEval_CallObject(comparator, tuple); |
| 2198 | Py_DECREF(tuple); |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2199 | if (result == NULL) |
| 2200 | return NULL; |
| 2201 | if (!PyInt_Check(result)) { |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2202 | PyErr_SetString(PyExc_TypeError, |
| 2203 | "callback MUST return an int"); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2204 | return NULL; |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2205 | } else if (PyInt_AsLong(result) != 0) { |
| 2206 | PyErr_SetString(PyExc_TypeError, |
| 2207 | "callback failed to return 0 on two empty strings"); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2208 | return NULL; |
| 2209 | } |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2210 | Py_DECREF(result); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2211 | |
| 2212 | /* We don't accept multiple set_bt_compare operations, in order to |
| 2213 | * simplify the code. This would have no real use, as one cannot |
| 2214 | * change the function once the db is opened anyway */ |
| 2215 | if (self->btCompareCallback != NULL) { |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2216 | 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] | 2217 | return NULL; |
| 2218 | } |
| 2219 | |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2220 | Py_INCREF(comparator); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2221 | self->btCompareCallback = comparator; |
| 2222 | |
| 2223 | /* This is to workaround a problem with un-initialized threads (see |
| 2224 | comment in DB_associate) */ |
| 2225 | #ifdef WITH_THREAD |
| 2226 | PyEval_InitThreads(); |
| 2227 | #endif |
| 2228 | |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2229 | err = self->db->set_bt_compare(self->db, _db_compareCallback); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2230 | |
| 2231 | if (err) { |
| 2232 | /* restore the old state in case of error */ |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2233 | Py_DECREF(comparator); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2234 | self->btCompareCallback = NULL; |
| 2235 | } |
| 2236 | |
Georg Brandl | ef1701f | 2006-03-07 14:57:48 +0000 | [diff] [blame] | 2237 | RETURN_IF_ERR(); |
| 2238 | RETURN_NONE(); |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2239 | } |
Neal Norwitz | 8456235 | 2005-10-20 04:30:15 +0000 | [diff] [blame] | 2240 | #endif /* DBVER >= 33 */ |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 2241 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2242 | |
| 2243 | static PyObject* |
| 2244 | DB_set_cachesize(DBObject* self, PyObject* args) |
| 2245 | { |
| 2246 | int err; |
| 2247 | int gbytes = 0, bytes = 0, ncache = 0; |
| 2248 | |
| 2249 | if (!PyArg_ParseTuple(args,"ii|i:set_cachesize", |
| 2250 | &gbytes,&bytes,&ncache)) |
| 2251 | return NULL; |
| 2252 | CHECK_DB_NOT_CLOSED(self); |
| 2253 | |
| 2254 | MYDB_BEGIN_ALLOW_THREADS; |
| 2255 | err = self->db->set_cachesize(self->db, gbytes, bytes, ncache); |
| 2256 | MYDB_END_ALLOW_THREADS; |
| 2257 | RETURN_IF_ERR(); |
| 2258 | RETURN_NONE(); |
| 2259 | } |
| 2260 | |
| 2261 | |
| 2262 | static PyObject* |
| 2263 | DB_set_flags(DBObject* self, PyObject* args) |
| 2264 | { |
| 2265 | int err, flags; |
| 2266 | |
| 2267 | if (!PyArg_ParseTuple(args,"i:set_flags", &flags)) |
| 2268 | return NULL; |
| 2269 | CHECK_DB_NOT_CLOSED(self); |
| 2270 | |
| 2271 | MYDB_BEGIN_ALLOW_THREADS; |
| 2272 | err = self->db->set_flags(self->db, flags); |
| 2273 | MYDB_END_ALLOW_THREADS; |
| 2274 | RETURN_IF_ERR(); |
| 2275 | |
| 2276 | self->setflags |= flags; |
| 2277 | RETURN_NONE(); |
| 2278 | } |
| 2279 | |
| 2280 | |
| 2281 | static PyObject* |
| 2282 | DB_set_h_ffactor(DBObject* self, PyObject* args) |
| 2283 | { |
| 2284 | int err, ffactor; |
| 2285 | |
| 2286 | if (!PyArg_ParseTuple(args,"i:set_h_ffactor", &ffactor)) |
| 2287 | return NULL; |
| 2288 | CHECK_DB_NOT_CLOSED(self); |
| 2289 | |
| 2290 | MYDB_BEGIN_ALLOW_THREADS; |
| 2291 | err = self->db->set_h_ffactor(self->db, ffactor); |
| 2292 | MYDB_END_ALLOW_THREADS; |
| 2293 | RETURN_IF_ERR(); |
| 2294 | RETURN_NONE(); |
| 2295 | } |
| 2296 | |
| 2297 | |
| 2298 | static PyObject* |
| 2299 | DB_set_h_nelem(DBObject* self, PyObject* args) |
| 2300 | { |
| 2301 | int err, nelem; |
| 2302 | |
| 2303 | if (!PyArg_ParseTuple(args,"i:set_h_nelem", &nelem)) |
| 2304 | return NULL; |
| 2305 | CHECK_DB_NOT_CLOSED(self); |
| 2306 | |
| 2307 | MYDB_BEGIN_ALLOW_THREADS; |
| 2308 | err = self->db->set_h_nelem(self->db, nelem); |
| 2309 | MYDB_END_ALLOW_THREADS; |
| 2310 | RETURN_IF_ERR(); |
| 2311 | RETURN_NONE(); |
| 2312 | } |
| 2313 | |
| 2314 | |
| 2315 | static PyObject* |
| 2316 | DB_set_lorder(DBObject* self, PyObject* args) |
| 2317 | { |
| 2318 | int err, lorder; |
| 2319 | |
| 2320 | if (!PyArg_ParseTuple(args,"i:set_lorder", &lorder)) |
| 2321 | return NULL; |
| 2322 | CHECK_DB_NOT_CLOSED(self); |
| 2323 | |
| 2324 | MYDB_BEGIN_ALLOW_THREADS; |
| 2325 | err = self->db->set_lorder(self->db, lorder); |
| 2326 | MYDB_END_ALLOW_THREADS; |
| 2327 | RETURN_IF_ERR(); |
| 2328 | RETURN_NONE(); |
| 2329 | } |
| 2330 | |
| 2331 | |
| 2332 | static PyObject* |
| 2333 | DB_set_pagesize(DBObject* self, PyObject* args) |
| 2334 | { |
| 2335 | int err, pagesize; |
| 2336 | |
| 2337 | if (!PyArg_ParseTuple(args,"i:set_pagesize", &pagesize)) |
| 2338 | return NULL; |
| 2339 | CHECK_DB_NOT_CLOSED(self); |
| 2340 | |
| 2341 | MYDB_BEGIN_ALLOW_THREADS; |
| 2342 | err = self->db->set_pagesize(self->db, pagesize); |
| 2343 | MYDB_END_ALLOW_THREADS; |
| 2344 | RETURN_IF_ERR(); |
| 2345 | RETURN_NONE(); |
| 2346 | } |
| 2347 | |
| 2348 | |
| 2349 | static PyObject* |
| 2350 | DB_set_re_delim(DBObject* self, PyObject* args) |
| 2351 | { |
| 2352 | int err; |
| 2353 | char delim; |
| 2354 | |
| 2355 | if (!PyArg_ParseTuple(args,"b:set_re_delim", &delim)) { |
| 2356 | PyErr_Clear(); |
| 2357 | if (!PyArg_ParseTuple(args,"c:set_re_delim", &delim)) |
| 2358 | return NULL; |
| 2359 | } |
| 2360 | |
| 2361 | CHECK_DB_NOT_CLOSED(self); |
| 2362 | |
| 2363 | MYDB_BEGIN_ALLOW_THREADS; |
| 2364 | err = self->db->set_re_delim(self->db, delim); |
| 2365 | MYDB_END_ALLOW_THREADS; |
| 2366 | RETURN_IF_ERR(); |
| 2367 | RETURN_NONE(); |
| 2368 | } |
| 2369 | |
| 2370 | static PyObject* |
| 2371 | DB_set_re_len(DBObject* self, PyObject* args) |
| 2372 | { |
| 2373 | int err, len; |
| 2374 | |
| 2375 | if (!PyArg_ParseTuple(args,"i:set_re_len", &len)) |
| 2376 | return NULL; |
| 2377 | CHECK_DB_NOT_CLOSED(self); |
| 2378 | |
| 2379 | MYDB_BEGIN_ALLOW_THREADS; |
| 2380 | err = self->db->set_re_len(self->db, len); |
| 2381 | MYDB_END_ALLOW_THREADS; |
| 2382 | RETURN_IF_ERR(); |
| 2383 | RETURN_NONE(); |
| 2384 | } |
| 2385 | |
| 2386 | |
| 2387 | static PyObject* |
| 2388 | DB_set_re_pad(DBObject* self, PyObject* args) |
| 2389 | { |
| 2390 | int err; |
| 2391 | char pad; |
| 2392 | |
| 2393 | if (!PyArg_ParseTuple(args,"b:set_re_pad", &pad)) { |
| 2394 | PyErr_Clear(); |
| 2395 | if (!PyArg_ParseTuple(args,"c:set_re_pad", &pad)) |
| 2396 | return NULL; |
| 2397 | } |
| 2398 | CHECK_DB_NOT_CLOSED(self); |
| 2399 | |
| 2400 | MYDB_BEGIN_ALLOW_THREADS; |
| 2401 | err = self->db->set_re_pad(self->db, pad); |
| 2402 | MYDB_END_ALLOW_THREADS; |
| 2403 | RETURN_IF_ERR(); |
| 2404 | RETURN_NONE(); |
| 2405 | } |
| 2406 | |
| 2407 | |
| 2408 | static PyObject* |
| 2409 | DB_set_re_source(DBObject* self, PyObject* args) |
| 2410 | { |
| 2411 | int err; |
| 2412 | char *re_source; |
| 2413 | |
| 2414 | if (!PyArg_ParseTuple(args,"s:set_re_source", &re_source)) |
| 2415 | return NULL; |
| 2416 | CHECK_DB_NOT_CLOSED(self); |
| 2417 | |
| 2418 | MYDB_BEGIN_ALLOW_THREADS; |
| 2419 | err = self->db->set_re_source(self->db, re_source); |
| 2420 | MYDB_END_ALLOW_THREADS; |
| 2421 | RETURN_IF_ERR(); |
| 2422 | RETURN_NONE(); |
| 2423 | } |
| 2424 | |
| 2425 | |
| 2426 | #if (DBVER >= 32) |
| 2427 | static PyObject* |
| 2428 | DB_set_q_extentsize(DBObject* self, PyObject* args) |
| 2429 | { |
| 2430 | int err; |
| 2431 | int extentsize; |
| 2432 | |
| 2433 | if (!PyArg_ParseTuple(args,"i:set_q_extentsize", &extentsize)) |
| 2434 | return NULL; |
| 2435 | CHECK_DB_NOT_CLOSED(self); |
| 2436 | |
| 2437 | MYDB_BEGIN_ALLOW_THREADS; |
| 2438 | err = self->db->set_q_extentsize(self->db, extentsize); |
| 2439 | MYDB_END_ALLOW_THREADS; |
| 2440 | RETURN_IF_ERR(); |
| 2441 | RETURN_NONE(); |
| 2442 | } |
| 2443 | #endif |
| 2444 | |
| 2445 | static PyObject* |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 2446 | DB_stat(DBObject* self, PyObject* args, PyObject* kwargs) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2447 | { |
| 2448 | int err, flags = 0, type; |
| 2449 | void* sp; |
| 2450 | PyObject* d; |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 2451 | #if (DBVER >= 43) |
| 2452 | PyObject* txnobj = NULL; |
| 2453 | DB_TXN *txn = NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2454 | static char* kwnames[] = { "flags", "txn", NULL }; |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 2455 | #else |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2456 | static char* kwnames[] = { "flags", NULL }; |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 2457 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2458 | |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 2459 | #if (DBVER >= 43) |
| 2460 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iO:stat", kwnames, |
| 2461 | &flags, &txnobj)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2462 | return NULL; |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 2463 | if (!checkTxnObj(txnobj, &txn)) |
| 2464 | return NULL; |
| 2465 | #else |
| 2466 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:stat", kwnames, &flags)) |
| 2467 | return NULL; |
| 2468 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2469 | CHECK_DB_NOT_CLOSED(self); |
| 2470 | |
| 2471 | MYDB_BEGIN_ALLOW_THREADS; |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 2472 | #if (DBVER >= 43) |
| 2473 | err = self->db->stat(self->db, txn, &sp, flags); |
| 2474 | #elif (DBVER >= 33) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2475 | err = self->db->stat(self->db, &sp, flags); |
| 2476 | #else |
| 2477 | err = self->db->stat(self->db, &sp, NULL, flags); |
| 2478 | #endif |
| 2479 | MYDB_END_ALLOW_THREADS; |
| 2480 | RETURN_IF_ERR(); |
| 2481 | |
| 2482 | self->haveStat = 1; |
| 2483 | |
| 2484 | /* Turn the stat structure into a dictionary */ |
| 2485 | type = _DB_get_type(self); |
| 2486 | if ((type == -1) || ((d = PyDict_New()) == NULL)) { |
| 2487 | free(sp); |
| 2488 | return NULL; |
| 2489 | } |
| 2490 | |
| 2491 | #define MAKE_HASH_ENTRY(name) _addIntToDict(d, #name, ((DB_HASH_STAT*)sp)->hash_##name) |
| 2492 | #define MAKE_BT_ENTRY(name) _addIntToDict(d, #name, ((DB_BTREE_STAT*)sp)->bt_##name) |
| 2493 | #define MAKE_QUEUE_ENTRY(name) _addIntToDict(d, #name, ((DB_QUEUE_STAT*)sp)->qs_##name) |
| 2494 | |
| 2495 | switch (type) { |
| 2496 | case DB_HASH: |
| 2497 | MAKE_HASH_ENTRY(magic); |
| 2498 | MAKE_HASH_ENTRY(version); |
| 2499 | MAKE_HASH_ENTRY(nkeys); |
| 2500 | MAKE_HASH_ENTRY(ndata); |
| 2501 | MAKE_HASH_ENTRY(pagesize); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2502 | #if (DBVER < 41) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2503 | MAKE_HASH_ENTRY(nelem); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2504 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2505 | MAKE_HASH_ENTRY(ffactor); |
| 2506 | MAKE_HASH_ENTRY(buckets); |
| 2507 | MAKE_HASH_ENTRY(free); |
| 2508 | MAKE_HASH_ENTRY(bfree); |
| 2509 | MAKE_HASH_ENTRY(bigpages); |
| 2510 | MAKE_HASH_ENTRY(big_bfree); |
| 2511 | MAKE_HASH_ENTRY(overflows); |
| 2512 | MAKE_HASH_ENTRY(ovfl_free); |
| 2513 | MAKE_HASH_ENTRY(dup); |
| 2514 | MAKE_HASH_ENTRY(dup_free); |
| 2515 | break; |
| 2516 | |
| 2517 | case DB_BTREE: |
| 2518 | case DB_RECNO: |
| 2519 | MAKE_BT_ENTRY(magic); |
| 2520 | MAKE_BT_ENTRY(version); |
| 2521 | MAKE_BT_ENTRY(nkeys); |
| 2522 | MAKE_BT_ENTRY(ndata); |
| 2523 | MAKE_BT_ENTRY(pagesize); |
| 2524 | MAKE_BT_ENTRY(minkey); |
| 2525 | MAKE_BT_ENTRY(re_len); |
| 2526 | MAKE_BT_ENTRY(re_pad); |
| 2527 | MAKE_BT_ENTRY(levels); |
| 2528 | MAKE_BT_ENTRY(int_pg); |
| 2529 | MAKE_BT_ENTRY(leaf_pg); |
| 2530 | MAKE_BT_ENTRY(dup_pg); |
| 2531 | MAKE_BT_ENTRY(over_pg); |
| 2532 | MAKE_BT_ENTRY(free); |
| 2533 | MAKE_BT_ENTRY(int_pgfree); |
| 2534 | MAKE_BT_ENTRY(leaf_pgfree); |
| 2535 | MAKE_BT_ENTRY(dup_pgfree); |
| 2536 | MAKE_BT_ENTRY(over_pgfree); |
| 2537 | break; |
| 2538 | |
| 2539 | case DB_QUEUE: |
| 2540 | MAKE_QUEUE_ENTRY(magic); |
| 2541 | MAKE_QUEUE_ENTRY(version); |
| 2542 | MAKE_QUEUE_ENTRY(nkeys); |
| 2543 | MAKE_QUEUE_ENTRY(ndata); |
| 2544 | MAKE_QUEUE_ENTRY(pagesize); |
| 2545 | MAKE_QUEUE_ENTRY(pages); |
| 2546 | MAKE_QUEUE_ENTRY(re_len); |
| 2547 | MAKE_QUEUE_ENTRY(re_pad); |
| 2548 | MAKE_QUEUE_ENTRY(pgfree); |
| 2549 | #if (DBVER == 31) |
| 2550 | MAKE_QUEUE_ENTRY(start); |
| 2551 | #endif |
| 2552 | MAKE_QUEUE_ENTRY(first_recno); |
| 2553 | MAKE_QUEUE_ENTRY(cur_recno); |
| 2554 | break; |
| 2555 | |
| 2556 | default: |
| 2557 | PyErr_SetString(PyExc_TypeError, "Unknown DB type, unable to stat"); |
| 2558 | Py_DECREF(d); |
| 2559 | d = NULL; |
| 2560 | } |
| 2561 | |
| 2562 | #undef MAKE_HASH_ENTRY |
| 2563 | #undef MAKE_BT_ENTRY |
| 2564 | #undef MAKE_QUEUE_ENTRY |
| 2565 | |
| 2566 | free(sp); |
| 2567 | return d; |
| 2568 | } |
| 2569 | |
| 2570 | static PyObject* |
| 2571 | DB_sync(DBObject* self, PyObject* args) |
| 2572 | { |
| 2573 | int err; |
| 2574 | int flags = 0; |
| 2575 | |
| 2576 | if (!PyArg_ParseTuple(args,"|i:sync", &flags )) |
| 2577 | return NULL; |
| 2578 | CHECK_DB_NOT_CLOSED(self); |
| 2579 | |
| 2580 | MYDB_BEGIN_ALLOW_THREADS; |
| 2581 | err = self->db->sync(self->db, flags); |
| 2582 | MYDB_END_ALLOW_THREADS; |
| 2583 | RETURN_IF_ERR(); |
| 2584 | RETURN_NONE(); |
| 2585 | } |
| 2586 | |
| 2587 | |
| 2588 | #if (DBVER >= 33) |
| 2589 | static PyObject* |
| 2590 | DB_truncate(DBObject* self, PyObject* args, PyObject* kwargs) |
| 2591 | { |
| 2592 | int err, flags=0; |
| 2593 | u_int32_t count=0; |
| 2594 | PyObject* txnobj = NULL; |
| 2595 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2596 | static char* kwnames[] = { "txn", "flags", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2597 | |
| 2598 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oi:cursor", kwnames, |
| 2599 | &txnobj, &flags)) |
| 2600 | return NULL; |
| 2601 | CHECK_DB_NOT_CLOSED(self); |
| 2602 | if (!checkTxnObj(txnobj, &txn)) |
| 2603 | return NULL; |
| 2604 | |
| 2605 | MYDB_BEGIN_ALLOW_THREADS; |
| 2606 | err = self->db->truncate(self->db, txn, &count, flags); |
| 2607 | MYDB_END_ALLOW_THREADS; |
| 2608 | RETURN_IF_ERR(); |
| 2609 | return PyInt_FromLong(count); |
| 2610 | } |
| 2611 | #endif |
| 2612 | |
| 2613 | |
| 2614 | static PyObject* |
| 2615 | DB_upgrade(DBObject* self, PyObject* args) |
| 2616 | { |
| 2617 | int err, flags=0; |
| 2618 | char *filename; |
| 2619 | |
| 2620 | if (!PyArg_ParseTuple(args,"s|i:upgrade", &filename, &flags)) |
| 2621 | return NULL; |
| 2622 | CHECK_DB_NOT_CLOSED(self); |
| 2623 | |
| 2624 | MYDB_BEGIN_ALLOW_THREADS; |
| 2625 | err = self->db->upgrade(self->db, filename, flags); |
| 2626 | MYDB_END_ALLOW_THREADS; |
| 2627 | RETURN_IF_ERR(); |
| 2628 | RETURN_NONE(); |
| 2629 | } |
| 2630 | |
| 2631 | |
| 2632 | static PyObject* |
| 2633 | DB_verify(DBObject* self, PyObject* args, PyObject* kwargs) |
| 2634 | { |
| 2635 | int err, flags=0; |
| 2636 | char* fileName; |
| 2637 | char* dbName=NULL; |
| 2638 | char* outFileName=NULL; |
| 2639 | FILE* outFile=NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2640 | static char* kwnames[] = { "filename", "dbname", "outfile", "flags", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 2641 | NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2642 | |
| 2643 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|zzi:verify", kwnames, |
| 2644 | &fileName, &dbName, &outFileName, &flags)) |
| 2645 | return NULL; |
| 2646 | |
| 2647 | CHECK_DB_NOT_CLOSED(self); |
| 2648 | if (outFileName) |
| 2649 | outFile = fopen(outFileName, "w"); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 2650 | /* XXX(nnorwitz): it should probably be an exception if outFile |
| 2651 | can't be opened. */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2652 | |
| 2653 | MYDB_BEGIN_ALLOW_THREADS; |
| 2654 | err = self->db->verify(self->db, fileName, dbName, outFile, flags); |
| 2655 | MYDB_END_ALLOW_THREADS; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 2656 | if (outFile) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2657 | fclose(outFile); |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 2658 | |
| 2659 | /* DB.verify acts as a DB handle destructor (like close); this was |
| 2660 | * documented in BerkeleyDB 4.2 but had the undocumented effect |
| 2661 | * of not being safe in prior versions while still requiring an explicit |
| 2662 | * DB.close call afterwards. Lets call close for the user to emulate |
| 2663 | * the safe 4.2 behaviour. */ |
| 2664 | #if (DBVER <= 41) |
| 2665 | self->db->close(self->db, 0); |
| 2666 | #endif |
| 2667 | self->db = NULL; |
| 2668 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2669 | RETURN_IF_ERR(); |
| 2670 | RETURN_NONE(); |
| 2671 | } |
| 2672 | |
| 2673 | |
| 2674 | static PyObject* |
| 2675 | DB_set_get_returns_none(DBObject* self, PyObject* args) |
| 2676 | { |
| 2677 | int flags=0; |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 2678 | int oldValue=0; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2679 | |
| 2680 | if (!PyArg_ParseTuple(args,"i:set_get_returns_none", &flags)) |
| 2681 | return NULL; |
| 2682 | CHECK_DB_NOT_CLOSED(self); |
| 2683 | |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 2684 | if (self->moduleFlags.getReturnsNone) |
| 2685 | ++oldValue; |
| 2686 | if (self->moduleFlags.cursorSetReturnsNone) |
| 2687 | ++oldValue; |
| 2688 | self->moduleFlags.getReturnsNone = (flags >= 1); |
| 2689 | self->moduleFlags.cursorSetReturnsNone = (flags >= 2); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2690 | return PyInt_FromLong(oldValue); |
| 2691 | } |
| 2692 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2693 | #if (DBVER >= 41) |
| 2694 | static PyObject* |
| 2695 | DB_set_encrypt(DBObject* self, PyObject* args, PyObject* kwargs) |
| 2696 | { |
| 2697 | int err; |
| 2698 | u_int32_t flags=0; |
| 2699 | char *passwd = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2700 | static char* kwnames[] = { "passwd", "flags", NULL }; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2701 | |
| 2702 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|i:set_encrypt", kwnames, |
| 2703 | &passwd, &flags)) { |
| 2704 | return NULL; |
| 2705 | } |
| 2706 | |
| 2707 | MYDB_BEGIN_ALLOW_THREADS; |
| 2708 | err = self->db->set_encrypt(self->db, passwd, flags); |
| 2709 | MYDB_END_ALLOW_THREADS; |
| 2710 | |
| 2711 | RETURN_IF_ERR(); |
| 2712 | RETURN_NONE(); |
| 2713 | } |
| 2714 | #endif /* DBVER >= 41 */ |
| 2715 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2716 | |
| 2717 | /*-------------------------------------------------------------- */ |
| 2718 | /* Mapping and Dictionary-like access routines */ |
| 2719 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2720 | Py_ssize_t DB_length(PyObject* _self) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2721 | { |
| 2722 | int err; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2723 | Py_ssize_t size = 0; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2724 | int flags = 0; |
| 2725 | void* sp; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2726 | DBObject* self = (DBObject*)_self; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2727 | |
| 2728 | if (self->db == NULL) { |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2729 | PyObject *t = Py_BuildValue("(is)", 0, "DB object has been closed"); |
| 2730 | PyErr_SetObject(DBError, t); |
| 2731 | Py_DECREF(t); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2732 | return -1; |
| 2733 | } |
| 2734 | |
| 2735 | if (self->haveStat) { /* Has the stat function been called recently? If |
| 2736 | so, we can use the cached value. */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2737 | flags = DB_FAST_STAT; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2738 | } |
| 2739 | |
| 2740 | MYDB_BEGIN_ALLOW_THREADS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2741 | redo_stat_for_length: |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 2742 | #if (DBVER >= 43) |
| 2743 | err = self->db->stat(self->db, /*txnid*/ NULL, &sp, flags); |
| 2744 | #elif (DBVER >= 33) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2745 | err = self->db->stat(self->db, &sp, flags); |
| 2746 | #else |
| 2747 | err = self->db->stat(self->db, &sp, NULL, flags); |
| 2748 | #endif |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2749 | |
| 2750 | /* All the stat structures have matching fields upto the ndata field, |
| 2751 | so we can use any of them for the type cast */ |
| 2752 | size = ((DB_BTREE_STAT*)sp)->bt_ndata; |
| 2753 | |
| 2754 | /* A size of 0 could mean that BerkeleyDB no longer had the stat values cached. |
| 2755 | * redo a full stat to make sure. |
| 2756 | * Fixes SF python bug 1493322, pybsddb bug 1184012 |
| 2757 | */ |
| 2758 | if (size == 0 && (flags & DB_FAST_STAT)) { |
| 2759 | flags = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2760 | if (!err) |
| 2761 | free(sp); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2762 | goto redo_stat_for_length; |
| 2763 | } |
| 2764 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2765 | MYDB_END_ALLOW_THREADS; |
| 2766 | |
| 2767 | if (err) |
| 2768 | return -1; |
| 2769 | |
| 2770 | self->haveStat = 1; |
| 2771 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2772 | free(sp); |
| 2773 | return size; |
| 2774 | } |
| 2775 | |
| 2776 | |
| 2777 | PyObject* DB_subscript(DBObject* self, PyObject* keyobj) |
| 2778 | { |
| 2779 | int err; |
| 2780 | PyObject* retval; |
| 2781 | DBT key; |
| 2782 | DBT data; |
| 2783 | |
| 2784 | CHECK_DB_NOT_CLOSED(self); |
| 2785 | if (!make_key_dbt(self, keyobj, &key, NULL)) |
| 2786 | return NULL; |
| 2787 | |
| 2788 | CLEAR_DBT(data); |
| 2789 | if (CHECK_DBFLAG(self, DB_THREAD)) { |
| 2790 | /* Tell BerkeleyDB to malloc the return value (thread safe) */ |
| 2791 | data.flags = DB_DBT_MALLOC; |
| 2792 | } |
| 2793 | MYDB_BEGIN_ALLOW_THREADS; |
| 2794 | err = self->db->get(self->db, NULL, &key, &data, 0); |
| 2795 | MYDB_END_ALLOW_THREADS; |
| 2796 | if (err == DB_NOTFOUND || err == DB_KEYEMPTY) { |
| 2797 | PyErr_SetObject(PyExc_KeyError, keyobj); |
| 2798 | retval = NULL; |
| 2799 | } |
| 2800 | else if (makeDBError(err)) { |
| 2801 | retval = NULL; |
| 2802 | } |
| 2803 | else { |
| 2804 | retval = PyString_FromStringAndSize((char*)data.data, data.size); |
| 2805 | FREE_DBT(data); |
| 2806 | } |
| 2807 | |
| 2808 | FREE_DBT(key); |
| 2809 | return retval; |
| 2810 | } |
| 2811 | |
| 2812 | |
| 2813 | static int |
| 2814 | DB_ass_sub(DBObject* self, PyObject* keyobj, PyObject* dataobj) |
| 2815 | { |
| 2816 | DBT key, data; |
| 2817 | int retval; |
| 2818 | int flags = 0; |
| 2819 | |
| 2820 | if (self->db == NULL) { |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2821 | PyObject *t = Py_BuildValue("(is)", 0, "DB object has been closed"); |
| 2822 | PyErr_SetObject(DBError, t); |
| 2823 | Py_DECREF(t); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2824 | return -1; |
| 2825 | } |
| 2826 | |
| 2827 | if (!make_key_dbt(self, keyobj, &key, NULL)) |
| 2828 | return -1; |
| 2829 | |
| 2830 | if (dataobj != NULL) { |
| 2831 | if (!make_dbt(dataobj, &data)) |
| 2832 | retval = -1; |
| 2833 | else { |
| 2834 | if (self->setflags & (DB_DUP|DB_DUPSORT)) |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2835 | /* dictionaries shouldn't have duplicate keys */ |
| 2836 | flags = DB_NOOVERWRITE; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2837 | retval = _DB_put(self, NULL, &key, &data, flags); |
| 2838 | |
| 2839 | if ((retval == -1) && (self->setflags & (DB_DUP|DB_DUPSORT))) { |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2840 | /* try deleting any old record that matches and then PUT it |
| 2841 | * again... */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2842 | _DB_delete(self, NULL, &key, 0); |
| 2843 | PyErr_Clear(); |
| 2844 | retval = _DB_put(self, NULL, &key, &data, flags); |
| 2845 | } |
| 2846 | } |
| 2847 | } |
| 2848 | else { |
| 2849 | /* dataobj == NULL, so delete the key */ |
| 2850 | retval = _DB_delete(self, NULL, &key, 0); |
| 2851 | } |
| 2852 | FREE_DBT(key); |
| 2853 | return retval; |
| 2854 | } |
| 2855 | |
| 2856 | |
| 2857 | static PyObject* |
| 2858 | DB_has_key(DBObject* self, PyObject* args) |
| 2859 | { |
| 2860 | int err; |
| 2861 | PyObject* keyobj; |
| 2862 | DBT key, data; |
| 2863 | PyObject* txnobj = NULL; |
| 2864 | DB_TXN *txn = NULL; |
| 2865 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2866 | if (!PyArg_ParseTuple(args,"O|O:has_key", &keyobj, &txnobj)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2867 | return NULL; |
| 2868 | CHECK_DB_NOT_CLOSED(self); |
| 2869 | if (!make_key_dbt(self, keyobj, &key, NULL)) |
| 2870 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 2871 | if (!checkTxnObj(txnobj, &txn)) { |
| 2872 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2873 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 2874 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2875 | |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 2876 | /* 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] | 2877 | it has a record but can't allocate a buffer for the data. This saves |
| 2878 | having to deal with data we won't be using. |
| 2879 | */ |
| 2880 | CLEAR_DBT(data); |
| 2881 | data.flags = DB_DBT_USERMEM; |
| 2882 | |
| 2883 | MYDB_BEGIN_ALLOW_THREADS; |
Gregory P. Smith | 0c65771 | 2004-03-16 06:56:58 +0000 | [diff] [blame] | 2884 | err = self->db->get(self->db, txn, &key, &data, 0); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2885 | MYDB_END_ALLOW_THREADS; |
| 2886 | FREE_DBT(key); |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 2887 | |
| 2888 | if (err == DB_BUFFER_SMALL || err == 0) { |
| 2889 | return PyInt_FromLong(1); |
| 2890 | } else if (err == DB_NOTFOUND || err == DB_KEYEMPTY) { |
| 2891 | return PyInt_FromLong(0); |
| 2892 | } |
| 2893 | |
| 2894 | makeDBError(err); |
| 2895 | return NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2896 | } |
| 2897 | |
| 2898 | |
| 2899 | #define _KEYS_LIST 1 |
| 2900 | #define _VALUES_LIST 2 |
| 2901 | #define _ITEMS_LIST 3 |
| 2902 | |
| 2903 | static PyObject* |
| 2904 | _DB_make_list(DBObject* self, DB_TXN* txn, int type) |
| 2905 | { |
| 2906 | int err, dbtype; |
| 2907 | DBT key; |
| 2908 | DBT data; |
| 2909 | DBC *cursor; |
| 2910 | PyObject* list; |
| 2911 | PyObject* item = NULL; |
| 2912 | |
| 2913 | CHECK_DB_NOT_CLOSED(self); |
| 2914 | CLEAR_DBT(key); |
| 2915 | CLEAR_DBT(data); |
| 2916 | |
| 2917 | dbtype = _DB_get_type(self); |
| 2918 | if (dbtype == -1) |
| 2919 | return NULL; |
| 2920 | |
| 2921 | list = PyList_New(0); |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2922 | if (list == NULL) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2923 | return NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2924 | |
| 2925 | /* get a cursor */ |
| 2926 | MYDB_BEGIN_ALLOW_THREADS; |
Gregory P. Smith | 442c9fc | 2004-09-04 01:36:59 +0000 | [diff] [blame] | 2927 | err = self->db->cursor(self->db, txn, &cursor, 0); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2928 | MYDB_END_ALLOW_THREADS; |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2929 | if (makeDBError(err)) { |
| 2930 | Py_DECREF(list); |
| 2931 | return NULL; |
| 2932 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2933 | |
| 2934 | if (CHECK_DBFLAG(self, DB_THREAD)) { |
| 2935 | key.flags = DB_DBT_REALLOC; |
| 2936 | data.flags = DB_DBT_REALLOC; |
| 2937 | } |
| 2938 | |
| 2939 | while (1) { /* use the cursor to traverse the DB, collecting items */ |
| 2940 | MYDB_BEGIN_ALLOW_THREADS; |
| 2941 | err = cursor->c_get(cursor, &key, &data, DB_NEXT); |
| 2942 | MYDB_END_ALLOW_THREADS; |
| 2943 | |
| 2944 | if (err) { |
| 2945 | /* for any error, break out of the loop */ |
| 2946 | break; |
| 2947 | } |
| 2948 | |
| 2949 | switch (type) { |
| 2950 | case _KEYS_LIST: |
| 2951 | switch(dbtype) { |
| 2952 | case DB_BTREE: |
| 2953 | case DB_HASH: |
| 2954 | default: |
| 2955 | item = PyString_FromStringAndSize((char*)key.data, key.size); |
| 2956 | break; |
| 2957 | case DB_RECNO: |
| 2958 | case DB_QUEUE: |
| 2959 | item = PyInt_FromLong(*((db_recno_t*)key.data)); |
| 2960 | break; |
| 2961 | } |
| 2962 | break; |
| 2963 | |
| 2964 | case _VALUES_LIST: |
| 2965 | item = PyString_FromStringAndSize((char*)data.data, data.size); |
| 2966 | break; |
| 2967 | |
| 2968 | case _ITEMS_LIST: |
| 2969 | switch(dbtype) { |
| 2970 | case DB_BTREE: |
| 2971 | case DB_HASH: |
| 2972 | default: |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2973 | item = Py_BuildValue("s#s#", key.data, key.size, data.data, |
| 2974 | data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2975 | break; |
| 2976 | case DB_RECNO: |
| 2977 | case DB_QUEUE: |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 2978 | item = Py_BuildValue("is#", *((db_recno_t*)key.data), |
| 2979 | data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2980 | break; |
| 2981 | } |
| 2982 | break; |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 2983 | default: |
| 2984 | PyErr_Format(PyExc_ValueError, "Unknown key type 0x%x", type); |
| 2985 | item = NULL; |
| 2986 | break; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2987 | } |
| 2988 | if (item == NULL) { |
| 2989 | Py_DECREF(list); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2990 | list = NULL; |
| 2991 | goto done; |
| 2992 | } |
| 2993 | PyList_Append(list, item); |
| 2994 | Py_DECREF(item); |
| 2995 | } |
| 2996 | |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 2997 | /* DB_NOTFOUND || DB_KEYEMPTY is okay, it means we got to the end */ |
| 2998 | if (err != DB_NOTFOUND && err != DB_KEYEMPTY && makeDBError(err)) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 2999 | Py_DECREF(list); |
| 3000 | list = NULL; |
| 3001 | } |
| 3002 | |
| 3003 | done: |
| 3004 | FREE_DBT(key); |
| 3005 | FREE_DBT(data); |
| 3006 | MYDB_BEGIN_ALLOW_THREADS; |
| 3007 | cursor->c_close(cursor); |
| 3008 | MYDB_END_ALLOW_THREADS; |
| 3009 | return list; |
| 3010 | } |
| 3011 | |
| 3012 | |
| 3013 | static PyObject* |
| 3014 | DB_keys(DBObject* self, PyObject* args) |
| 3015 | { |
| 3016 | PyObject* txnobj = NULL; |
| 3017 | DB_TXN *txn = NULL; |
| 3018 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 3019 | if (!PyArg_UnpackTuple(args, "keys", 0, 1, &txnobj)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3020 | return NULL; |
| 3021 | if (!checkTxnObj(txnobj, &txn)) |
| 3022 | return NULL; |
| 3023 | return _DB_make_list(self, txn, _KEYS_LIST); |
| 3024 | } |
| 3025 | |
| 3026 | |
| 3027 | static PyObject* |
| 3028 | DB_items(DBObject* self, PyObject* args) |
| 3029 | { |
| 3030 | PyObject* txnobj = NULL; |
| 3031 | DB_TXN *txn = NULL; |
| 3032 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 3033 | if (!PyArg_UnpackTuple(args, "items", 0, 1, &txnobj)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3034 | return NULL; |
| 3035 | if (!checkTxnObj(txnobj, &txn)) |
| 3036 | return NULL; |
| 3037 | return _DB_make_list(self, txn, _ITEMS_LIST); |
| 3038 | } |
| 3039 | |
| 3040 | |
| 3041 | static PyObject* |
| 3042 | DB_values(DBObject* self, PyObject* args) |
| 3043 | { |
| 3044 | PyObject* txnobj = NULL; |
| 3045 | DB_TXN *txn = NULL; |
| 3046 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 3047 | if (!PyArg_UnpackTuple(args, "values", 0, 1, &txnobj)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3048 | return NULL; |
| 3049 | if (!checkTxnObj(txnobj, &txn)) |
| 3050 | return NULL; |
| 3051 | return _DB_make_list(self, txn, _VALUES_LIST); |
| 3052 | } |
| 3053 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3054 | /* --------------------------------------------------------------------- */ |
| 3055 | /* DBCursor methods */ |
| 3056 | |
| 3057 | |
| 3058 | static PyObject* |
| 3059 | DBC_close(DBCursorObject* self, PyObject* args) |
| 3060 | { |
| 3061 | int err = 0; |
| 3062 | |
| 3063 | if (!PyArg_ParseTuple(args, ":close")) |
| 3064 | return NULL; |
| 3065 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3066 | if (self->dbc != NULL) { |
| 3067 | MYDB_BEGIN_ALLOW_THREADS; |
| 3068 | err = self->dbc->c_close(self->dbc); |
| 3069 | self->dbc = NULL; |
| 3070 | MYDB_END_ALLOW_THREADS; |
| 3071 | } |
| 3072 | RETURN_IF_ERR(); |
| 3073 | RETURN_NONE(); |
| 3074 | } |
| 3075 | |
| 3076 | |
| 3077 | static PyObject* |
| 3078 | DBC_count(DBCursorObject* self, PyObject* args) |
| 3079 | { |
| 3080 | int err = 0; |
| 3081 | db_recno_t count; |
| 3082 | int flags = 0; |
| 3083 | |
| 3084 | if (!PyArg_ParseTuple(args, "|i:count", &flags)) |
| 3085 | return NULL; |
| 3086 | |
| 3087 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3088 | |
| 3089 | MYDB_BEGIN_ALLOW_THREADS; |
| 3090 | err = self->dbc->c_count(self->dbc, &count, flags); |
| 3091 | MYDB_END_ALLOW_THREADS; |
| 3092 | RETURN_IF_ERR(); |
| 3093 | |
| 3094 | return PyInt_FromLong(count); |
| 3095 | } |
| 3096 | |
| 3097 | |
| 3098 | static PyObject* |
| 3099 | DBC_current(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3100 | { |
| 3101 | return _DBCursor_get(self,DB_CURRENT,args,kwargs,"|iii:current"); |
| 3102 | } |
| 3103 | |
| 3104 | |
| 3105 | static PyObject* |
| 3106 | DBC_delete(DBCursorObject* self, PyObject* args) |
| 3107 | { |
| 3108 | int err, flags=0; |
| 3109 | |
| 3110 | if (!PyArg_ParseTuple(args, "|i:delete", &flags)) |
| 3111 | return NULL; |
| 3112 | |
| 3113 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3114 | |
| 3115 | MYDB_BEGIN_ALLOW_THREADS; |
| 3116 | err = self->dbc->c_del(self->dbc, flags); |
| 3117 | MYDB_END_ALLOW_THREADS; |
| 3118 | RETURN_IF_ERR(); |
| 3119 | |
| 3120 | self->mydb->haveStat = 0; |
| 3121 | RETURN_NONE(); |
| 3122 | } |
| 3123 | |
| 3124 | |
| 3125 | static PyObject* |
| 3126 | DBC_dup(DBCursorObject* self, PyObject* args) |
| 3127 | { |
| 3128 | int err, flags =0; |
| 3129 | DBC* dbc = NULL; |
| 3130 | |
| 3131 | if (!PyArg_ParseTuple(args, "|i:dup", &flags)) |
| 3132 | return NULL; |
| 3133 | |
| 3134 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3135 | |
| 3136 | MYDB_BEGIN_ALLOW_THREADS; |
| 3137 | err = self->dbc->c_dup(self->dbc, &dbc, flags); |
| 3138 | MYDB_END_ALLOW_THREADS; |
| 3139 | RETURN_IF_ERR(); |
| 3140 | |
| 3141 | return (PyObject*) newDBCursorObject(dbc, self->mydb); |
| 3142 | } |
| 3143 | |
| 3144 | static PyObject* |
| 3145 | DBC_first(DBCursorObject* self, PyObject* args, PyObject* kwargs) |
| 3146 | { |
| 3147 | return _DBCursor_get(self,DB_FIRST,args,kwargs,"|iii:first"); |
| 3148 | } |
| 3149 | |
| 3150 | |
| 3151 | static PyObject* |
| 3152 | DBC_get(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3153 | { |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 3154 | int err, flags=0; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3155 | PyObject* keyobj = NULL; |
| 3156 | PyObject* dataobj = NULL; |
| 3157 | PyObject* retval = NULL; |
| 3158 | int dlen = -1; |
| 3159 | int doff = -1; |
| 3160 | DBT key, data; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3161 | static char* kwnames[] = { "key","data", "flags", "dlen", "doff", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 3162 | NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3163 | |
| 3164 | CLEAR_DBT(key); |
| 3165 | CLEAR_DBT(data); |
| 3166 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|ii:get", &kwnames[2], |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 3167 | &flags, &dlen, &doff)) |
| 3168 | { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3169 | PyErr_Clear(); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 3170 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi|ii:get", |
| 3171 | &kwnames[1], |
| 3172 | &keyobj, &flags, &dlen, &doff)) |
| 3173 | { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3174 | PyErr_Clear(); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 3175 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OOi|ii:get", |
| 3176 | kwnames, &keyobj, &dataobj, |
| 3177 | &flags, &dlen, &doff)) |
| 3178 | { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3179 | return NULL; |
| 3180 | } |
| 3181 | } |
| 3182 | } |
| 3183 | |
| 3184 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3185 | |
| 3186 | if (keyobj && !make_key_dbt(self->mydb, keyobj, &key, NULL)) |
| 3187 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3188 | if ( (dataobj && !make_dbt(dataobj, &data)) || |
| 3189 | (!add_partial_dbt(&data, dlen, doff)) ) |
| 3190 | { |
| 3191 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3192 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3193 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3194 | |
| 3195 | if (CHECK_DBFLAG(self->mydb, DB_THREAD)) { |
| 3196 | data.flags = DB_DBT_MALLOC; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3197 | if (!(key.flags & DB_DBT_REALLOC)) { |
| 3198 | key.flags |= DB_DBT_MALLOC; |
| 3199 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3200 | } |
| 3201 | |
| 3202 | MYDB_BEGIN_ALLOW_THREADS; |
| 3203 | err = self->dbc->c_get(self->dbc, &key, &data, flags); |
| 3204 | MYDB_END_ALLOW_THREADS; |
| 3205 | |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 3206 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 3207 | && self->mydb->moduleFlags.getReturnsNone) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3208 | Py_INCREF(Py_None); |
| 3209 | retval = Py_None; |
| 3210 | } |
| 3211 | else if (makeDBError(err)) { |
| 3212 | retval = NULL; |
| 3213 | } |
| 3214 | else { |
| 3215 | switch (_DB_get_type(self->mydb)) { |
| 3216 | case -1: |
| 3217 | retval = NULL; |
| 3218 | break; |
| 3219 | case DB_BTREE: |
| 3220 | case DB_HASH: |
| 3221 | default: |
| 3222 | retval = Py_BuildValue("s#s#", key.data, key.size, |
| 3223 | data.data, data.size); |
| 3224 | break; |
| 3225 | case DB_RECNO: |
| 3226 | case DB_QUEUE: |
| 3227 | retval = Py_BuildValue("is#", *((db_recno_t*)key.data), |
| 3228 | data.data, data.size); |
| 3229 | break; |
| 3230 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3231 | FREE_DBT(data); |
| 3232 | } |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3233 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3234 | return retval; |
| 3235 | } |
| 3236 | |
Gregory P. Smith | 8a6a59c | 2004-12-16 09:47:28 +0000 | [diff] [blame] | 3237 | #if (DBVER >= 33) |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3238 | static PyObject* |
| 3239 | DBC_pget(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3240 | { |
| 3241 | int err, flags=0; |
| 3242 | PyObject* keyobj = NULL; |
| 3243 | PyObject* dataobj = NULL; |
| 3244 | PyObject* retval = NULL; |
| 3245 | int dlen = -1; |
| 3246 | int doff = -1; |
| 3247 | DBT key, pkey, data; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3248 | static char* kwnames_keyOnly[] = { "key", "flags", "dlen", "doff", NULL }; |
| 3249 | static char* kwnames[] = { "key", "data", "flags", "dlen", "doff", NULL }; |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3250 | |
| 3251 | CLEAR_DBT(key); |
| 3252 | CLEAR_DBT(data); |
| 3253 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|ii:pget", &kwnames[2], |
| 3254 | &flags, &dlen, &doff)) |
| 3255 | { |
| 3256 | PyErr_Clear(); |
| 3257 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi|ii:pget", |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3258 | kwnames_keyOnly, |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3259 | &keyobj, &flags, &dlen, &doff)) |
| 3260 | { |
| 3261 | PyErr_Clear(); |
| 3262 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OOi|ii:pget", |
| 3263 | kwnames, &keyobj, &dataobj, |
| 3264 | &flags, &dlen, &doff)) |
| 3265 | { |
| 3266 | return NULL; |
| 3267 | } |
| 3268 | } |
| 3269 | } |
| 3270 | |
| 3271 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3272 | |
| 3273 | if (keyobj && !make_key_dbt(self->mydb, keyobj, &key, NULL)) |
| 3274 | return NULL; |
| 3275 | if ( (dataobj && !make_dbt(dataobj, &data)) || |
| 3276 | (!add_partial_dbt(&data, dlen, doff)) ) { |
| 3277 | FREE_DBT(key); |
| 3278 | return NULL; |
| 3279 | } |
| 3280 | |
| 3281 | if (CHECK_DBFLAG(self->mydb, DB_THREAD)) { |
| 3282 | data.flags = DB_DBT_MALLOC; |
| 3283 | if (!(key.flags & DB_DBT_REALLOC)) { |
| 3284 | key.flags |= DB_DBT_MALLOC; |
| 3285 | } |
| 3286 | } |
| 3287 | |
| 3288 | CLEAR_DBT(pkey); |
| 3289 | pkey.flags = DB_DBT_MALLOC; |
| 3290 | |
| 3291 | MYDB_BEGIN_ALLOW_THREADS; |
| 3292 | err = self->dbc->c_pget(self->dbc, &key, &pkey, &data, flags); |
| 3293 | MYDB_END_ALLOW_THREADS; |
| 3294 | |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 3295 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 3296 | && self->mydb->moduleFlags.getReturnsNone) { |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3297 | Py_INCREF(Py_None); |
| 3298 | retval = Py_None; |
| 3299 | } |
| 3300 | else if (makeDBError(err)) { |
| 3301 | retval = NULL; |
| 3302 | } |
| 3303 | else { |
| 3304 | PyObject *pkeyObj; |
| 3305 | PyObject *dataObj; |
| 3306 | dataObj = PyString_FromStringAndSize(data.data, data.size); |
| 3307 | |
| 3308 | if (self->mydb->primaryDBType == DB_RECNO || |
| 3309 | self->mydb->primaryDBType == DB_QUEUE) |
Neal Norwitz | 40c6b47 | 2006-01-05 05:43:35 +0000 | [diff] [blame] | 3310 | pkeyObj = PyInt_FromLong(*(int *)pkey.data); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3311 | else |
| 3312 | pkeyObj = PyString_FromStringAndSize(pkey.data, pkey.size); |
| 3313 | |
Gregory P. Smith | 4e414d8 | 2006-01-24 19:55:02 +0000 | [diff] [blame] | 3314 | if (key.data && key.size) /* return key, pkey and data */ |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3315 | { |
| 3316 | PyObject *keyObj; |
| 3317 | int type = _DB_get_type(self->mydb); |
| 3318 | if (type == DB_RECNO || type == DB_QUEUE) |
Neal Norwitz | 40c6b47 | 2006-01-05 05:43:35 +0000 | [diff] [blame] | 3319 | keyObj = PyInt_FromLong(*(int *)key.data); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3320 | else |
| 3321 | keyObj = PyString_FromStringAndSize(key.data, key.size); |
Gregory P. Smith | fd049a6 | 2006-01-30 00:22:08 +0000 | [diff] [blame] | 3322 | #if (PY_VERSION_HEX >= 0x02040000) |
Gregory P. Smith | 4e414d8 | 2006-01-24 19:55:02 +0000 | [diff] [blame] | 3323 | retval = PyTuple_Pack(3, keyObj, pkeyObj, dataObj); |
Gregory P. Smith | fd049a6 | 2006-01-30 00:22:08 +0000 | [diff] [blame] | 3324 | #else |
| 3325 | retval = Py_BuildValue("OOO", keyObj, pkeyObj, dataObj); |
| 3326 | #endif |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 3327 | Py_DECREF(keyObj); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3328 | FREE_DBT(key); |
| 3329 | } |
| 3330 | else /* return just the pkey and data */ |
| 3331 | { |
Gregory P. Smith | fd049a6 | 2006-01-30 00:22:08 +0000 | [diff] [blame] | 3332 | #if (PY_VERSION_HEX >= 0x02040000) |
Gregory P. Smith | 4e414d8 | 2006-01-24 19:55:02 +0000 | [diff] [blame] | 3333 | retval = PyTuple_Pack(2, pkeyObj, dataObj); |
Gregory P. Smith | fd049a6 | 2006-01-30 00:22:08 +0000 | [diff] [blame] | 3334 | #else |
| 3335 | retval = Py_BuildValue("OO", pkeyObj, dataObj); |
| 3336 | #endif |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3337 | } |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 3338 | Py_DECREF(dataObj); |
| 3339 | Py_DECREF(pkeyObj); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3340 | FREE_DBT(pkey); |
| 3341 | FREE_DBT(data); |
| 3342 | } |
| 3343 | /* the only time REALLOC should be set is if we used an integer |
| 3344 | * key that make_key_dbt malloc'd for us. always free these. */ |
| 3345 | if (key.flags & DB_DBT_REALLOC) { |
| 3346 | FREE_DBT(key); |
| 3347 | } |
| 3348 | return retval; |
| 3349 | } |
Gregory P. Smith | 8a6a59c | 2004-12-16 09:47:28 +0000 | [diff] [blame] | 3350 | #endif |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3351 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3352 | |
| 3353 | static PyObject* |
| 3354 | DBC_get_recno(DBCursorObject* self, PyObject* args) |
| 3355 | { |
| 3356 | int err; |
| 3357 | db_recno_t recno; |
| 3358 | DBT key; |
| 3359 | DBT data; |
| 3360 | |
| 3361 | if (!PyArg_ParseTuple(args, ":get_recno")) |
| 3362 | return NULL; |
| 3363 | |
| 3364 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3365 | |
| 3366 | CLEAR_DBT(key); |
| 3367 | CLEAR_DBT(data); |
| 3368 | if (CHECK_DBFLAG(self->mydb, DB_THREAD)) { |
| 3369 | /* Tell BerkeleyDB to malloc the return value (thread safe) */ |
| 3370 | data.flags = DB_DBT_MALLOC; |
| 3371 | key.flags = DB_DBT_MALLOC; |
| 3372 | } |
| 3373 | |
| 3374 | MYDB_BEGIN_ALLOW_THREADS; |
| 3375 | err = self->dbc->c_get(self->dbc, &key, &data, DB_GET_RECNO); |
| 3376 | MYDB_END_ALLOW_THREADS; |
| 3377 | RETURN_IF_ERR(); |
| 3378 | |
| 3379 | recno = *((db_recno_t*)data.data); |
| 3380 | FREE_DBT(key); |
| 3381 | FREE_DBT(data); |
| 3382 | return PyInt_FromLong(recno); |
| 3383 | } |
| 3384 | |
| 3385 | |
| 3386 | static PyObject* |
| 3387 | DBC_last(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3388 | { |
| 3389 | return _DBCursor_get(self,DB_LAST,args,kwargs,"|iii:last"); |
| 3390 | } |
| 3391 | |
| 3392 | |
| 3393 | static PyObject* |
| 3394 | DBC_next(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3395 | { |
| 3396 | return _DBCursor_get(self,DB_NEXT,args,kwargs,"|iii:next"); |
| 3397 | } |
| 3398 | |
| 3399 | |
| 3400 | static PyObject* |
| 3401 | DBC_prev(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3402 | { |
| 3403 | return _DBCursor_get(self,DB_PREV,args,kwargs,"|iii:prev"); |
| 3404 | } |
| 3405 | |
| 3406 | |
| 3407 | static PyObject* |
| 3408 | DBC_put(DBCursorObject* self, PyObject* args, PyObject* kwargs) |
| 3409 | { |
| 3410 | int err, flags = 0; |
| 3411 | PyObject* keyobj, *dataobj; |
| 3412 | DBT key, data; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3413 | static char* kwnames[] = { "key", "data", "flags", "dlen", "doff", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 3414 | NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3415 | int dlen = -1; |
| 3416 | int doff = -1; |
| 3417 | |
| 3418 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|iii:put", kwnames, |
| 3419 | &keyobj, &dataobj, &flags, &dlen, &doff)) |
| 3420 | return NULL; |
| 3421 | |
| 3422 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3423 | |
| 3424 | if (!make_key_dbt(self->mydb, keyobj, &key, NULL)) |
| 3425 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3426 | if (!make_dbt(dataobj, &data) || |
| 3427 | !add_partial_dbt(&data, dlen, doff) ) |
| 3428 | { |
| 3429 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3430 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3431 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3432 | |
| 3433 | MYDB_BEGIN_ALLOW_THREADS; |
| 3434 | err = self->dbc->c_put(self->dbc, &key, &data, flags); |
| 3435 | MYDB_END_ALLOW_THREADS; |
| 3436 | FREE_DBT(key); |
| 3437 | RETURN_IF_ERR(); |
| 3438 | self->mydb->haveStat = 0; |
| 3439 | RETURN_NONE(); |
| 3440 | } |
| 3441 | |
| 3442 | |
| 3443 | static PyObject* |
| 3444 | DBC_set(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3445 | { |
| 3446 | int err, flags = 0; |
| 3447 | DBT key, data; |
| 3448 | PyObject* retval, *keyobj; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3449 | static char* kwnames[] = { "key", "flags", "dlen", "doff", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3450 | int dlen = -1; |
| 3451 | int doff = -1; |
| 3452 | |
| 3453 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|iii:set", kwnames, |
| 3454 | &keyobj, &flags, &dlen, &doff)) |
| 3455 | return NULL; |
| 3456 | |
| 3457 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3458 | |
| 3459 | if (!make_key_dbt(self->mydb, keyobj, &key, NULL)) |
| 3460 | return NULL; |
| 3461 | |
| 3462 | CLEAR_DBT(data); |
| 3463 | if (CHECK_DBFLAG(self->mydb, DB_THREAD)) { |
| 3464 | /* Tell BerkeleyDB to malloc the return value (thread safe) */ |
| 3465 | data.flags = DB_DBT_MALLOC; |
| 3466 | } |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3467 | if (!add_partial_dbt(&data, dlen, doff)) { |
| 3468 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3469 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3470 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3471 | |
| 3472 | MYDB_BEGIN_ALLOW_THREADS; |
| 3473 | err = self->dbc->c_get(self->dbc, &key, &data, flags|DB_SET); |
| 3474 | MYDB_END_ALLOW_THREADS; |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 3475 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 3476 | && self->mydb->moduleFlags.cursorSetReturnsNone) { |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3477 | Py_INCREF(Py_None); |
| 3478 | retval = Py_None; |
| 3479 | } |
| 3480 | else if (makeDBError(err)) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3481 | retval = NULL; |
| 3482 | } |
| 3483 | else { |
| 3484 | switch (_DB_get_type(self->mydb)) { |
| 3485 | case -1: |
| 3486 | retval = NULL; |
| 3487 | break; |
| 3488 | case DB_BTREE: |
| 3489 | case DB_HASH: |
| 3490 | default: |
| 3491 | retval = Py_BuildValue("s#s#", key.data, key.size, |
| 3492 | data.data, data.size); |
| 3493 | break; |
| 3494 | case DB_RECNO: |
| 3495 | case DB_QUEUE: |
| 3496 | retval = Py_BuildValue("is#", *((db_recno_t*)key.data), |
| 3497 | data.data, data.size); |
| 3498 | break; |
| 3499 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3500 | FREE_DBT(data); |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3501 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3502 | } |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 3503 | /* the only time REALLOC should be set is if we used an integer |
| 3504 | * key that make_key_dbt malloc'd for us. always free these. */ |
| 3505 | if (key.flags & DB_DBT_REALLOC) { |
| 3506 | FREE_DBT(key); |
| 3507 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3508 | |
| 3509 | return retval; |
| 3510 | } |
| 3511 | |
| 3512 | |
| 3513 | static PyObject* |
| 3514 | DBC_set_range(DBCursorObject* self, PyObject* args, PyObject* kwargs) |
| 3515 | { |
| 3516 | int err, flags = 0; |
| 3517 | DBT key, data; |
| 3518 | PyObject* retval, *keyobj; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3519 | static char* kwnames[] = { "key", "flags", "dlen", "doff", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3520 | int dlen = -1; |
| 3521 | int doff = -1; |
| 3522 | |
| 3523 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|iii:set_range", kwnames, |
| 3524 | &keyobj, &flags, &dlen, &doff)) |
| 3525 | return NULL; |
| 3526 | |
| 3527 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3528 | |
| 3529 | if (!make_key_dbt(self->mydb, keyobj, &key, NULL)) |
| 3530 | return NULL; |
| 3531 | |
| 3532 | CLEAR_DBT(data); |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3533 | if (!add_partial_dbt(&data, dlen, doff)) { |
| 3534 | FREE_DBT(key); |
| 3535 | return NULL; |
| 3536 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3537 | if (CHECK_DBFLAG(self->mydb, DB_THREAD)) { |
| 3538 | /* Tell BerkeleyDB to malloc the return value (thread safe) */ |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3539 | data.flags |= DB_DBT_MALLOC; |
| 3540 | /* only BTREE databases will return anything in the key */ |
| 3541 | if (!(key.flags & DB_DBT_REALLOC) && _DB_get_type(self->mydb) == DB_BTREE) { |
| 3542 | key.flags |= DB_DBT_MALLOC; |
| 3543 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3544 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3545 | MYDB_BEGIN_ALLOW_THREADS; |
| 3546 | err = self->dbc->c_get(self->dbc, &key, &data, flags|DB_SET_RANGE); |
| 3547 | MYDB_END_ALLOW_THREADS; |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 3548 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 3549 | && self->mydb->moduleFlags.cursorSetReturnsNone) { |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3550 | Py_INCREF(Py_None); |
| 3551 | retval = Py_None; |
| 3552 | } |
| 3553 | else if (makeDBError(err)) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3554 | retval = NULL; |
| 3555 | } |
| 3556 | else { |
| 3557 | switch (_DB_get_type(self->mydb)) { |
| 3558 | case -1: |
| 3559 | retval = NULL; |
| 3560 | break; |
| 3561 | case DB_BTREE: |
| 3562 | case DB_HASH: |
| 3563 | default: |
| 3564 | retval = Py_BuildValue("s#s#", key.data, key.size, |
| 3565 | data.data, data.size); |
| 3566 | break; |
| 3567 | case DB_RECNO: |
| 3568 | case DB_QUEUE: |
| 3569 | retval = Py_BuildValue("is#", *((db_recno_t*)key.data), |
| 3570 | data.data, data.size); |
| 3571 | break; |
| 3572 | } |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3573 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3574 | FREE_DBT(data); |
| 3575 | } |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3576 | /* 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] | 3577 | * 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] | 3578 | if (key.flags & DB_DBT_REALLOC) { |
| 3579 | FREE_DBT(key); |
| 3580 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3581 | |
| 3582 | return retval; |
| 3583 | } |
| 3584 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3585 | static PyObject* |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3586 | _DBC_get_set_both(DBCursorObject* self, PyObject* keyobj, PyObject* dataobj, |
| 3587 | int flags, unsigned int returnsNone) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3588 | { |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3589 | int err; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3590 | DBT key, data; |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3591 | PyObject* retval; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3592 | |
Gregory P. Smith | 7441e65 | 2003-11-03 21:35:31 +0000 | [diff] [blame] | 3593 | /* the caller did this: CHECK_CURSOR_NOT_CLOSED(self); */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3594 | if (!make_key_dbt(self->mydb, keyobj, &key, NULL)) |
| 3595 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3596 | if (!make_dbt(dataobj, &data)) { |
| 3597 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3598 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3599 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3600 | |
| 3601 | MYDB_BEGIN_ALLOW_THREADS; |
| 3602 | err = self->dbc->c_get(self->dbc, &key, &data, flags|DB_GET_BOTH); |
| 3603 | MYDB_END_ALLOW_THREADS; |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 3604 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) && returnsNone) { |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3605 | Py_INCREF(Py_None); |
| 3606 | retval = Py_None; |
| 3607 | } |
| 3608 | else if (makeDBError(err)) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3609 | retval = NULL; |
| 3610 | } |
| 3611 | else { |
| 3612 | switch (_DB_get_type(self->mydb)) { |
| 3613 | case -1: |
| 3614 | retval = NULL; |
| 3615 | break; |
| 3616 | case DB_BTREE: |
| 3617 | case DB_HASH: |
| 3618 | default: |
| 3619 | retval = Py_BuildValue("s#s#", key.data, key.size, |
| 3620 | data.data, data.size); |
| 3621 | break; |
| 3622 | case DB_RECNO: |
| 3623 | case DB_QUEUE: |
| 3624 | retval = Py_BuildValue("is#", *((db_recno_t*)key.data), |
| 3625 | data.data, data.size); |
| 3626 | break; |
| 3627 | } |
| 3628 | } |
| 3629 | |
| 3630 | FREE_DBT(key); |
| 3631 | return retval; |
| 3632 | } |
| 3633 | |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3634 | static PyObject* |
| 3635 | DBC_get_both(DBCursorObject* self, PyObject* args) |
| 3636 | { |
| 3637 | int flags=0; |
| 3638 | PyObject *keyobj, *dataobj; |
| 3639 | |
| 3640 | if (!PyArg_ParseTuple(args, "OO|i:get_both", &keyobj, &dataobj, &flags)) |
| 3641 | return NULL; |
| 3642 | |
Gregory P. Smith | 7441e65 | 2003-11-03 21:35:31 +0000 | [diff] [blame] | 3643 | /* if the cursor is closed, self->mydb may be invalid */ |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3644 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3645 | |
| 3646 | return _DBC_get_set_both(self, keyobj, dataobj, flags, |
| 3647 | self->mydb->moduleFlags.getReturnsNone); |
| 3648 | } |
| 3649 | |
Gregory P. Smith | be0db8b | 2003-10-01 06:48:51 +0000 | [diff] [blame] | 3650 | /* Return size of entry */ |
| 3651 | static PyObject* |
| 3652 | DBC_get_current_size(DBCursorObject* self, PyObject* args) |
| 3653 | { |
| 3654 | int err, flags=DB_CURRENT; |
| 3655 | PyObject* retval = NULL; |
| 3656 | DBT key, data; |
| 3657 | |
| 3658 | if (!PyArg_ParseTuple(args, ":get_current_size")) |
| 3659 | return NULL; |
| 3660 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3661 | CLEAR_DBT(key); |
| 3662 | CLEAR_DBT(data); |
| 3663 | |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 3664 | /* 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] | 3665 | getting the record size. */ |
| 3666 | data.flags = DB_DBT_USERMEM; |
| 3667 | data.ulen = 0; |
| 3668 | MYDB_BEGIN_ALLOW_THREADS; |
| 3669 | err = self->dbc->c_get(self->dbc, &key, &data, flags); |
| 3670 | MYDB_END_ALLOW_THREADS; |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 3671 | if (err == DB_BUFFER_SMALL || !err) { |
| 3672 | /* DB_BUFFER_SMALL means positive size, !err means zero length value */ |
Gregory P. Smith | be0db8b | 2003-10-01 06:48:51 +0000 | [diff] [blame] | 3673 | retval = PyInt_FromLong((long)data.size); |
| 3674 | err = 0; |
| 3675 | } |
| 3676 | |
| 3677 | FREE_DBT(key); |
| 3678 | FREE_DBT(data); |
| 3679 | RETURN_IF_ERR(); |
| 3680 | return retval; |
| 3681 | } |
| 3682 | |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3683 | static PyObject* |
| 3684 | DBC_set_both(DBCursorObject* self, PyObject* args) |
| 3685 | { |
| 3686 | int flags=0; |
| 3687 | PyObject *keyobj, *dataobj; |
| 3688 | |
| 3689 | if (!PyArg_ParseTuple(args, "OO|i:set_both", &keyobj, &dataobj, &flags)) |
| 3690 | return NULL; |
| 3691 | |
Gregory P. Smith | 7441e65 | 2003-11-03 21:35:31 +0000 | [diff] [blame] | 3692 | /* if the cursor is closed, self->mydb may be invalid */ |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3693 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3694 | |
| 3695 | return _DBC_get_set_both(self, keyobj, dataobj, flags, |
| 3696 | self->mydb->moduleFlags.cursorSetReturnsNone); |
| 3697 | } |
| 3698 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3699 | |
| 3700 | static PyObject* |
| 3701 | DBC_set_recno(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3702 | { |
| 3703 | int err, irecno, flags=0; |
| 3704 | db_recno_t recno; |
| 3705 | DBT key, data; |
| 3706 | PyObject* retval; |
| 3707 | int dlen = -1; |
| 3708 | int doff = -1; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3709 | static char* kwnames[] = { "recno","flags", "dlen", "doff", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3710 | |
| 3711 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|iii:set_recno", kwnames, |
| 3712 | &irecno, &flags, &dlen, &doff)) |
| 3713 | return NULL; |
| 3714 | |
| 3715 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3716 | |
| 3717 | CLEAR_DBT(key); |
| 3718 | recno = (db_recno_t) irecno; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 3719 | /* use allocated space so DB will be able to realloc room for the real |
| 3720 | * key */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3721 | key.data = malloc(sizeof(db_recno_t)); |
| 3722 | if (key.data == NULL) { |
| 3723 | PyErr_SetString(PyExc_MemoryError, "Key memory allocation failed"); |
| 3724 | return NULL; |
| 3725 | } |
| 3726 | key.size = sizeof(db_recno_t); |
| 3727 | key.ulen = key.size; |
| 3728 | memcpy(key.data, &recno, sizeof(db_recno_t)); |
| 3729 | key.flags = DB_DBT_REALLOC; |
| 3730 | |
| 3731 | CLEAR_DBT(data); |
| 3732 | if (CHECK_DBFLAG(self->mydb, DB_THREAD)) { |
| 3733 | /* Tell BerkeleyDB to malloc the return value (thread safe) */ |
| 3734 | data.flags = DB_DBT_MALLOC; |
| 3735 | } |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3736 | if (!add_partial_dbt(&data, dlen, doff)) { |
| 3737 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3738 | return NULL; |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3739 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3740 | |
| 3741 | MYDB_BEGIN_ALLOW_THREADS; |
| 3742 | err = self->dbc->c_get(self->dbc, &key, &data, flags|DB_SET_RECNO); |
| 3743 | MYDB_END_ALLOW_THREADS; |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 3744 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 3745 | && self->mydb->moduleFlags.cursorSetReturnsNone) { |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3746 | Py_INCREF(Py_None); |
| 3747 | retval = Py_None; |
| 3748 | } |
| 3749 | else if (makeDBError(err)) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3750 | retval = NULL; |
| 3751 | } |
| 3752 | else { /* Can only be used for BTrees, so no need to return int key */ |
| 3753 | retval = Py_BuildValue("s#s#", key.data, key.size, |
| 3754 | data.data, data.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3755 | FREE_DBT(data); |
| 3756 | } |
Gregory P. Smith | dc5af70 | 2004-06-27 23:32:34 +0000 | [diff] [blame] | 3757 | FREE_DBT(key); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3758 | |
| 3759 | return retval; |
| 3760 | } |
| 3761 | |
| 3762 | |
| 3763 | static PyObject* |
| 3764 | DBC_consume(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3765 | { |
| 3766 | return _DBCursor_get(self,DB_CONSUME,args,kwargs,"|iii:consume"); |
| 3767 | } |
| 3768 | |
| 3769 | |
| 3770 | static PyObject* |
| 3771 | DBC_next_dup(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3772 | { |
| 3773 | return _DBCursor_get(self,DB_NEXT_DUP,args,kwargs,"|iii:next_dup"); |
| 3774 | } |
| 3775 | |
| 3776 | |
| 3777 | static PyObject* |
| 3778 | DBC_next_nodup(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3779 | { |
| 3780 | return _DBCursor_get(self,DB_NEXT_NODUP,args,kwargs,"|iii:next_nodup"); |
| 3781 | } |
| 3782 | |
| 3783 | |
| 3784 | static PyObject* |
| 3785 | DBC_prev_nodup(DBCursorObject* self, PyObject* args, PyObject *kwargs) |
| 3786 | { |
| 3787 | return _DBCursor_get(self,DB_PREV_NODUP,args,kwargs,"|iii:prev_nodup"); |
| 3788 | } |
| 3789 | |
| 3790 | |
| 3791 | static PyObject* |
| 3792 | DBC_join_item(DBCursorObject* self, PyObject* args) |
| 3793 | { |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3794 | int err, flags=0; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3795 | DBT key, data; |
| 3796 | PyObject* retval; |
| 3797 | |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3798 | if (!PyArg_ParseTuple(args, "|i:join_item", &flags)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3799 | return NULL; |
| 3800 | |
| 3801 | CHECK_CURSOR_NOT_CLOSED(self); |
| 3802 | |
| 3803 | CLEAR_DBT(key); |
| 3804 | CLEAR_DBT(data); |
| 3805 | if (CHECK_DBFLAG(self->mydb, DB_THREAD)) { |
| 3806 | /* Tell BerkeleyDB to malloc the return value (thread safe) */ |
| 3807 | key.flags = DB_DBT_MALLOC; |
| 3808 | } |
| 3809 | |
| 3810 | MYDB_BEGIN_ALLOW_THREADS; |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3811 | err = self->dbc->c_get(self->dbc, &key, &data, flags | DB_JOIN_ITEM); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3812 | MYDB_END_ALLOW_THREADS; |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 3813 | if ((err == DB_NOTFOUND || err == DB_KEYEMPTY) |
| 3814 | && self->mydb->moduleFlags.getReturnsNone) { |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 3815 | Py_INCREF(Py_None); |
| 3816 | retval = Py_None; |
| 3817 | } |
| 3818 | else if (makeDBError(err)) { |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3819 | retval = NULL; |
| 3820 | } |
| 3821 | else { |
Gregory P. Smith | 84261d2 | 2003-07-07 19:06:45 +0000 | [diff] [blame] | 3822 | retval = Py_BuildValue("s#", key.data, key.size); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3823 | FREE_DBT(key); |
| 3824 | } |
| 3825 | |
| 3826 | return retval; |
| 3827 | } |
| 3828 | |
| 3829 | |
| 3830 | |
| 3831 | /* --------------------------------------------------------------------- */ |
| 3832 | /* DBEnv methods */ |
| 3833 | |
| 3834 | |
| 3835 | static PyObject* |
| 3836 | DBEnv_close(DBEnvObject* self, PyObject* args) |
| 3837 | { |
| 3838 | int err, flags = 0; |
| 3839 | |
| 3840 | if (!PyArg_ParseTuple(args, "|i:close", &flags)) |
| 3841 | return NULL; |
| 3842 | if (!self->closed) { /* Don't close more than once */ |
| 3843 | MYDB_BEGIN_ALLOW_THREADS; |
| 3844 | err = self->db_env->close(self->db_env, flags); |
| 3845 | MYDB_END_ALLOW_THREADS; |
| 3846 | /* after calling DBEnv->close, regardless of error, this DBEnv |
| 3847 | * may not be accessed again (BerkeleyDB docs). */ |
| 3848 | self->closed = 1; |
| 3849 | self->db_env = NULL; |
| 3850 | RETURN_IF_ERR(); |
| 3851 | } |
| 3852 | RETURN_NONE(); |
| 3853 | } |
| 3854 | |
| 3855 | |
| 3856 | static PyObject* |
| 3857 | DBEnv_open(DBEnvObject* self, PyObject* args) |
| 3858 | { |
| 3859 | int err, flags=0, mode=0660; |
| 3860 | char *db_home; |
| 3861 | |
| 3862 | if (!PyArg_ParseTuple(args, "z|ii:open", &db_home, &flags, &mode)) |
| 3863 | return NULL; |
| 3864 | |
| 3865 | CHECK_ENV_NOT_CLOSED(self); |
| 3866 | |
| 3867 | MYDB_BEGIN_ALLOW_THREADS; |
| 3868 | err = self->db_env->open(self->db_env, db_home, flags, mode); |
| 3869 | MYDB_END_ALLOW_THREADS; |
| 3870 | RETURN_IF_ERR(); |
| 3871 | self->closed = 0; |
| 3872 | self->flags = flags; |
| 3873 | RETURN_NONE(); |
| 3874 | } |
| 3875 | |
| 3876 | |
| 3877 | static PyObject* |
| 3878 | DBEnv_remove(DBEnvObject* self, PyObject* args) |
| 3879 | { |
| 3880 | int err, flags=0; |
| 3881 | char *db_home; |
| 3882 | |
| 3883 | if (!PyArg_ParseTuple(args, "s|i:remove", &db_home, &flags)) |
| 3884 | return NULL; |
| 3885 | CHECK_ENV_NOT_CLOSED(self); |
| 3886 | MYDB_BEGIN_ALLOW_THREADS; |
| 3887 | err = self->db_env->remove(self->db_env, db_home, flags); |
| 3888 | MYDB_END_ALLOW_THREADS; |
| 3889 | RETURN_IF_ERR(); |
| 3890 | RETURN_NONE(); |
| 3891 | } |
| 3892 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 3893 | #if (DBVER >= 41) |
| 3894 | static PyObject* |
| 3895 | DBEnv_dbremove(DBEnvObject* self, PyObject* args, PyObject* kwargs) |
| 3896 | { |
| 3897 | int err; |
| 3898 | u_int32_t flags=0; |
| 3899 | char *file = NULL; |
| 3900 | char *database = NULL; |
| 3901 | PyObject *txnobj = NULL; |
| 3902 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3903 | static char* kwnames[] = { "file", "database", "txn", "flags", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 3904 | NULL }; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 3905 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3906 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|zOi:dbremove", kwnames, |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 3907 | &file, &database, &txnobj, &flags)) { |
| 3908 | return NULL; |
| 3909 | } |
| 3910 | if (!checkTxnObj(txnobj, &txn)) { |
| 3911 | return NULL; |
| 3912 | } |
| 3913 | CHECK_ENV_NOT_CLOSED(self); |
| 3914 | MYDB_BEGIN_ALLOW_THREADS; |
| 3915 | err = self->db_env->dbremove(self->db_env, txn, file, database, flags); |
| 3916 | MYDB_END_ALLOW_THREADS; |
| 3917 | RETURN_IF_ERR(); |
| 3918 | RETURN_NONE(); |
| 3919 | } |
| 3920 | |
| 3921 | static PyObject* |
| 3922 | DBEnv_dbrename(DBEnvObject* self, PyObject* args, PyObject* kwargs) |
| 3923 | { |
| 3924 | int err; |
| 3925 | u_int32_t flags=0; |
| 3926 | char *file = NULL; |
| 3927 | char *database = NULL; |
| 3928 | char *newname = NULL; |
| 3929 | PyObject *txnobj = NULL; |
| 3930 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3931 | static char* kwnames[] = { "file", "database", "newname", "txn", |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 3932 | "flags", NULL }; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 3933 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3934 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "szs|Oi:dbrename", kwnames, |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 3935 | &file, &database, &newname, &txnobj, &flags)) { |
| 3936 | return NULL; |
| 3937 | } |
| 3938 | if (!checkTxnObj(txnobj, &txn)) { |
| 3939 | return NULL; |
| 3940 | } |
| 3941 | CHECK_ENV_NOT_CLOSED(self); |
| 3942 | MYDB_BEGIN_ALLOW_THREADS; |
| 3943 | err = self->db_env->dbrename(self->db_env, txn, file, database, newname, |
| 3944 | flags); |
| 3945 | MYDB_END_ALLOW_THREADS; |
| 3946 | RETURN_IF_ERR(); |
| 3947 | RETURN_NONE(); |
| 3948 | } |
| 3949 | |
| 3950 | static PyObject* |
| 3951 | DBEnv_set_encrypt(DBEnvObject* self, PyObject* args, PyObject* kwargs) |
| 3952 | { |
| 3953 | int err; |
| 3954 | u_int32_t flags=0; |
| 3955 | char *passwd = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3956 | static char* kwnames[] = { "passwd", "flags", NULL }; |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 3957 | |
| 3958 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|i:set_encrypt", kwnames, |
| 3959 | &passwd, &flags)) { |
| 3960 | return NULL; |
| 3961 | } |
| 3962 | |
| 3963 | MYDB_BEGIN_ALLOW_THREADS; |
| 3964 | err = self->db_env->set_encrypt(self->db_env, passwd, flags); |
| 3965 | MYDB_END_ALLOW_THREADS; |
| 3966 | |
| 3967 | RETURN_IF_ERR(); |
| 3968 | RETURN_NONE(); |
| 3969 | } |
| 3970 | #endif /* DBVER >= 41 */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3971 | |
Gregory P. Smith | fe11d3e | 2003-03-27 17:23:29 +0000 | [diff] [blame] | 3972 | #if (DBVER >= 40) |
| 3973 | static PyObject* |
| 3974 | DBEnv_set_timeout(DBEnvObject* self, PyObject* args, PyObject* kwargs) |
| 3975 | { |
| 3976 | int err; |
| 3977 | u_int32_t flags=0; |
| 3978 | u_int32_t timeout = 0; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 3979 | static char* kwnames[] = { "timeout", "flags", NULL }; |
Gregory P. Smith | fe11d3e | 2003-03-27 17:23:29 +0000 | [diff] [blame] | 3980 | |
| 3981 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii:set_timeout", kwnames, |
| 3982 | &timeout, &flags)) { |
| 3983 | return NULL; |
| 3984 | } |
| 3985 | |
| 3986 | MYDB_BEGIN_ALLOW_THREADS; |
| 3987 | err = self->db_env->set_timeout(self->db_env, (db_timeout_t)timeout, flags); |
| 3988 | MYDB_END_ALLOW_THREADS; |
| 3989 | |
| 3990 | RETURN_IF_ERR(); |
| 3991 | RETURN_NONE(); |
| 3992 | } |
| 3993 | #endif /* DBVER >= 40 */ |
| 3994 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 3995 | static PyObject* |
Gregory P. Smith | 6676f6e | 2003-08-28 21:50:30 +0000 | [diff] [blame] | 3996 | DBEnv_set_shm_key(DBEnvObject* self, PyObject* args) |
| 3997 | { |
| 3998 | int err; |
| 3999 | long shm_key = 0; |
| 4000 | |
| 4001 | if (!PyArg_ParseTuple(args, "l:set_shm_key", &shm_key)) |
| 4002 | return NULL; |
| 4003 | CHECK_ENV_NOT_CLOSED(self); |
| 4004 | |
| 4005 | err = self->db_env->set_shm_key(self->db_env, shm_key); |
| 4006 | RETURN_IF_ERR(); |
| 4007 | RETURN_NONE(); |
| 4008 | } |
| 4009 | |
| 4010 | static PyObject* |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4011 | DBEnv_set_cachesize(DBEnvObject* self, PyObject* args) |
| 4012 | { |
| 4013 | int err, gbytes=0, bytes=0, ncache=0; |
| 4014 | |
| 4015 | if (!PyArg_ParseTuple(args, "ii|i:set_cachesize", |
| 4016 | &gbytes, &bytes, &ncache)) |
| 4017 | return NULL; |
| 4018 | CHECK_ENV_NOT_CLOSED(self); |
| 4019 | |
| 4020 | MYDB_BEGIN_ALLOW_THREADS; |
| 4021 | err = self->db_env->set_cachesize(self->db_env, gbytes, bytes, ncache); |
| 4022 | MYDB_END_ALLOW_THREADS; |
| 4023 | RETURN_IF_ERR(); |
| 4024 | RETURN_NONE(); |
| 4025 | } |
| 4026 | |
| 4027 | |
| 4028 | #if (DBVER >= 32) |
| 4029 | static PyObject* |
| 4030 | DBEnv_set_flags(DBEnvObject* self, PyObject* args) |
| 4031 | { |
| 4032 | int err, flags=0, onoff=0; |
| 4033 | |
| 4034 | if (!PyArg_ParseTuple(args, "ii:set_flags", |
| 4035 | &flags, &onoff)) |
| 4036 | return NULL; |
| 4037 | CHECK_ENV_NOT_CLOSED(self); |
| 4038 | |
| 4039 | MYDB_BEGIN_ALLOW_THREADS; |
| 4040 | err = self->db_env->set_flags(self->db_env, flags, onoff); |
| 4041 | MYDB_END_ALLOW_THREADS; |
| 4042 | RETURN_IF_ERR(); |
| 4043 | RETURN_NONE(); |
| 4044 | } |
| 4045 | #endif |
| 4046 | |
| 4047 | |
| 4048 | static PyObject* |
| 4049 | DBEnv_set_data_dir(DBEnvObject* self, PyObject* args) |
| 4050 | { |
| 4051 | int err; |
| 4052 | char *dir; |
| 4053 | |
| 4054 | if (!PyArg_ParseTuple(args, "s:set_data_dir", &dir)) |
| 4055 | return NULL; |
| 4056 | CHECK_ENV_NOT_CLOSED(self); |
| 4057 | |
| 4058 | MYDB_BEGIN_ALLOW_THREADS; |
| 4059 | err = self->db_env->set_data_dir(self->db_env, dir); |
| 4060 | MYDB_END_ALLOW_THREADS; |
| 4061 | RETURN_IF_ERR(); |
| 4062 | RETURN_NONE(); |
| 4063 | } |
| 4064 | |
| 4065 | |
| 4066 | static PyObject* |
| 4067 | DBEnv_set_lg_bsize(DBEnvObject* self, PyObject* args) |
| 4068 | { |
| 4069 | int err, lg_bsize; |
| 4070 | |
| 4071 | if (!PyArg_ParseTuple(args, "i:set_lg_bsize", &lg_bsize)) |
| 4072 | return NULL; |
| 4073 | CHECK_ENV_NOT_CLOSED(self); |
| 4074 | |
| 4075 | MYDB_BEGIN_ALLOW_THREADS; |
| 4076 | err = self->db_env->set_lg_bsize(self->db_env, lg_bsize); |
| 4077 | MYDB_END_ALLOW_THREADS; |
| 4078 | RETURN_IF_ERR(); |
| 4079 | RETURN_NONE(); |
| 4080 | } |
| 4081 | |
| 4082 | |
| 4083 | static PyObject* |
| 4084 | DBEnv_set_lg_dir(DBEnvObject* self, PyObject* args) |
| 4085 | { |
| 4086 | int err; |
| 4087 | char *dir; |
| 4088 | |
| 4089 | if (!PyArg_ParseTuple(args, "s:set_lg_dir", &dir)) |
| 4090 | return NULL; |
| 4091 | CHECK_ENV_NOT_CLOSED(self); |
| 4092 | |
| 4093 | MYDB_BEGIN_ALLOW_THREADS; |
| 4094 | err = self->db_env->set_lg_dir(self->db_env, dir); |
| 4095 | MYDB_END_ALLOW_THREADS; |
| 4096 | RETURN_IF_ERR(); |
| 4097 | RETURN_NONE(); |
| 4098 | } |
| 4099 | |
| 4100 | static PyObject* |
| 4101 | DBEnv_set_lg_max(DBEnvObject* self, PyObject* args) |
| 4102 | { |
| 4103 | int err, lg_max; |
| 4104 | |
| 4105 | if (!PyArg_ParseTuple(args, "i:set_lg_max", &lg_max)) |
| 4106 | return NULL; |
| 4107 | CHECK_ENV_NOT_CLOSED(self); |
| 4108 | |
| 4109 | MYDB_BEGIN_ALLOW_THREADS; |
| 4110 | err = self->db_env->set_lg_max(self->db_env, lg_max); |
| 4111 | MYDB_END_ALLOW_THREADS; |
| 4112 | RETURN_IF_ERR(); |
| 4113 | RETURN_NONE(); |
| 4114 | } |
| 4115 | |
| 4116 | |
Neal Norwitz | 8456235 | 2005-10-20 04:30:15 +0000 | [diff] [blame] | 4117 | #if (DBVER >= 33) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4118 | static PyObject* |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 4119 | DBEnv_set_lg_regionmax(DBEnvObject* self, PyObject* args) |
| 4120 | { |
| 4121 | int err, lg_max; |
| 4122 | |
| 4123 | if (!PyArg_ParseTuple(args, "i:set_lg_regionmax", &lg_max)) |
| 4124 | return NULL; |
| 4125 | CHECK_ENV_NOT_CLOSED(self); |
| 4126 | |
| 4127 | MYDB_BEGIN_ALLOW_THREADS; |
| 4128 | err = self->db_env->set_lg_regionmax(self->db_env, lg_max); |
| 4129 | MYDB_END_ALLOW_THREADS; |
| 4130 | RETURN_IF_ERR(); |
| 4131 | RETURN_NONE(); |
| 4132 | } |
Neal Norwitz | 8456235 | 2005-10-20 04:30:15 +0000 | [diff] [blame] | 4133 | #endif |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 4134 | |
| 4135 | |
| 4136 | static PyObject* |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4137 | DBEnv_set_lk_detect(DBEnvObject* self, PyObject* args) |
| 4138 | { |
| 4139 | int err, lk_detect; |
| 4140 | |
| 4141 | if (!PyArg_ParseTuple(args, "i:set_lk_detect", &lk_detect)) |
| 4142 | return NULL; |
| 4143 | CHECK_ENV_NOT_CLOSED(self); |
| 4144 | |
| 4145 | MYDB_BEGIN_ALLOW_THREADS; |
| 4146 | err = self->db_env->set_lk_detect(self->db_env, lk_detect); |
| 4147 | MYDB_END_ALLOW_THREADS; |
| 4148 | RETURN_IF_ERR(); |
| 4149 | RETURN_NONE(); |
| 4150 | } |
| 4151 | |
| 4152 | |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 4153 | #if (DBVER < 45) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4154 | static PyObject* |
| 4155 | DBEnv_set_lk_max(DBEnvObject* self, PyObject* args) |
| 4156 | { |
| 4157 | int err, max; |
| 4158 | |
| 4159 | if (!PyArg_ParseTuple(args, "i:set_lk_max", &max)) |
| 4160 | return NULL; |
| 4161 | CHECK_ENV_NOT_CLOSED(self); |
| 4162 | |
| 4163 | MYDB_BEGIN_ALLOW_THREADS; |
| 4164 | err = self->db_env->set_lk_max(self->db_env, max); |
| 4165 | MYDB_END_ALLOW_THREADS; |
| 4166 | RETURN_IF_ERR(); |
| 4167 | RETURN_NONE(); |
| 4168 | } |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 4169 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4170 | |
| 4171 | |
| 4172 | #if (DBVER >= 32) |
| 4173 | |
| 4174 | static PyObject* |
| 4175 | DBEnv_set_lk_max_locks(DBEnvObject* self, PyObject* args) |
| 4176 | { |
| 4177 | int err, max; |
| 4178 | |
| 4179 | if (!PyArg_ParseTuple(args, "i:set_lk_max_locks", &max)) |
| 4180 | return NULL; |
| 4181 | CHECK_ENV_NOT_CLOSED(self); |
| 4182 | |
| 4183 | MYDB_BEGIN_ALLOW_THREADS; |
| 4184 | err = self->db_env->set_lk_max_locks(self->db_env, max); |
| 4185 | MYDB_END_ALLOW_THREADS; |
| 4186 | RETURN_IF_ERR(); |
| 4187 | RETURN_NONE(); |
| 4188 | } |
| 4189 | |
| 4190 | |
| 4191 | static PyObject* |
| 4192 | DBEnv_set_lk_max_lockers(DBEnvObject* self, PyObject* args) |
| 4193 | { |
| 4194 | int err, max; |
| 4195 | |
| 4196 | if (!PyArg_ParseTuple(args, "i:set_lk_max_lockers", &max)) |
| 4197 | return NULL; |
| 4198 | CHECK_ENV_NOT_CLOSED(self); |
| 4199 | |
| 4200 | MYDB_BEGIN_ALLOW_THREADS; |
| 4201 | err = self->db_env->set_lk_max_lockers(self->db_env, max); |
| 4202 | MYDB_END_ALLOW_THREADS; |
| 4203 | RETURN_IF_ERR(); |
| 4204 | RETURN_NONE(); |
| 4205 | } |
| 4206 | |
| 4207 | |
| 4208 | static PyObject* |
| 4209 | DBEnv_set_lk_max_objects(DBEnvObject* self, PyObject* args) |
| 4210 | { |
| 4211 | int err, max; |
| 4212 | |
| 4213 | if (!PyArg_ParseTuple(args, "i:set_lk_max_objects", &max)) |
| 4214 | return NULL; |
| 4215 | CHECK_ENV_NOT_CLOSED(self); |
| 4216 | |
| 4217 | MYDB_BEGIN_ALLOW_THREADS; |
| 4218 | err = self->db_env->set_lk_max_objects(self->db_env, max); |
| 4219 | MYDB_END_ALLOW_THREADS; |
| 4220 | RETURN_IF_ERR(); |
| 4221 | RETURN_NONE(); |
| 4222 | } |
| 4223 | |
| 4224 | #endif |
| 4225 | |
| 4226 | |
| 4227 | static PyObject* |
| 4228 | DBEnv_set_mp_mmapsize(DBEnvObject* self, PyObject* args) |
| 4229 | { |
| 4230 | int err, mp_mmapsize; |
| 4231 | |
| 4232 | if (!PyArg_ParseTuple(args, "i:set_mp_mmapsize", &mp_mmapsize)) |
| 4233 | return NULL; |
| 4234 | CHECK_ENV_NOT_CLOSED(self); |
| 4235 | |
| 4236 | MYDB_BEGIN_ALLOW_THREADS; |
| 4237 | err = self->db_env->set_mp_mmapsize(self->db_env, mp_mmapsize); |
| 4238 | MYDB_END_ALLOW_THREADS; |
| 4239 | RETURN_IF_ERR(); |
| 4240 | RETURN_NONE(); |
| 4241 | } |
| 4242 | |
| 4243 | |
| 4244 | static PyObject* |
| 4245 | DBEnv_set_tmp_dir(DBEnvObject* self, PyObject* args) |
| 4246 | { |
| 4247 | int err; |
| 4248 | char *dir; |
| 4249 | |
| 4250 | if (!PyArg_ParseTuple(args, "s:set_tmp_dir", &dir)) |
| 4251 | return NULL; |
| 4252 | CHECK_ENV_NOT_CLOSED(self); |
| 4253 | |
| 4254 | MYDB_BEGIN_ALLOW_THREADS; |
| 4255 | err = self->db_env->set_tmp_dir(self->db_env, dir); |
| 4256 | MYDB_END_ALLOW_THREADS; |
| 4257 | RETURN_IF_ERR(); |
| 4258 | RETURN_NONE(); |
| 4259 | } |
| 4260 | |
| 4261 | |
| 4262 | static PyObject* |
| 4263 | DBEnv_txn_begin(DBEnvObject* self, PyObject* args, PyObject* kwargs) |
| 4264 | { |
| 4265 | int flags = 0; |
| 4266 | PyObject* txnobj = NULL; |
| 4267 | DB_TXN *txn = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 4268 | static char* kwnames[] = { "parent", "flags", NULL }; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4269 | |
| 4270 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oi:txn_begin", kwnames, |
| 4271 | &txnobj, &flags)) |
| 4272 | return NULL; |
| 4273 | |
| 4274 | if (!checkTxnObj(txnobj, &txn)) |
| 4275 | return NULL; |
| 4276 | CHECK_ENV_NOT_CLOSED(self); |
| 4277 | |
| 4278 | return (PyObject*)newDBTxnObject(self, txn, flags); |
| 4279 | } |
| 4280 | |
| 4281 | |
| 4282 | static PyObject* |
| 4283 | DBEnv_txn_checkpoint(DBEnvObject* self, PyObject* args) |
| 4284 | { |
| 4285 | int err, kbyte=0, min=0, flags=0; |
| 4286 | |
| 4287 | if (!PyArg_ParseTuple(args, "|iii:txn_checkpoint", &kbyte, &min, &flags)) |
| 4288 | return NULL; |
| 4289 | CHECK_ENV_NOT_CLOSED(self); |
| 4290 | |
| 4291 | MYDB_BEGIN_ALLOW_THREADS; |
| 4292 | #if (DBVER >= 40) |
| 4293 | err = self->db_env->txn_checkpoint(self->db_env, kbyte, min, flags); |
| 4294 | #else |
| 4295 | err = txn_checkpoint(self->db_env, kbyte, min, flags); |
| 4296 | #endif |
| 4297 | MYDB_END_ALLOW_THREADS; |
| 4298 | RETURN_IF_ERR(); |
| 4299 | RETURN_NONE(); |
| 4300 | } |
| 4301 | |
| 4302 | |
| 4303 | static PyObject* |
| 4304 | DBEnv_set_tx_max(DBEnvObject* self, PyObject* args) |
| 4305 | { |
| 4306 | int err, max; |
| 4307 | |
| 4308 | if (!PyArg_ParseTuple(args, "i:set_tx_max", &max)) |
| 4309 | return NULL; |
| 4310 | CHECK_ENV_NOT_CLOSED(self); |
| 4311 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4312 | err = self->db_env->set_tx_max(self->db_env, max); |
Gregory P. Smith | 8a47404 | 2006-01-27 07:05:40 +0000 | [diff] [blame] | 4313 | RETURN_IF_ERR(); |
| 4314 | RETURN_NONE(); |
| 4315 | } |
| 4316 | |
| 4317 | |
| 4318 | static PyObject* |
| 4319 | DBEnv_set_tx_timestamp(DBEnvObject* self, PyObject* args) |
| 4320 | { |
| 4321 | int err; |
Thomas Wouters | 9d63cca | 2006-03-01 01:01:55 +0000 | [diff] [blame] | 4322 | long stamp; |
| 4323 | time_t timestamp; |
Gregory P. Smith | 8a47404 | 2006-01-27 07:05:40 +0000 | [diff] [blame] | 4324 | |
Thomas Wouters | 9d63cca | 2006-03-01 01:01:55 +0000 | [diff] [blame] | 4325 | if (!PyArg_ParseTuple(args, "l:set_tx_timestamp", &stamp)) |
Gregory P. Smith | 8a47404 | 2006-01-27 07:05:40 +0000 | [diff] [blame] | 4326 | return NULL; |
| 4327 | CHECK_ENV_NOT_CLOSED(self); |
Thomas Wouters | 9d63cca | 2006-03-01 01:01:55 +0000 | [diff] [blame] | 4328 | timestamp = (time_t)stamp; |
| 4329 | 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] | 4330 | RETURN_IF_ERR(); |
| 4331 | RETURN_NONE(); |
| 4332 | } |
| 4333 | |
| 4334 | |
| 4335 | static PyObject* |
| 4336 | DBEnv_lock_detect(DBEnvObject* self, PyObject* args) |
| 4337 | { |
| 4338 | int err, atype, flags=0; |
| 4339 | int aborted = 0; |
| 4340 | |
| 4341 | if (!PyArg_ParseTuple(args, "i|i:lock_detect", &atype, &flags)) |
| 4342 | return NULL; |
| 4343 | CHECK_ENV_NOT_CLOSED(self); |
| 4344 | |
| 4345 | MYDB_BEGIN_ALLOW_THREADS; |
| 4346 | #if (DBVER >= 40) |
| 4347 | err = self->db_env->lock_detect(self->db_env, flags, atype, &aborted); |
| 4348 | #else |
| 4349 | err = lock_detect(self->db_env, flags, atype, &aborted); |
| 4350 | #endif |
| 4351 | MYDB_END_ALLOW_THREADS; |
| 4352 | RETURN_IF_ERR(); |
| 4353 | return PyInt_FromLong(aborted); |
| 4354 | } |
| 4355 | |
| 4356 | |
| 4357 | static PyObject* |
| 4358 | DBEnv_lock_get(DBEnvObject* self, PyObject* args) |
| 4359 | { |
| 4360 | int flags=0; |
| 4361 | int locker, lock_mode; |
| 4362 | DBT obj; |
| 4363 | PyObject* objobj; |
| 4364 | |
| 4365 | if (!PyArg_ParseTuple(args, "iOi|i:lock_get", &locker, &objobj, &lock_mode, &flags)) |
| 4366 | return NULL; |
| 4367 | |
| 4368 | |
| 4369 | if (!make_dbt(objobj, &obj)) |
| 4370 | return NULL; |
| 4371 | |
| 4372 | return (PyObject*)newDBLockObject(self, locker, &obj, lock_mode, flags); |
| 4373 | } |
| 4374 | |
| 4375 | |
| 4376 | static PyObject* |
| 4377 | DBEnv_lock_id(DBEnvObject* self, PyObject* args) |
| 4378 | { |
| 4379 | int err; |
| 4380 | u_int32_t theID; |
| 4381 | |
| 4382 | if (!PyArg_ParseTuple(args, ":lock_id")) |
| 4383 | return NULL; |
| 4384 | |
| 4385 | CHECK_ENV_NOT_CLOSED(self); |
| 4386 | MYDB_BEGIN_ALLOW_THREADS; |
| 4387 | #if (DBVER >= 40) |
| 4388 | err = self->db_env->lock_id(self->db_env, &theID); |
| 4389 | #else |
| 4390 | err = lock_id(self->db_env, &theID); |
| 4391 | #endif |
| 4392 | MYDB_END_ALLOW_THREADS; |
| 4393 | RETURN_IF_ERR(); |
| 4394 | |
| 4395 | return PyInt_FromLong((long)theID); |
| 4396 | } |
| 4397 | |
| 4398 | |
| 4399 | static PyObject* |
| 4400 | DBEnv_lock_put(DBEnvObject* self, PyObject* args) |
| 4401 | { |
| 4402 | int err; |
| 4403 | DBLockObject* dblockobj; |
| 4404 | |
| 4405 | if (!PyArg_ParseTuple(args, "O!:lock_put", &DBLock_Type, &dblockobj)) |
| 4406 | return NULL; |
| 4407 | |
| 4408 | CHECK_ENV_NOT_CLOSED(self); |
| 4409 | MYDB_BEGIN_ALLOW_THREADS; |
| 4410 | #if (DBVER >= 40) |
| 4411 | err = self->db_env->lock_put(self->db_env, &dblockobj->lock); |
| 4412 | #else |
| 4413 | err = lock_put(self->db_env, &dblockobj->lock); |
| 4414 | #endif |
| 4415 | MYDB_END_ALLOW_THREADS; |
| 4416 | RETURN_IF_ERR(); |
| 4417 | RETURN_NONE(); |
| 4418 | } |
| 4419 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4420 | #if (DBVER >= 44) |
| 4421 | static PyObject* |
| 4422 | DBEnv_lsn_reset(DBEnvObject* self, PyObject* args, PyObject* kwargs) |
| 4423 | { |
| 4424 | int err; |
| 4425 | char *file; |
| 4426 | u_int32_t flags = 0; |
| 4427 | static char* kwnames[] = { "file", "flags", NULL}; |
| 4428 | |
| 4429 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "z|i:lsn_reset", kwnames, |
| 4430 | &file, &flags)) |
| 4431 | return NULL; |
| 4432 | CHECK_ENV_NOT_CLOSED(self); |
| 4433 | |
| 4434 | MYDB_BEGIN_ALLOW_THREADS; |
| 4435 | err = self->db_env->lsn_reset(self->db_env, file, flags); |
| 4436 | MYDB_END_ALLOW_THREADS; |
| 4437 | RETURN_IF_ERR(); |
| 4438 | RETURN_NONE(); |
| 4439 | } |
| 4440 | #endif /* DBVER >= 4.4 */ |
| 4441 | |
| 4442 | #if (DBVER >= 40) |
| 4443 | static PyObject* |
| 4444 | DBEnv_log_stat(DBEnvObject* self, PyObject* args) |
| 4445 | { |
| 4446 | int err; |
| 4447 | DB_LOG_STAT* statp = NULL; |
| 4448 | PyObject* d = NULL; |
| 4449 | u_int32_t flags = 0; |
| 4450 | |
| 4451 | if (!PyArg_ParseTuple(args, "|i:log_stat", &flags)) |
| 4452 | return NULL; |
| 4453 | CHECK_ENV_NOT_CLOSED(self); |
| 4454 | |
| 4455 | MYDB_BEGIN_ALLOW_THREADS; |
| 4456 | err = self->db_env->log_stat(self->db_env, &statp, flags); |
| 4457 | MYDB_END_ALLOW_THREADS; |
| 4458 | RETURN_IF_ERR(); |
| 4459 | |
| 4460 | /* Turn the stat structure into a dictionary */ |
| 4461 | d = PyDict_New(); |
| 4462 | if (d == NULL) { |
| 4463 | if (statp) |
| 4464 | free(statp); |
| 4465 | return NULL; |
| 4466 | } |
| 4467 | |
| 4468 | #define MAKE_ENTRY(name) _addIntToDict(d, #name, statp->st_##name) |
| 4469 | |
| 4470 | MAKE_ENTRY(magic); |
| 4471 | MAKE_ENTRY(version); |
| 4472 | MAKE_ENTRY(mode); |
| 4473 | MAKE_ENTRY(lg_bsize); |
| 4474 | #if (DBVER >= 44) |
| 4475 | MAKE_ENTRY(lg_size); |
| 4476 | MAKE_ENTRY(record); |
| 4477 | #endif |
| 4478 | #if (DBVER <= 40) |
| 4479 | MAKE_ENTRY(lg_max); |
| 4480 | #endif |
| 4481 | MAKE_ENTRY(w_mbytes); |
| 4482 | MAKE_ENTRY(w_bytes); |
| 4483 | MAKE_ENTRY(wc_mbytes); |
| 4484 | MAKE_ENTRY(wc_bytes); |
| 4485 | MAKE_ENTRY(wcount); |
| 4486 | MAKE_ENTRY(wcount_fill); |
| 4487 | #if (DBVER >= 44) |
| 4488 | MAKE_ENTRY(rcount); |
| 4489 | #endif |
| 4490 | MAKE_ENTRY(scount); |
| 4491 | MAKE_ENTRY(cur_file); |
| 4492 | MAKE_ENTRY(cur_offset); |
| 4493 | MAKE_ENTRY(disk_file); |
| 4494 | MAKE_ENTRY(disk_offset); |
| 4495 | MAKE_ENTRY(maxcommitperflush); |
| 4496 | MAKE_ENTRY(mincommitperflush); |
| 4497 | MAKE_ENTRY(regsize); |
| 4498 | MAKE_ENTRY(region_wait); |
| 4499 | MAKE_ENTRY(region_nowait); |
| 4500 | |
| 4501 | #undef MAKE_ENTRY |
| 4502 | free(statp); |
| 4503 | return d; |
| 4504 | } /* DBEnv_log_stat */ |
| 4505 | #endif /* DBVER >= 4.0 for log_stat method */ |
| 4506 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4507 | |
| 4508 | static PyObject* |
| 4509 | DBEnv_lock_stat(DBEnvObject* self, PyObject* args) |
| 4510 | { |
| 4511 | int err; |
| 4512 | DB_LOCK_STAT* sp; |
| 4513 | PyObject* d = NULL; |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 4514 | u_int32_t flags = 0; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4515 | |
| 4516 | if (!PyArg_ParseTuple(args, "|i:lock_stat", &flags)) |
| 4517 | return NULL; |
| 4518 | CHECK_ENV_NOT_CLOSED(self); |
| 4519 | |
| 4520 | MYDB_BEGIN_ALLOW_THREADS; |
| 4521 | #if (DBVER >= 40) |
| 4522 | err = self->db_env->lock_stat(self->db_env, &sp, flags); |
| 4523 | #else |
| 4524 | #if (DBVER >= 33) |
| 4525 | err = lock_stat(self->db_env, &sp); |
| 4526 | #else |
| 4527 | err = lock_stat(self->db_env, &sp, NULL); |
| 4528 | #endif |
| 4529 | #endif |
| 4530 | MYDB_END_ALLOW_THREADS; |
| 4531 | RETURN_IF_ERR(); |
| 4532 | |
| 4533 | /* Turn the stat structure into a dictionary */ |
| 4534 | d = PyDict_New(); |
| 4535 | if (d == NULL) { |
| 4536 | free(sp); |
| 4537 | return NULL; |
| 4538 | } |
| 4539 | |
| 4540 | #define MAKE_ENTRY(name) _addIntToDict(d, #name, sp->st_##name) |
| 4541 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 4542 | #if (DBVER < 41) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4543 | MAKE_ENTRY(lastid); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 4544 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4545 | MAKE_ENTRY(nmodes); |
| 4546 | #if (DBVER >= 32) |
| 4547 | MAKE_ENTRY(maxlocks); |
| 4548 | MAKE_ENTRY(maxlockers); |
| 4549 | MAKE_ENTRY(maxobjects); |
| 4550 | MAKE_ENTRY(nlocks); |
| 4551 | MAKE_ENTRY(maxnlocks); |
| 4552 | #endif |
| 4553 | MAKE_ENTRY(nlockers); |
| 4554 | MAKE_ENTRY(maxnlockers); |
| 4555 | #if (DBVER >= 32) |
| 4556 | MAKE_ENTRY(nobjects); |
| 4557 | MAKE_ENTRY(maxnobjects); |
| 4558 | #endif |
| 4559 | MAKE_ENTRY(nrequests); |
| 4560 | MAKE_ENTRY(nreleases); |
Gregory P. Smith | 29602d2 | 2006-01-24 09:46:48 +0000 | [diff] [blame] | 4561 | #if (DBVER < 44) |
| 4562 | MAKE_ENTRY(nnowaits); /* these were renamed in 4.4 */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4563 | MAKE_ENTRY(nconflicts); |
Gregory P. Smith | 29602d2 | 2006-01-24 09:46:48 +0000 | [diff] [blame] | 4564 | #else |
| 4565 | MAKE_ENTRY(lock_nowait); |
| 4566 | MAKE_ENTRY(lock_wait); |
| 4567 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4568 | MAKE_ENTRY(ndeadlocks); |
| 4569 | MAKE_ENTRY(regsize); |
| 4570 | MAKE_ENTRY(region_wait); |
| 4571 | MAKE_ENTRY(region_nowait); |
| 4572 | |
| 4573 | #undef MAKE_ENTRY |
| 4574 | free(sp); |
| 4575 | return d; |
| 4576 | } |
| 4577 | |
| 4578 | |
| 4579 | static PyObject* |
| 4580 | DBEnv_log_archive(DBEnvObject* self, PyObject* args) |
| 4581 | { |
| 4582 | int flags=0; |
| 4583 | int err; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4584 | char **log_list = NULL; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4585 | PyObject* list; |
| 4586 | PyObject* item = NULL; |
| 4587 | |
| 4588 | if (!PyArg_ParseTuple(args, "|i:log_archive", &flags)) |
| 4589 | return NULL; |
| 4590 | |
| 4591 | CHECK_ENV_NOT_CLOSED(self); |
| 4592 | MYDB_BEGIN_ALLOW_THREADS; |
| 4593 | #if (DBVER >= 40) |
| 4594 | err = self->db_env->log_archive(self->db_env, &log_list, flags); |
| 4595 | #elif (DBVER == 33) |
| 4596 | err = log_archive(self->db_env, &log_list, flags); |
| 4597 | #else |
| 4598 | err = log_archive(self->db_env, &log_list, flags, NULL); |
| 4599 | #endif |
| 4600 | MYDB_END_ALLOW_THREADS; |
| 4601 | RETURN_IF_ERR(); |
| 4602 | |
| 4603 | list = PyList_New(0); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4604 | if (list == NULL) { |
| 4605 | if (log_list) |
| 4606 | free(log_list); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4607 | return NULL; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4608 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4609 | |
| 4610 | if (log_list) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4611 | char **log_list_start; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4612 | for (log_list_start = log_list; *log_list != NULL; ++log_list) { |
| 4613 | item = PyString_FromString (*log_list); |
| 4614 | if (item == NULL) { |
| 4615 | Py_DECREF(list); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4616 | list = NULL; |
| 4617 | break; |
| 4618 | } |
| 4619 | PyList_Append(list, item); |
| 4620 | Py_DECREF(item); |
| 4621 | } |
| 4622 | free(log_list_start); |
| 4623 | } |
| 4624 | return list; |
| 4625 | } |
| 4626 | |
| 4627 | |
| 4628 | static PyObject* |
| 4629 | DBEnv_txn_stat(DBEnvObject* self, PyObject* args) |
| 4630 | { |
| 4631 | int err; |
| 4632 | DB_TXN_STAT* sp; |
| 4633 | PyObject* d = NULL; |
Martin v. Löwis | b2c7aff | 2002-11-23 11:26:07 +0000 | [diff] [blame] | 4634 | u_int32_t flags=0; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4635 | |
| 4636 | if (!PyArg_ParseTuple(args, "|i:txn_stat", &flags)) |
| 4637 | return NULL; |
| 4638 | CHECK_ENV_NOT_CLOSED(self); |
| 4639 | |
| 4640 | MYDB_BEGIN_ALLOW_THREADS; |
| 4641 | #if (DBVER >= 40) |
| 4642 | err = self->db_env->txn_stat(self->db_env, &sp, flags); |
| 4643 | #elif (DBVER == 33) |
| 4644 | err = txn_stat(self->db_env, &sp); |
| 4645 | #else |
| 4646 | err = txn_stat(self->db_env, &sp, NULL); |
| 4647 | #endif |
| 4648 | MYDB_END_ALLOW_THREADS; |
| 4649 | RETURN_IF_ERR(); |
| 4650 | |
| 4651 | /* Turn the stat structure into a dictionary */ |
| 4652 | d = PyDict_New(); |
| 4653 | if (d == NULL) { |
| 4654 | free(sp); |
| 4655 | return NULL; |
| 4656 | } |
| 4657 | |
| 4658 | #define MAKE_ENTRY(name) _addIntToDict(d, #name, sp->st_##name) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4659 | #define MAKE_TIME_T_ENTRY(name)_addTimeTToDict(d, #name, sp->st_##name) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4660 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4661 | MAKE_TIME_T_ENTRY(time_ckp); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4662 | MAKE_ENTRY(last_txnid); |
| 4663 | MAKE_ENTRY(maxtxns); |
| 4664 | MAKE_ENTRY(nactive); |
| 4665 | MAKE_ENTRY(maxnactive); |
| 4666 | MAKE_ENTRY(nbegins); |
| 4667 | MAKE_ENTRY(naborts); |
| 4668 | MAKE_ENTRY(ncommits); |
| 4669 | MAKE_ENTRY(regsize); |
| 4670 | MAKE_ENTRY(region_wait); |
| 4671 | MAKE_ENTRY(region_nowait); |
| 4672 | |
| 4673 | #undef MAKE_ENTRY |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4674 | #undef MAKE_TIME_T_ENTRY |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4675 | free(sp); |
| 4676 | return d; |
| 4677 | } |
| 4678 | |
| 4679 | |
| 4680 | static PyObject* |
| 4681 | DBEnv_set_get_returns_none(DBEnvObject* self, PyObject* args) |
| 4682 | { |
| 4683 | int flags=0; |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 4684 | int oldValue=0; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4685 | |
| 4686 | if (!PyArg_ParseTuple(args,"i:set_get_returns_none", &flags)) |
| 4687 | return NULL; |
| 4688 | CHECK_ENV_NOT_CLOSED(self); |
| 4689 | |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 4690 | if (self->moduleFlags.getReturnsNone) |
| 4691 | ++oldValue; |
| 4692 | if (self->moduleFlags.cursorSetReturnsNone) |
| 4693 | ++oldValue; |
| 4694 | self->moduleFlags.getReturnsNone = (flags >= 1); |
| 4695 | self->moduleFlags.cursorSetReturnsNone = (flags >= 2); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4696 | return PyInt_FromLong(oldValue); |
| 4697 | } |
| 4698 | |
| 4699 | |
| 4700 | /* --------------------------------------------------------------------- */ |
| 4701 | /* DBTxn methods */ |
| 4702 | |
| 4703 | |
| 4704 | static PyObject* |
| 4705 | DBTxn_commit(DBTxnObject* self, PyObject* args) |
| 4706 | { |
| 4707 | int flags=0, err; |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 4708 | DB_TXN *txn; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4709 | |
| 4710 | if (!PyArg_ParseTuple(args, "|i:commit", &flags)) |
| 4711 | return NULL; |
| 4712 | |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 4713 | if (!self->txn) { |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 4714 | PyObject *t = Py_BuildValue("(is)", 0, "DBTxn must not be used " |
| 4715 | "after txn_commit or txn_abort"); |
| 4716 | PyErr_SetObject(DBError, t); |
| 4717 | Py_DECREF(t); |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 4718 | return NULL; |
| 4719 | } |
| 4720 | txn = self->txn; |
| 4721 | self->txn = NULL; /* this DB_TXN is no longer valid after this call */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4722 | MYDB_BEGIN_ALLOW_THREADS; |
| 4723 | #if (DBVER >= 40) |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 4724 | err = txn->commit(txn, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4725 | #else |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 4726 | err = txn_commit(txn, flags); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4727 | #endif |
| 4728 | MYDB_END_ALLOW_THREADS; |
| 4729 | RETURN_IF_ERR(); |
| 4730 | RETURN_NONE(); |
| 4731 | } |
| 4732 | |
| 4733 | static PyObject* |
| 4734 | DBTxn_prepare(DBTxnObject* self, PyObject* args) |
| 4735 | { |
| 4736 | #if (DBVER >= 33) |
| 4737 | int err; |
| 4738 | char* gid=NULL; |
| 4739 | int gid_size=0; |
| 4740 | |
| 4741 | if (!PyArg_ParseTuple(args, "s#:prepare", &gid, &gid_size)) |
| 4742 | return NULL; |
| 4743 | |
| 4744 | if (gid_size != DB_XIDDATASIZE) { |
| 4745 | PyErr_SetString(PyExc_TypeError, |
| 4746 | "gid must be DB_XIDDATASIZE bytes long"); |
| 4747 | return NULL; |
| 4748 | } |
| 4749 | |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 4750 | if (!self->txn) { |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 4751 | PyObject *t = Py_BuildValue("(is)", 0,"DBTxn must not be used " |
| 4752 | "after txn_commit or txn_abort"); |
| 4753 | PyErr_SetObject(DBError, t); |
| 4754 | Py_DECREF(t); |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 4755 | return NULL; |
| 4756 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4757 | MYDB_BEGIN_ALLOW_THREADS; |
| 4758 | #if (DBVER >= 40) |
| 4759 | err = self->txn->prepare(self->txn, (u_int8_t*)gid); |
| 4760 | #else |
| 4761 | err = txn_prepare(self->txn, (u_int8_t*)gid); |
| 4762 | #endif |
| 4763 | MYDB_END_ALLOW_THREADS; |
| 4764 | RETURN_IF_ERR(); |
| 4765 | RETURN_NONE(); |
| 4766 | #else |
| 4767 | int err; |
| 4768 | |
| 4769 | if (!PyArg_ParseTuple(args, ":prepare")) |
| 4770 | return NULL; |
| 4771 | |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 4772 | if (!self->txn) { |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 4773 | PyObject *t = Py_BuildValue("(is)", 0, "DBTxn must not be used " |
| 4774 | "after txn_commit or txn_abort"); |
| 4775 | PyErr_SetObject(DBError, t); |
| 4776 | Py_DECREF(t); |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 4777 | return NULL; |
| 4778 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4779 | MYDB_BEGIN_ALLOW_THREADS; |
| 4780 | err = txn_prepare(self->txn); |
| 4781 | MYDB_END_ALLOW_THREADS; |
| 4782 | RETURN_IF_ERR(); |
| 4783 | RETURN_NONE(); |
| 4784 | #endif |
| 4785 | } |
| 4786 | |
| 4787 | |
| 4788 | static PyObject* |
| 4789 | DBTxn_abort(DBTxnObject* self, PyObject* args) |
| 4790 | { |
| 4791 | int err; |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 4792 | DB_TXN *txn; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4793 | |
| 4794 | if (!PyArg_ParseTuple(args, ":abort")) |
| 4795 | return NULL; |
| 4796 | |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 4797 | if (!self->txn) { |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 4798 | PyObject *t = Py_BuildValue("(is)", 0, "DBTxn must not be used " |
| 4799 | "after txn_commit or txn_abort"); |
| 4800 | PyErr_SetObject(DBError, t); |
| 4801 | Py_DECREF(t); |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 4802 | return NULL; |
| 4803 | } |
| 4804 | txn = self->txn; |
| 4805 | self->txn = NULL; /* this DB_TXN is no longer valid after this call */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4806 | MYDB_BEGIN_ALLOW_THREADS; |
| 4807 | #if (DBVER >= 40) |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 4808 | err = txn->abort(txn); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4809 | #else |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 4810 | err = txn_abort(txn); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4811 | #endif |
| 4812 | MYDB_END_ALLOW_THREADS; |
| 4813 | RETURN_IF_ERR(); |
| 4814 | RETURN_NONE(); |
| 4815 | } |
| 4816 | |
| 4817 | |
| 4818 | static PyObject* |
| 4819 | DBTxn_id(DBTxnObject* self, PyObject* args) |
| 4820 | { |
| 4821 | int id; |
| 4822 | |
| 4823 | if (!PyArg_ParseTuple(args, ":id")) |
| 4824 | return NULL; |
| 4825 | |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 4826 | if (!self->txn) { |
Thomas Wouters | b315383 | 2006-03-08 01:47:19 +0000 | [diff] [blame] | 4827 | PyObject *t = Py_BuildValue("(is)", 0, "DBTxn must not be used " |
| 4828 | "after txn_commit or txn_abort"); |
| 4829 | PyErr_SetObject(DBError, t); |
| 4830 | Py_DECREF(t); |
Gregory P. Smith | c25fd3f | 2003-01-17 07:52:59 +0000 | [diff] [blame] | 4831 | return NULL; |
| 4832 | } |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 4833 | MYDB_BEGIN_ALLOW_THREADS; |
| 4834 | #if (DBVER >= 40) |
| 4835 | id = self->txn->id(self->txn); |
| 4836 | #else |
| 4837 | id = txn_id(self->txn); |
| 4838 | #endif |
| 4839 | MYDB_END_ALLOW_THREADS; |
| 4840 | return PyInt_FromLong(id); |
| 4841 | } |
| 4842 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4843 | #if (DBVER >= 43) |
| 4844 | /* --------------------------------------------------------------------- */ |
| 4845 | /* DBSequence methods */ |
| 4846 | |
| 4847 | |
| 4848 | static PyObject* |
| 4849 | DBSequence_close(DBSequenceObject* self, PyObject* args) |
| 4850 | { |
| 4851 | int err, flags=0; |
| 4852 | if (!PyArg_ParseTuple(args,"|i:close", &flags)) |
| 4853 | return NULL; |
| 4854 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 4855 | |
| 4856 | MYDB_BEGIN_ALLOW_THREADS |
| 4857 | err = self->sequence->close(self->sequence, flags); |
| 4858 | self->sequence = NULL; |
| 4859 | MYDB_END_ALLOW_THREADS |
| 4860 | |
| 4861 | RETURN_IF_ERR(); |
| 4862 | |
| 4863 | RETURN_NONE(); |
| 4864 | } |
| 4865 | |
| 4866 | static PyObject* |
| 4867 | DBSequence_get(DBSequenceObject* self, PyObject* args, PyObject* kwargs) |
| 4868 | { |
| 4869 | int err, flags = 0; |
| 4870 | int delta = 1; |
| 4871 | db_seq_t value; |
| 4872 | PyObject *txnobj = NULL; |
| 4873 | DB_TXN *txn = NULL; |
| 4874 | static char* kwnames[] = {"delta", "txn", "flags", NULL }; |
| 4875 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iOi:get", kwnames, &delta, &txnobj, &flags)) |
| 4876 | return NULL; |
| 4877 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 4878 | |
| 4879 | if (!checkTxnObj(txnobj, &txn)) |
| 4880 | return NULL; |
| 4881 | |
| 4882 | MYDB_BEGIN_ALLOW_THREADS |
| 4883 | err = self->sequence->get(self->sequence, txn, delta, &value, flags); |
| 4884 | MYDB_END_ALLOW_THREADS |
| 4885 | |
| 4886 | RETURN_IF_ERR(); |
| 4887 | return PyLong_FromLongLong(value); |
| 4888 | |
| 4889 | } |
| 4890 | |
| 4891 | static PyObject* |
| 4892 | DBSequence_get_dbp(DBSequenceObject* self, PyObject* args) |
| 4893 | { |
| 4894 | if (!PyArg_ParseTuple(args,":get_dbp")) |
| 4895 | return NULL; |
| 4896 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 4897 | Py_INCREF(self->mydb); |
| 4898 | return (PyObject* )self->mydb; |
| 4899 | } |
| 4900 | |
| 4901 | static PyObject* |
| 4902 | DBSequence_get_key(DBSequenceObject* self, PyObject* args) |
| 4903 | { |
| 4904 | int err; |
| 4905 | DBT key; |
| 4906 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 4907 | MYDB_BEGIN_ALLOW_THREADS |
| 4908 | err = self->sequence->get_key(self->sequence, &key); |
| 4909 | MYDB_END_ALLOW_THREADS |
| 4910 | |
| 4911 | RETURN_IF_ERR(); |
| 4912 | |
| 4913 | return PyString_FromStringAndSize(key.data, key.size); |
| 4914 | } |
| 4915 | |
| 4916 | static PyObject* |
| 4917 | DBSequence_init_value(DBSequenceObject* self, PyObject* args) |
| 4918 | { |
| 4919 | int err; |
| 4920 | db_seq_t value; |
| 4921 | if (!PyArg_ParseTuple(args,"L:init_value", &value)) |
| 4922 | return NULL; |
| 4923 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 4924 | |
| 4925 | MYDB_BEGIN_ALLOW_THREADS |
| 4926 | err = self->sequence->initial_value(self->sequence, value); |
| 4927 | MYDB_END_ALLOW_THREADS |
| 4928 | |
| 4929 | RETURN_IF_ERR(); |
| 4930 | |
| 4931 | RETURN_NONE(); |
| 4932 | } |
| 4933 | |
| 4934 | static PyObject* |
| 4935 | DBSequence_open(DBSequenceObject* self, PyObject* args, PyObject* kwargs) |
| 4936 | { |
| 4937 | int err, flags = 0; |
| 4938 | PyObject* keyobj; |
| 4939 | PyObject *txnobj = NULL; |
| 4940 | DB_TXN *txn = NULL; |
| 4941 | DBT key; |
| 4942 | |
| 4943 | static char* kwnames[] = {"key", "txn", "flags", NULL }; |
| 4944 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|Oi:open", kwnames, &keyobj, &txnobj, &flags)) |
| 4945 | return NULL; |
| 4946 | |
| 4947 | if (!checkTxnObj(txnobj, &txn)) |
| 4948 | return NULL; |
| 4949 | |
| 4950 | if (!make_key_dbt(self->mydb, keyobj, &key, NULL)) |
| 4951 | return NULL; |
| 4952 | |
| 4953 | MYDB_BEGIN_ALLOW_THREADS |
| 4954 | err = self->sequence->open(self->sequence, txn, &key, flags); |
| 4955 | MYDB_END_ALLOW_THREADS |
| 4956 | |
| 4957 | CLEAR_DBT(key); |
| 4958 | RETURN_IF_ERR(); |
| 4959 | |
| 4960 | RETURN_NONE(); |
| 4961 | } |
| 4962 | |
| 4963 | static PyObject* |
| 4964 | DBSequence_remove(DBSequenceObject* self, PyObject* args, PyObject* kwargs) |
| 4965 | { |
| 4966 | int err, flags = 0; |
| 4967 | PyObject *txnobj = NULL; |
| 4968 | DB_TXN *txn = NULL; |
| 4969 | |
| 4970 | static char* kwnames[] = {"txn", "flags", NULL }; |
| 4971 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oi:remove", kwnames, &txnobj, &flags)) |
| 4972 | return NULL; |
| 4973 | |
| 4974 | if (!checkTxnObj(txnobj, &txn)) |
| 4975 | return NULL; |
| 4976 | |
| 4977 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 4978 | |
| 4979 | MYDB_BEGIN_ALLOW_THREADS |
| 4980 | err = self->sequence->remove(self->sequence, txn, flags); |
| 4981 | MYDB_END_ALLOW_THREADS |
| 4982 | |
| 4983 | RETURN_IF_ERR(); |
| 4984 | RETURN_NONE(); |
| 4985 | } |
| 4986 | |
| 4987 | static PyObject* |
| 4988 | DBSequence_set_cachesize(DBSequenceObject* self, PyObject* args) |
| 4989 | { |
| 4990 | int err, size; |
| 4991 | if (!PyArg_ParseTuple(args,"i:set_cachesize", &size)) |
| 4992 | return NULL; |
| 4993 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 4994 | |
| 4995 | MYDB_BEGIN_ALLOW_THREADS |
| 4996 | err = self->sequence->set_cachesize(self->sequence, size); |
| 4997 | MYDB_END_ALLOW_THREADS |
| 4998 | |
| 4999 | RETURN_IF_ERR(); |
| 5000 | RETURN_NONE(); |
| 5001 | } |
| 5002 | |
| 5003 | static PyObject* |
| 5004 | DBSequence_get_cachesize(DBSequenceObject* self, PyObject* args) |
| 5005 | { |
| 5006 | int err, size; |
| 5007 | if (!PyArg_ParseTuple(args,":get_cachesize")) |
| 5008 | return NULL; |
| 5009 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 5010 | |
| 5011 | MYDB_BEGIN_ALLOW_THREADS |
| 5012 | err = self->sequence->get_cachesize(self->sequence, &size); |
| 5013 | MYDB_END_ALLOW_THREADS |
| 5014 | |
| 5015 | RETURN_IF_ERR(); |
| 5016 | return PyInt_FromLong(size); |
| 5017 | } |
| 5018 | |
| 5019 | static PyObject* |
| 5020 | DBSequence_set_flags(DBSequenceObject* self, PyObject* args) |
| 5021 | { |
| 5022 | int err, flags = 0; |
| 5023 | if (!PyArg_ParseTuple(args,"i:set_flags", &flags)) |
| 5024 | return NULL; |
| 5025 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 5026 | |
| 5027 | MYDB_BEGIN_ALLOW_THREADS |
| 5028 | err = self->sequence->set_flags(self->sequence, flags); |
| 5029 | MYDB_END_ALLOW_THREADS |
| 5030 | |
| 5031 | RETURN_IF_ERR(); |
| 5032 | RETURN_NONE(); |
| 5033 | |
| 5034 | } |
| 5035 | |
| 5036 | static PyObject* |
| 5037 | DBSequence_get_flags(DBSequenceObject* self, PyObject* args) |
| 5038 | { |
| 5039 | unsigned int flags; |
| 5040 | int err; |
| 5041 | if (!PyArg_ParseTuple(args,":get_flags")) |
| 5042 | return NULL; |
| 5043 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 5044 | |
| 5045 | MYDB_BEGIN_ALLOW_THREADS |
| 5046 | err = self->sequence->get_flags(self->sequence, &flags); |
| 5047 | MYDB_END_ALLOW_THREADS |
| 5048 | |
| 5049 | RETURN_IF_ERR(); |
| 5050 | return PyInt_FromLong((int)flags); |
| 5051 | } |
| 5052 | |
| 5053 | static PyObject* |
| 5054 | DBSequence_set_range(DBSequenceObject* self, PyObject* args) |
| 5055 | { |
| 5056 | int err; |
| 5057 | db_seq_t min, max; |
| 5058 | if (!PyArg_ParseTuple(args,"(LL):set_range", &min, &max)) |
| 5059 | return NULL; |
| 5060 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 5061 | |
| 5062 | MYDB_BEGIN_ALLOW_THREADS |
| 5063 | err = self->sequence->set_range(self->sequence, min, max); |
| 5064 | MYDB_END_ALLOW_THREADS |
| 5065 | |
| 5066 | RETURN_IF_ERR(); |
| 5067 | RETURN_NONE(); |
| 5068 | } |
| 5069 | |
| 5070 | static PyObject* |
| 5071 | DBSequence_get_range(DBSequenceObject* self, PyObject* args) |
| 5072 | { |
| 5073 | int err; |
| 5074 | db_seq_t min, max; |
| 5075 | if (!PyArg_ParseTuple(args,":get_range")) |
| 5076 | return NULL; |
| 5077 | CHECK_SEQUENCE_NOT_CLOSED(self) |
| 5078 | |
| 5079 | MYDB_BEGIN_ALLOW_THREADS |
| 5080 | err = self->sequence->get_range(self->sequence, &min, &max); |
| 5081 | MYDB_END_ALLOW_THREADS |
| 5082 | |
| 5083 | RETURN_IF_ERR(); |
| 5084 | return Py_BuildValue("(LL)", min, max); |
| 5085 | } |
| 5086 | |
| 5087 | static PyObject* |
| 5088 | DBSequence_stat(DBSequenceObject* self, PyObject* args, PyObject* kwargs) |
| 5089 | { |
| 5090 | int err, flags = 0; |
| 5091 | DB_SEQUENCE_STAT* sp = NULL; |
| 5092 | PyObject* dict_stat; |
| 5093 | static char* kwnames[] = {"flags", NULL }; |
| 5094 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:stat", kwnames, &flags)) |
| 5095 | return NULL; |
| 5096 | CHECK_SEQUENCE_NOT_CLOSED(self); |
| 5097 | |
| 5098 | MYDB_BEGIN_ALLOW_THREADS; |
| 5099 | err = self->sequence->stat(self->sequence, &sp, flags); |
| 5100 | MYDB_END_ALLOW_THREADS; |
| 5101 | RETURN_IF_ERR(); |
| 5102 | |
| 5103 | if ((dict_stat = PyDict_New()) == NULL) { |
| 5104 | free(sp); |
| 5105 | return NULL; |
| 5106 | } |
| 5107 | |
| 5108 | |
| 5109 | #define MAKE_INT_ENTRY(name) _addIntToDict(dict_stat, #name, sp->st_##name) |
| 5110 | #define MAKE_LONG_LONG_ENTRY(name) _addDb_seq_tToDict(dict_stat, #name, sp->st_##name) |
| 5111 | |
| 5112 | MAKE_INT_ENTRY(wait); |
| 5113 | MAKE_INT_ENTRY(nowait); |
| 5114 | MAKE_LONG_LONG_ENTRY(current); |
| 5115 | MAKE_LONG_LONG_ENTRY(value); |
| 5116 | MAKE_LONG_LONG_ENTRY(last_value); |
| 5117 | MAKE_LONG_LONG_ENTRY(min); |
| 5118 | MAKE_LONG_LONG_ENTRY(max); |
| 5119 | MAKE_INT_ENTRY(cache_size); |
| 5120 | MAKE_INT_ENTRY(flags); |
| 5121 | |
| 5122 | #undef MAKE_INT_ENTRY |
| 5123 | #undef MAKE_LONG_LONG_ENTRY |
| 5124 | |
| 5125 | free(sp); |
| 5126 | return dict_stat; |
| 5127 | } |
| 5128 | #endif |
| 5129 | |
| 5130 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5131 | /* --------------------------------------------------------------------- */ |
| 5132 | /* Method definition tables and type objects */ |
| 5133 | |
| 5134 | static PyMethodDef DB_methods[] = { |
| 5135 | {"append", (PyCFunction)DB_append, METH_VARARGS}, |
| 5136 | #if (DBVER >= 33) |
| 5137 | {"associate", (PyCFunction)DB_associate, METH_VARARGS|METH_KEYWORDS}, |
| 5138 | #endif |
| 5139 | {"close", (PyCFunction)DB_close, METH_VARARGS}, |
| 5140 | #if (DBVER >= 32) |
| 5141 | {"consume", (PyCFunction)DB_consume, METH_VARARGS|METH_KEYWORDS}, |
| 5142 | {"consume_wait", (PyCFunction)DB_consume_wait, METH_VARARGS|METH_KEYWORDS}, |
| 5143 | #endif |
| 5144 | {"cursor", (PyCFunction)DB_cursor, METH_VARARGS|METH_KEYWORDS}, |
| 5145 | {"delete", (PyCFunction)DB_delete, METH_VARARGS|METH_KEYWORDS}, |
| 5146 | {"fd", (PyCFunction)DB_fd, METH_VARARGS}, |
| 5147 | {"get", (PyCFunction)DB_get, METH_VARARGS|METH_KEYWORDS}, |
Gregory P. Smith | 8a6a59c | 2004-12-16 09:47:28 +0000 | [diff] [blame] | 5148 | #if (DBVER >= 33) |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 5149 | {"pget", (PyCFunction)DB_pget, METH_VARARGS|METH_KEYWORDS}, |
Gregory P. Smith | 8a6a59c | 2004-12-16 09:47:28 +0000 | [diff] [blame] | 5150 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5151 | {"get_both", (PyCFunction)DB_get_both, METH_VARARGS|METH_KEYWORDS}, |
| 5152 | {"get_byteswapped", (PyCFunction)DB_get_byteswapped,METH_VARARGS}, |
| 5153 | {"get_size", (PyCFunction)DB_get_size, METH_VARARGS|METH_KEYWORDS}, |
| 5154 | {"get_type", (PyCFunction)DB_get_type, METH_VARARGS}, |
| 5155 | {"join", (PyCFunction)DB_join, METH_VARARGS}, |
| 5156 | {"key_range", (PyCFunction)DB_key_range, METH_VARARGS|METH_KEYWORDS}, |
| 5157 | {"has_key", (PyCFunction)DB_has_key, METH_VARARGS}, |
| 5158 | {"items", (PyCFunction)DB_items, METH_VARARGS}, |
| 5159 | {"keys", (PyCFunction)DB_keys, METH_VARARGS}, |
| 5160 | {"open", (PyCFunction)DB_open, METH_VARARGS|METH_KEYWORDS}, |
| 5161 | {"put", (PyCFunction)DB_put, METH_VARARGS|METH_KEYWORDS}, |
| 5162 | {"remove", (PyCFunction)DB_remove, METH_VARARGS|METH_KEYWORDS}, |
| 5163 | {"rename", (PyCFunction)DB_rename, METH_VARARGS}, |
| 5164 | {"set_bt_minkey", (PyCFunction)DB_set_bt_minkey, METH_VARARGS}, |
Neal Norwitz | 8456235 | 2005-10-20 04:30:15 +0000 | [diff] [blame] | 5165 | #if (DBVER >= 33) |
Gregory P. Smith | e4ed2de | 2005-06-03 07:03:07 +0000 | [diff] [blame] | 5166 | {"set_bt_compare", (PyCFunction)DB_set_bt_compare, METH_VARARGS}, |
Neal Norwitz | 8456235 | 2005-10-20 04:30:15 +0000 | [diff] [blame] | 5167 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5168 | {"set_cachesize", (PyCFunction)DB_set_cachesize, METH_VARARGS}, |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 5169 | #if (DBVER >= 41) |
| 5170 | {"set_encrypt", (PyCFunction)DB_set_encrypt, METH_VARARGS|METH_KEYWORDS}, |
| 5171 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5172 | {"set_flags", (PyCFunction)DB_set_flags, METH_VARARGS}, |
| 5173 | {"set_h_ffactor", (PyCFunction)DB_set_h_ffactor, METH_VARARGS}, |
| 5174 | {"set_h_nelem", (PyCFunction)DB_set_h_nelem, METH_VARARGS}, |
| 5175 | {"set_lorder", (PyCFunction)DB_set_lorder, METH_VARARGS}, |
| 5176 | {"set_pagesize", (PyCFunction)DB_set_pagesize, METH_VARARGS}, |
| 5177 | {"set_re_delim", (PyCFunction)DB_set_re_delim, METH_VARARGS}, |
| 5178 | {"set_re_len", (PyCFunction)DB_set_re_len, METH_VARARGS}, |
| 5179 | {"set_re_pad", (PyCFunction)DB_set_re_pad, METH_VARARGS}, |
| 5180 | {"set_re_source", (PyCFunction)DB_set_re_source, METH_VARARGS}, |
| 5181 | #if (DBVER >= 32) |
| 5182 | {"set_q_extentsize",(PyCFunction)DB_set_q_extentsize,METH_VARARGS}, |
| 5183 | #endif |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 5184 | {"stat", (PyCFunction)DB_stat, METH_VARARGS|METH_KEYWORDS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5185 | {"sync", (PyCFunction)DB_sync, METH_VARARGS}, |
| 5186 | #if (DBVER >= 33) |
| 5187 | {"truncate", (PyCFunction)DB_truncate, METH_VARARGS|METH_KEYWORDS}, |
| 5188 | #endif |
| 5189 | {"type", (PyCFunction)DB_get_type, METH_VARARGS}, |
| 5190 | {"upgrade", (PyCFunction)DB_upgrade, METH_VARARGS}, |
| 5191 | {"values", (PyCFunction)DB_values, METH_VARARGS}, |
| 5192 | {"verify", (PyCFunction)DB_verify, METH_VARARGS|METH_KEYWORDS}, |
| 5193 | {"set_get_returns_none",(PyCFunction)DB_set_get_returns_none, METH_VARARGS}, |
| 5194 | {NULL, NULL} /* sentinel */ |
| 5195 | }; |
| 5196 | |
| 5197 | |
| 5198 | static PyMappingMethods DB_mapping = { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5199 | DB_length, /*mp_length*/ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5200 | (binaryfunc)DB_subscript, /*mp_subscript*/ |
| 5201 | (objobjargproc)DB_ass_sub, /*mp_ass_subscript*/ |
| 5202 | }; |
| 5203 | |
| 5204 | |
| 5205 | static PyMethodDef DBCursor_methods[] = { |
| 5206 | {"close", (PyCFunction)DBC_close, METH_VARARGS}, |
| 5207 | {"count", (PyCFunction)DBC_count, METH_VARARGS}, |
| 5208 | {"current", (PyCFunction)DBC_current, METH_VARARGS|METH_KEYWORDS}, |
| 5209 | {"delete", (PyCFunction)DBC_delete, METH_VARARGS}, |
| 5210 | {"dup", (PyCFunction)DBC_dup, METH_VARARGS}, |
| 5211 | {"first", (PyCFunction)DBC_first, METH_VARARGS|METH_KEYWORDS}, |
| 5212 | {"get", (PyCFunction)DBC_get, METH_VARARGS|METH_KEYWORDS}, |
Gregory P. Smith | 8a6a59c | 2004-12-16 09:47:28 +0000 | [diff] [blame] | 5213 | #if (DBVER >= 33) |
Gregory P. Smith | 19699a9 | 2004-06-28 04:06:49 +0000 | [diff] [blame] | 5214 | {"pget", (PyCFunction)DBC_pget, METH_VARARGS|METH_KEYWORDS}, |
Gregory P. Smith | 8a6a59c | 2004-12-16 09:47:28 +0000 | [diff] [blame] | 5215 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5216 | {"get_recno", (PyCFunction)DBC_get_recno, METH_VARARGS}, |
| 5217 | {"last", (PyCFunction)DBC_last, METH_VARARGS|METH_KEYWORDS}, |
| 5218 | {"next", (PyCFunction)DBC_next, METH_VARARGS|METH_KEYWORDS}, |
| 5219 | {"prev", (PyCFunction)DBC_prev, METH_VARARGS|METH_KEYWORDS}, |
| 5220 | {"put", (PyCFunction)DBC_put, METH_VARARGS|METH_KEYWORDS}, |
| 5221 | {"set", (PyCFunction)DBC_set, METH_VARARGS|METH_KEYWORDS}, |
| 5222 | {"set_range", (PyCFunction)DBC_set_range, METH_VARARGS|METH_KEYWORDS}, |
| 5223 | {"get_both", (PyCFunction)DBC_get_both, METH_VARARGS}, |
Gregory P. Smith | be0db8b | 2003-10-01 06:48:51 +0000 | [diff] [blame] | 5224 | {"get_current_size",(PyCFunction)DBC_get_current_size, METH_VARARGS}, |
Gregory P. Smith | 455d46f | 2003-07-09 04:45:59 +0000 | [diff] [blame] | 5225 | {"set_both", (PyCFunction)DBC_set_both, METH_VARARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5226 | {"set_recno", (PyCFunction)DBC_set_recno, METH_VARARGS|METH_KEYWORDS}, |
| 5227 | {"consume", (PyCFunction)DBC_consume, METH_VARARGS|METH_KEYWORDS}, |
| 5228 | {"next_dup", (PyCFunction)DBC_next_dup, METH_VARARGS|METH_KEYWORDS}, |
| 5229 | {"next_nodup", (PyCFunction)DBC_next_nodup, METH_VARARGS|METH_KEYWORDS}, |
| 5230 | {"prev_nodup", (PyCFunction)DBC_prev_nodup, METH_VARARGS|METH_KEYWORDS}, |
| 5231 | {"join_item", (PyCFunction)DBC_join_item, METH_VARARGS}, |
| 5232 | {NULL, NULL} /* sentinel */ |
| 5233 | }; |
| 5234 | |
| 5235 | |
| 5236 | static PyMethodDef DBEnv_methods[] = { |
| 5237 | {"close", (PyCFunction)DBEnv_close, METH_VARARGS}, |
| 5238 | {"open", (PyCFunction)DBEnv_open, METH_VARARGS}, |
| 5239 | {"remove", (PyCFunction)DBEnv_remove, METH_VARARGS}, |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 5240 | #if (DBVER >= 41) |
| 5241 | {"dbremove", (PyCFunction)DBEnv_dbremove, METH_VARARGS|METH_KEYWORDS}, |
| 5242 | {"dbrename", (PyCFunction)DBEnv_dbrename, METH_VARARGS|METH_KEYWORDS}, |
| 5243 | {"set_encrypt", (PyCFunction)DBEnv_set_encrypt, METH_VARARGS|METH_KEYWORDS}, |
| 5244 | #endif |
Gregory P. Smith | fe11d3e | 2003-03-27 17:23:29 +0000 | [diff] [blame] | 5245 | #if (DBVER >= 40) |
| 5246 | {"set_timeout", (PyCFunction)DBEnv_set_timeout, METH_VARARGS|METH_KEYWORDS}, |
| 5247 | #endif |
Gregory P. Smith | 6676f6e | 2003-08-28 21:50:30 +0000 | [diff] [blame] | 5248 | {"set_shm_key", (PyCFunction)DBEnv_set_shm_key, METH_VARARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5249 | {"set_cachesize", (PyCFunction)DBEnv_set_cachesize, METH_VARARGS}, |
| 5250 | {"set_data_dir", (PyCFunction)DBEnv_set_data_dir, METH_VARARGS}, |
| 5251 | #if (DBVER >= 32) |
| 5252 | {"set_flags", (PyCFunction)DBEnv_set_flags, METH_VARARGS}, |
| 5253 | #endif |
| 5254 | {"set_lg_bsize", (PyCFunction)DBEnv_set_lg_bsize, METH_VARARGS}, |
| 5255 | {"set_lg_dir", (PyCFunction)DBEnv_set_lg_dir, METH_VARARGS}, |
| 5256 | {"set_lg_max", (PyCFunction)DBEnv_set_lg_max, METH_VARARGS}, |
Neal Norwitz | 8456235 | 2005-10-20 04:30:15 +0000 | [diff] [blame] | 5257 | #if (DBVER >= 33) |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 5258 | {"set_lg_regionmax",(PyCFunction)DBEnv_set_lg_regionmax, METH_VARARGS}, |
Neal Norwitz | 8456235 | 2005-10-20 04:30:15 +0000 | [diff] [blame] | 5259 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5260 | {"set_lk_detect", (PyCFunction)DBEnv_set_lk_detect, METH_VARARGS}, |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 5261 | #if (DBVER < 45) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5262 | {"set_lk_max", (PyCFunction)DBEnv_set_lk_max, METH_VARARGS}, |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 5263 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5264 | #if (DBVER >= 32) |
| 5265 | {"set_lk_max_locks", (PyCFunction)DBEnv_set_lk_max_locks, METH_VARARGS}, |
| 5266 | {"set_lk_max_lockers", (PyCFunction)DBEnv_set_lk_max_lockers, METH_VARARGS}, |
| 5267 | {"set_lk_max_objects", (PyCFunction)DBEnv_set_lk_max_objects, METH_VARARGS}, |
| 5268 | #endif |
| 5269 | {"set_mp_mmapsize", (PyCFunction)DBEnv_set_mp_mmapsize, METH_VARARGS}, |
| 5270 | {"set_tmp_dir", (PyCFunction)DBEnv_set_tmp_dir, METH_VARARGS}, |
| 5271 | {"txn_begin", (PyCFunction)DBEnv_txn_begin, METH_VARARGS|METH_KEYWORDS}, |
| 5272 | {"txn_checkpoint", (PyCFunction)DBEnv_txn_checkpoint, METH_VARARGS}, |
| 5273 | {"txn_stat", (PyCFunction)DBEnv_txn_stat, METH_VARARGS}, |
| 5274 | {"set_tx_max", (PyCFunction)DBEnv_set_tx_max, METH_VARARGS}, |
Gregory P. Smith | 8a47404 | 2006-01-27 07:05:40 +0000 | [diff] [blame] | 5275 | {"set_tx_timestamp", (PyCFunction)DBEnv_set_tx_timestamp, METH_VARARGS}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5276 | {"lock_detect", (PyCFunction)DBEnv_lock_detect, METH_VARARGS}, |
| 5277 | {"lock_get", (PyCFunction)DBEnv_lock_get, METH_VARARGS}, |
| 5278 | {"lock_id", (PyCFunction)DBEnv_lock_id, METH_VARARGS}, |
| 5279 | {"lock_put", (PyCFunction)DBEnv_lock_put, METH_VARARGS}, |
| 5280 | {"lock_stat", (PyCFunction)DBEnv_lock_stat, METH_VARARGS}, |
| 5281 | {"log_archive", (PyCFunction)DBEnv_log_archive, METH_VARARGS}, |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5282 | #if (DBVER >= 40) |
| 5283 | {"log_stat", (PyCFunction)DBEnv_log_stat, METH_VARARGS}, |
| 5284 | #endif |
| 5285 | #if (DBVER >= 44) |
| 5286 | {"lsn_reset", (PyCFunction)DBEnv_lsn_reset, METH_VARARGS|METH_KEYWORDS}, |
| 5287 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5288 | {"set_get_returns_none",(PyCFunction)DBEnv_set_get_returns_none, METH_VARARGS}, |
| 5289 | {NULL, NULL} /* sentinel */ |
| 5290 | }; |
| 5291 | |
| 5292 | |
| 5293 | static PyMethodDef DBTxn_methods[] = { |
| 5294 | {"commit", (PyCFunction)DBTxn_commit, METH_VARARGS}, |
| 5295 | {"prepare", (PyCFunction)DBTxn_prepare, METH_VARARGS}, |
| 5296 | {"abort", (PyCFunction)DBTxn_abort, METH_VARARGS}, |
| 5297 | {"id", (PyCFunction)DBTxn_id, METH_VARARGS}, |
| 5298 | {NULL, NULL} /* sentinel */ |
| 5299 | }; |
| 5300 | |
| 5301 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5302 | #if (DBVER >= 43) |
| 5303 | static PyMethodDef DBSequence_methods[] = { |
| 5304 | {"close", (PyCFunction)DBSequence_close, METH_VARARGS}, |
| 5305 | {"get", (PyCFunction)DBSequence_get, METH_VARARGS|METH_KEYWORDS}, |
| 5306 | {"get_dbp", (PyCFunction)DBSequence_get_dbp, METH_VARARGS}, |
| 5307 | {"get_key", (PyCFunction)DBSequence_get_key, METH_VARARGS}, |
| 5308 | {"init_value", (PyCFunction)DBSequence_init_value, METH_VARARGS}, |
| 5309 | {"open", (PyCFunction)DBSequence_open, METH_VARARGS|METH_KEYWORDS}, |
| 5310 | {"remove", (PyCFunction)DBSequence_remove, METH_VARARGS|METH_KEYWORDS}, |
| 5311 | {"set_cachesize", (PyCFunction)DBSequence_set_cachesize, METH_VARARGS}, |
| 5312 | {"get_cachesize", (PyCFunction)DBSequence_get_cachesize, METH_VARARGS}, |
| 5313 | {"set_flags", (PyCFunction)DBSequence_set_flags, METH_VARARGS}, |
| 5314 | {"get_flags", (PyCFunction)DBSequence_get_flags, METH_VARARGS}, |
| 5315 | {"set_range", (PyCFunction)DBSequence_set_range, METH_VARARGS}, |
| 5316 | {"get_range", (PyCFunction)DBSequence_get_range, METH_VARARGS}, |
| 5317 | {"stat", (PyCFunction)DBSequence_stat, METH_VARARGS|METH_KEYWORDS}, |
| 5318 | {NULL, NULL} /* sentinel */ |
| 5319 | }; |
| 5320 | #endif |
| 5321 | |
| 5322 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5323 | static PyObject* |
| 5324 | DB_getattr(DBObject* self, char *name) |
| 5325 | { |
| 5326 | return Py_FindMethod(DB_methods, (PyObject* )self, name); |
| 5327 | } |
| 5328 | |
| 5329 | |
| 5330 | static PyObject* |
| 5331 | DBEnv_getattr(DBEnvObject* self, char *name) |
| 5332 | { |
| 5333 | if (!strcmp(name, "db_home")) { |
| 5334 | CHECK_ENV_NOT_CLOSED(self); |
| 5335 | if (self->db_env->db_home == NULL) { |
| 5336 | RETURN_NONE(); |
| 5337 | } |
| 5338 | return PyString_FromString(self->db_env->db_home); |
| 5339 | } |
| 5340 | |
| 5341 | return Py_FindMethod(DBEnv_methods, (PyObject* )self, name); |
| 5342 | } |
| 5343 | |
| 5344 | |
| 5345 | static PyObject* |
| 5346 | DBCursor_getattr(DBCursorObject* self, char *name) |
| 5347 | { |
| 5348 | return Py_FindMethod(DBCursor_methods, (PyObject* )self, name); |
| 5349 | } |
| 5350 | |
| 5351 | static PyObject* |
| 5352 | DBTxn_getattr(DBTxnObject* self, char *name) |
| 5353 | { |
| 5354 | return Py_FindMethod(DBTxn_methods, (PyObject* )self, name); |
| 5355 | } |
| 5356 | |
| 5357 | static PyObject* |
| 5358 | DBLock_getattr(DBLockObject* self, char *name) |
| 5359 | { |
| 5360 | return NULL; |
| 5361 | } |
| 5362 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5363 | #if (DBVER >= 43) |
| 5364 | static PyObject* |
| 5365 | DBSequence_getattr(DBSequenceObject* self, char *name) |
| 5366 | { |
| 5367 | return Py_FindMethod(DBSequence_methods, (PyObject* )self, name); |
| 5368 | } |
| 5369 | #endif |
| 5370 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 5371 | static PyTypeObject DB_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame^] | 5372 | PyVarObject_HEAD_INIT(NULL, 0) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5373 | "DB", /*tp_name*/ |
| 5374 | sizeof(DBObject), /*tp_basicsize*/ |
| 5375 | 0, /*tp_itemsize*/ |
| 5376 | /* methods */ |
| 5377 | (destructor)DB_dealloc, /*tp_dealloc*/ |
| 5378 | 0, /*tp_print*/ |
| 5379 | (getattrfunc)DB_getattr, /*tp_getattr*/ |
| 5380 | 0, /*tp_setattr*/ |
| 5381 | 0, /*tp_compare*/ |
| 5382 | 0, /*tp_repr*/ |
| 5383 | 0, /*tp_as_number*/ |
| 5384 | 0, /*tp_as_sequence*/ |
| 5385 | &DB_mapping,/*tp_as_mapping*/ |
| 5386 | 0, /*tp_hash*/ |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 5387 | #ifdef HAVE_WEAKREF |
| 5388 | 0, /* tp_call */ |
| 5389 | 0, /* tp_str */ |
| 5390 | 0, /* tp_getattro */ |
| 5391 | 0, /* tp_setattro */ |
| 5392 | 0, /* tp_as_buffer */ |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 5393 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 5394 | 0, /* tp_doc */ |
| 5395 | 0, /* tp_traverse */ |
| 5396 | 0, /* tp_clear */ |
| 5397 | 0, /* tp_richcompare */ |
| 5398 | offsetof(DBObject, in_weakreflist), /* tp_weaklistoffset */ |
| 5399 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5400 | }; |
| 5401 | |
| 5402 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 5403 | static PyTypeObject DBCursor_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame^] | 5404 | PyVarObject_HEAD_INIT(NULL, 0) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5405 | "DBCursor", /*tp_name*/ |
| 5406 | sizeof(DBCursorObject), /*tp_basicsize*/ |
| 5407 | 0, /*tp_itemsize*/ |
| 5408 | /* methods */ |
| 5409 | (destructor)DBCursor_dealloc,/*tp_dealloc*/ |
| 5410 | 0, /*tp_print*/ |
| 5411 | (getattrfunc)DBCursor_getattr, /*tp_getattr*/ |
| 5412 | 0, /*tp_setattr*/ |
| 5413 | 0, /*tp_compare*/ |
| 5414 | 0, /*tp_repr*/ |
| 5415 | 0, /*tp_as_number*/ |
| 5416 | 0, /*tp_as_sequence*/ |
| 5417 | 0, /*tp_as_mapping*/ |
| 5418 | 0, /*tp_hash*/ |
Gregory P. Smith | a703a21 | 2003-11-03 01:04:41 +0000 | [diff] [blame] | 5419 | #ifdef HAVE_WEAKREF |
| 5420 | 0, /* tp_call */ |
| 5421 | 0, /* tp_str */ |
| 5422 | 0, /* tp_getattro */ |
| 5423 | 0, /* tp_setattro */ |
| 5424 | 0, /* tp_as_buffer */ |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 5425 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Gregory P. Smith | a703a21 | 2003-11-03 01:04:41 +0000 | [diff] [blame] | 5426 | 0, /* tp_doc */ |
| 5427 | 0, /* tp_traverse */ |
| 5428 | 0, /* tp_clear */ |
| 5429 | 0, /* tp_richcompare */ |
| 5430 | offsetof(DBCursorObject, in_weakreflist), /* tp_weaklistoffset */ |
| 5431 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5432 | }; |
| 5433 | |
| 5434 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 5435 | static PyTypeObject DBEnv_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame^] | 5436 | PyVarObject_HEAD_INIT(NULL, 0) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5437 | "DBEnv", /*tp_name*/ |
| 5438 | sizeof(DBEnvObject), /*tp_basicsize*/ |
| 5439 | 0, /*tp_itemsize*/ |
| 5440 | /* methods */ |
| 5441 | (destructor)DBEnv_dealloc, /*tp_dealloc*/ |
| 5442 | 0, /*tp_print*/ |
| 5443 | (getattrfunc)DBEnv_getattr, /*tp_getattr*/ |
| 5444 | 0, /*tp_setattr*/ |
| 5445 | 0, /*tp_compare*/ |
| 5446 | 0, /*tp_repr*/ |
| 5447 | 0, /*tp_as_number*/ |
| 5448 | 0, /*tp_as_sequence*/ |
| 5449 | 0, /*tp_as_mapping*/ |
| 5450 | 0, /*tp_hash*/ |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 5451 | #ifdef HAVE_WEAKREF |
| 5452 | 0, /* tp_call */ |
| 5453 | 0, /* tp_str */ |
| 5454 | 0, /* tp_getattro */ |
| 5455 | 0, /* tp_setattro */ |
| 5456 | 0, /* tp_as_buffer */ |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 5457 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 5458 | 0, /* tp_doc */ |
| 5459 | 0, /* tp_traverse */ |
| 5460 | 0, /* tp_clear */ |
| 5461 | 0, /* tp_richcompare */ |
| 5462 | offsetof(DBEnvObject, in_weakreflist), /* tp_weaklistoffset */ |
| 5463 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5464 | }; |
| 5465 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 5466 | static PyTypeObject DBTxn_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame^] | 5467 | PyVarObject_HEAD_INIT(NULL, 0) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5468 | "DBTxn", /*tp_name*/ |
| 5469 | sizeof(DBTxnObject), /*tp_basicsize*/ |
| 5470 | 0, /*tp_itemsize*/ |
| 5471 | /* methods */ |
| 5472 | (destructor)DBTxn_dealloc, /*tp_dealloc*/ |
| 5473 | 0, /*tp_print*/ |
| 5474 | (getattrfunc)DBTxn_getattr, /*tp_getattr*/ |
| 5475 | 0, /*tp_setattr*/ |
| 5476 | 0, /*tp_compare*/ |
| 5477 | 0, /*tp_repr*/ |
| 5478 | 0, /*tp_as_number*/ |
| 5479 | 0, /*tp_as_sequence*/ |
| 5480 | 0, /*tp_as_mapping*/ |
| 5481 | 0, /*tp_hash*/ |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 5482 | #ifdef HAVE_WEAKREF |
| 5483 | 0, /* tp_call */ |
| 5484 | 0, /* tp_str */ |
| 5485 | 0, /* tp_getattro */ |
| 5486 | 0, /* tp_setattro */ |
| 5487 | 0, /* tp_as_buffer */ |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 5488 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 5489 | 0, /* tp_doc */ |
| 5490 | 0, /* tp_traverse */ |
| 5491 | 0, /* tp_clear */ |
| 5492 | 0, /* tp_richcompare */ |
| 5493 | offsetof(DBTxnObject, in_weakreflist), /* tp_weaklistoffset */ |
| 5494 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5495 | }; |
| 5496 | |
| 5497 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 5498 | static PyTypeObject DBLock_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame^] | 5499 | PyVarObject_HEAD_INIT(NULL, 0) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5500 | "DBLock", /*tp_name*/ |
| 5501 | sizeof(DBLockObject), /*tp_basicsize*/ |
| 5502 | 0, /*tp_itemsize*/ |
| 5503 | /* methods */ |
| 5504 | (destructor)DBLock_dealloc, /*tp_dealloc*/ |
| 5505 | 0, /*tp_print*/ |
| 5506 | (getattrfunc)DBLock_getattr, /*tp_getattr*/ |
| 5507 | 0, /*tp_setattr*/ |
| 5508 | 0, /*tp_compare*/ |
| 5509 | 0, /*tp_repr*/ |
| 5510 | 0, /*tp_as_number*/ |
| 5511 | 0, /*tp_as_sequence*/ |
| 5512 | 0, /*tp_as_mapping*/ |
| 5513 | 0, /*tp_hash*/ |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 5514 | #ifdef HAVE_WEAKREF |
| 5515 | 0, /* tp_call */ |
| 5516 | 0, /* tp_str */ |
| 5517 | 0, /* tp_getattro */ |
| 5518 | 0, /* tp_setattro */ |
| 5519 | 0, /* tp_as_buffer */ |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 5520 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Gregory P. Smith | 31c5065 | 2004-06-28 01:20:40 +0000 | [diff] [blame] | 5521 | 0, /* tp_doc */ |
| 5522 | 0, /* tp_traverse */ |
| 5523 | 0, /* tp_clear */ |
| 5524 | 0, /* tp_richcompare */ |
| 5525 | offsetof(DBLockObject, in_weakreflist), /* tp_weaklistoffset */ |
| 5526 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5527 | }; |
| 5528 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5529 | #if (DBVER >= 43) |
| 5530 | static PyTypeObject DBSequence_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame^] | 5531 | PyVarObject_HEAD_INIT(NULL, 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5532 | "DBSequence", /*tp_name*/ |
| 5533 | sizeof(DBSequenceObject), /*tp_basicsize*/ |
| 5534 | 0, /*tp_itemsize*/ |
| 5535 | /* methods */ |
| 5536 | (destructor)DBSequence_dealloc, /*tp_dealloc*/ |
| 5537 | 0, /*tp_print*/ |
| 5538 | (getattrfunc)DBSequence_getattr,/*tp_getattr*/ |
| 5539 | 0, /*tp_setattr*/ |
| 5540 | 0, /*tp_compare*/ |
| 5541 | 0, /*tp_repr*/ |
| 5542 | 0, /*tp_as_number*/ |
| 5543 | 0, /*tp_as_sequence*/ |
| 5544 | 0, /*tp_as_mapping*/ |
| 5545 | 0, /*tp_hash*/ |
| 5546 | #ifdef HAVE_WEAKREF |
| 5547 | 0, /* tp_call */ |
| 5548 | 0, /* tp_str */ |
| 5549 | 0, /* tp_getattro */ |
| 5550 | 0, /* tp_setattro */ |
| 5551 | 0, /* tp_as_buffer */ |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 5552 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5553 | 0, /* tp_doc */ |
| 5554 | 0, /* tp_traverse */ |
| 5555 | 0, /* tp_clear */ |
| 5556 | 0, /* tp_richcompare */ |
| 5557 | offsetof(DBSequenceObject, in_weakreflist), /* tp_weaklistoffset */ |
| 5558 | #endif |
| 5559 | }; |
| 5560 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5561 | |
| 5562 | /* --------------------------------------------------------------------- */ |
| 5563 | /* Module-level functions */ |
| 5564 | |
| 5565 | static PyObject* |
| 5566 | DB_construct(PyObject* self, PyObject* args, PyObject* kwargs) |
| 5567 | { |
| 5568 | PyObject* dbenvobj = NULL; |
| 5569 | int flags = 0; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 5570 | static char* kwnames[] = { "dbEnv", "flags", NULL}; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5571 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 5572 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oi:DB", kwnames, |
| 5573 | &dbenvobj, &flags)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5574 | return NULL; |
| 5575 | if (dbenvobj == Py_None) |
| 5576 | dbenvobj = NULL; |
| 5577 | else if (dbenvobj && !DBEnvObject_Check(dbenvobj)) { |
| 5578 | makeTypeError("DBEnv", dbenvobj); |
| 5579 | return NULL; |
| 5580 | } |
| 5581 | |
| 5582 | return (PyObject* )newDBObject((DBEnvObject*)dbenvobj, flags); |
| 5583 | } |
| 5584 | |
| 5585 | |
| 5586 | static PyObject* |
| 5587 | DBEnv_construct(PyObject* self, PyObject* args) |
| 5588 | { |
| 5589 | int flags = 0; |
| 5590 | if (!PyArg_ParseTuple(args, "|i:DbEnv", &flags)) return NULL; |
| 5591 | return (PyObject* )newDBEnvObject(flags); |
| 5592 | } |
| 5593 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5594 | #if (DBVER >= 43) |
| 5595 | static PyObject* |
| 5596 | DBSequence_construct(PyObject* self, PyObject* args, PyObject* kwargs) |
| 5597 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5598 | PyObject* dbobj; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5599 | int flags = 0; |
| 5600 | static char* kwnames[] = { "db", "flags", NULL}; |
| 5601 | |
| 5602 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:DBSequence", kwnames, &dbobj, &flags)) |
| 5603 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5604 | if (!DBObject_Check(dbobj)) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5605 | makeTypeError("DB", dbobj); |
| 5606 | return NULL; |
| 5607 | } |
| 5608 | return (PyObject* )newDBSequenceObject((DBObject*)dbobj, flags); |
| 5609 | } |
| 5610 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5611 | |
| 5612 | static char bsddb_version_doc[] = |
| 5613 | "Returns a tuple of major, minor, and patch release numbers of the\n\ |
| 5614 | underlying DB library."; |
| 5615 | |
| 5616 | static PyObject* |
| 5617 | bsddb_version(PyObject* self, PyObject* args) |
| 5618 | { |
| 5619 | int major, minor, patch; |
| 5620 | |
| 5621 | if (!PyArg_ParseTuple(args, ":version")) |
| 5622 | return NULL; |
| 5623 | db_version(&major, &minor, &patch); |
| 5624 | return Py_BuildValue("(iii)", major, minor, patch); |
| 5625 | } |
| 5626 | |
| 5627 | |
| 5628 | /* List of functions defined in the module */ |
| 5629 | |
| 5630 | static PyMethodDef bsddb_methods[] = { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5631 | {"DB", (PyCFunction)DB_construct, METH_VARARGS | METH_KEYWORDS }, |
| 5632 | {"DBEnv", (PyCFunction)DBEnv_construct, METH_VARARGS}, |
| 5633 | #if (DBVER >= 43) |
| 5634 | {"DBSequence", (PyCFunction)DBSequence_construct, METH_VARARGS | METH_KEYWORDS }, |
| 5635 | #endif |
| 5636 | {"version", (PyCFunction)bsddb_version, METH_VARARGS, bsddb_version_doc}, |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5637 | {NULL, NULL} /* sentinel */ |
| 5638 | }; |
| 5639 | |
| 5640 | |
| 5641 | /* --------------------------------------------------------------------- */ |
| 5642 | /* Module initialization */ |
| 5643 | |
| 5644 | |
| 5645 | /* Convenience routine to export an integer value. |
| 5646 | * Errors are silently ignored, for better or for worse... |
| 5647 | */ |
| 5648 | #define ADD_INT(dict, NAME) _addIntToDict(dict, #NAME, NAME) |
| 5649 | |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 5650 | #define MODULE_NAME_MAX_LEN 11 |
| 5651 | static char _bsddbModuleName[MODULE_NAME_MAX_LEN+1] = "_bsddb"; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5652 | |
Neal Norwitz | f6657e6 | 2006-12-28 04:47:50 +0000 | [diff] [blame] | 5653 | PyMODINIT_FUNC init_bsddb(void) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5654 | { |
| 5655 | PyObject* m; |
| 5656 | PyObject* d; |
| 5657 | PyObject* pybsddb_version_s = PyString_FromString( PY_BSDDB_VERSION ); |
| 5658 | PyObject* db_version_s = PyString_FromString( DB_VERSION_STRING ); |
| 5659 | PyObject* cvsid_s = PyString_FromString( rcs_id ); |
| 5660 | |
| 5661 | /* Initialize the type of the new type objects here; doing it here |
| 5662 | is required for portability to Windows without requiring C++. */ |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame^] | 5663 | Py_Type(&DB_Type) = &PyType_Type; |
| 5664 | Py_Type(&DBCursor_Type) = &PyType_Type; |
| 5665 | Py_Type(&DBEnv_Type) = &PyType_Type; |
| 5666 | Py_Type(&DBTxn_Type) = &PyType_Type; |
| 5667 | Py_Type(&DBLock_Type) = &PyType_Type; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5668 | #if (DBVER >= 43) |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame^] | 5669 | Py_Type(&DBSequence_Type) = &PyType_Type; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5670 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5671 | |
| 5672 | |
Mark Hammond | a69d409 | 2003-04-22 23:13:27 +0000 | [diff] [blame] | 5673 | #if defined(WITH_THREAD) && !defined(MYDB_USE_GILSTATE) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5674 | /* Save the current interpreter, so callbacks can do the right thing. */ |
Nicholas Bastin | 2786d90 | 2004-03-25 02:16:23 +0000 | [diff] [blame] | 5675 | _db_interpreterState = PyThreadState_GET()->interp; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5676 | #endif |
| 5677 | |
| 5678 | /* Create the module and add the functions */ |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 5679 | m = Py_InitModule(_bsddbModuleName, bsddb_methods); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 5680 | if (m == NULL) |
| 5681 | return; |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5682 | |
| 5683 | /* Add some symbolic constants to the module */ |
| 5684 | d = PyModule_GetDict(m); |
| 5685 | PyDict_SetItemString(d, "__version__", pybsddb_version_s); |
| 5686 | PyDict_SetItemString(d, "cvsid", cvsid_s); |
| 5687 | PyDict_SetItemString(d, "DB_VERSION_STRING", db_version_s); |
| 5688 | Py_DECREF(pybsddb_version_s); |
| 5689 | pybsddb_version_s = NULL; |
| 5690 | Py_DECREF(cvsid_s); |
| 5691 | cvsid_s = NULL; |
| 5692 | Py_DECREF(db_version_s); |
| 5693 | db_version_s = NULL; |
| 5694 | |
| 5695 | ADD_INT(d, DB_VERSION_MAJOR); |
| 5696 | ADD_INT(d, DB_VERSION_MINOR); |
| 5697 | ADD_INT(d, DB_VERSION_PATCH); |
| 5698 | |
| 5699 | ADD_INT(d, DB_MAX_PAGES); |
| 5700 | ADD_INT(d, DB_MAX_RECORDS); |
| 5701 | |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 5702 | #if (DBVER >= 42) |
| 5703 | ADD_INT(d, DB_RPCCLIENT); |
| 5704 | #else |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5705 | ADD_INT(d, DB_CLIENT); |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 5706 | /* allow apps to be written using DB_RPCCLIENT on older BerkeleyDB */ |
| 5707 | _addIntToDict(d, "DB_RPCCLIENT", DB_CLIENT); |
| 5708 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5709 | ADD_INT(d, DB_XA_CREATE); |
| 5710 | |
| 5711 | ADD_INT(d, DB_CREATE); |
| 5712 | ADD_INT(d, DB_NOMMAP); |
| 5713 | ADD_INT(d, DB_THREAD); |
| 5714 | |
| 5715 | ADD_INT(d, DB_FORCE); |
| 5716 | ADD_INT(d, DB_INIT_CDB); |
| 5717 | ADD_INT(d, DB_INIT_LOCK); |
| 5718 | ADD_INT(d, DB_INIT_LOG); |
| 5719 | ADD_INT(d, DB_INIT_MPOOL); |
| 5720 | ADD_INT(d, DB_INIT_TXN); |
| 5721 | #if (DBVER >= 32) |
| 5722 | ADD_INT(d, DB_JOINENV); |
| 5723 | #endif |
| 5724 | |
| 5725 | ADD_INT(d, DB_RECOVER); |
| 5726 | ADD_INT(d, DB_RECOVER_FATAL); |
| 5727 | ADD_INT(d, DB_TXN_NOSYNC); |
| 5728 | ADD_INT(d, DB_USE_ENVIRON); |
| 5729 | ADD_INT(d, DB_USE_ENVIRON_ROOT); |
| 5730 | |
| 5731 | ADD_INT(d, DB_LOCKDOWN); |
| 5732 | ADD_INT(d, DB_PRIVATE); |
| 5733 | ADD_INT(d, DB_SYSTEM_MEM); |
| 5734 | |
| 5735 | ADD_INT(d, DB_TXN_SYNC); |
| 5736 | ADD_INT(d, DB_TXN_NOWAIT); |
| 5737 | |
| 5738 | ADD_INT(d, DB_EXCL); |
| 5739 | ADD_INT(d, DB_FCNTL_LOCKING); |
| 5740 | ADD_INT(d, DB_ODDFILESIZE); |
| 5741 | ADD_INT(d, DB_RDWRMASTER); |
| 5742 | ADD_INT(d, DB_RDONLY); |
| 5743 | ADD_INT(d, DB_TRUNCATE); |
| 5744 | #if (DBVER >= 32) |
| 5745 | ADD_INT(d, DB_EXTENT); |
| 5746 | ADD_INT(d, DB_CDB_ALLDB); |
| 5747 | ADD_INT(d, DB_VERIFY); |
| 5748 | #endif |
| 5749 | ADD_INT(d, DB_UPGRADE); |
| 5750 | |
| 5751 | ADD_INT(d, DB_AGGRESSIVE); |
| 5752 | ADD_INT(d, DB_NOORDERCHK); |
| 5753 | ADD_INT(d, DB_ORDERCHKONLY); |
| 5754 | ADD_INT(d, DB_PR_PAGE); |
| 5755 | #if ! (DBVER >= 33) |
| 5756 | ADD_INT(d, DB_VRFY_FLAGMASK); |
| 5757 | ADD_INT(d, DB_PR_HEADERS); |
| 5758 | #endif |
| 5759 | ADD_INT(d, DB_PR_RECOVERYTEST); |
| 5760 | ADD_INT(d, DB_SALVAGE); |
| 5761 | |
| 5762 | ADD_INT(d, DB_LOCK_NORUN); |
| 5763 | ADD_INT(d, DB_LOCK_DEFAULT); |
| 5764 | ADD_INT(d, DB_LOCK_OLDEST); |
| 5765 | ADD_INT(d, DB_LOCK_RANDOM); |
| 5766 | ADD_INT(d, DB_LOCK_YOUNGEST); |
| 5767 | #if (DBVER >= 33) |
| 5768 | ADD_INT(d, DB_LOCK_MAXLOCKS); |
| 5769 | ADD_INT(d, DB_LOCK_MINLOCKS); |
| 5770 | ADD_INT(d, DB_LOCK_MINWRITE); |
| 5771 | #endif |
| 5772 | |
| 5773 | |
| 5774 | #if (DBVER >= 33) |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 5775 | /* docs say to use zero instead */ |
| 5776 | _addIntToDict(d, "DB_LOCK_CONFLICT", 0); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5777 | #else |
| 5778 | ADD_INT(d, DB_LOCK_CONFLICT); |
| 5779 | #endif |
| 5780 | |
| 5781 | ADD_INT(d, DB_LOCK_DUMP); |
| 5782 | ADD_INT(d, DB_LOCK_GET); |
| 5783 | ADD_INT(d, DB_LOCK_INHERIT); |
| 5784 | ADD_INT(d, DB_LOCK_PUT); |
| 5785 | ADD_INT(d, DB_LOCK_PUT_ALL); |
| 5786 | ADD_INT(d, DB_LOCK_PUT_OBJ); |
| 5787 | |
| 5788 | ADD_INT(d, DB_LOCK_NG); |
| 5789 | ADD_INT(d, DB_LOCK_READ); |
| 5790 | ADD_INT(d, DB_LOCK_WRITE); |
| 5791 | ADD_INT(d, DB_LOCK_NOWAIT); |
| 5792 | #if (DBVER >= 32) |
| 5793 | ADD_INT(d, DB_LOCK_WAIT); |
| 5794 | #endif |
| 5795 | ADD_INT(d, DB_LOCK_IWRITE); |
| 5796 | ADD_INT(d, DB_LOCK_IREAD); |
| 5797 | ADD_INT(d, DB_LOCK_IWR); |
| 5798 | #if (DBVER >= 33) |
Gregory P. Smith | 29602d2 | 2006-01-24 09:46:48 +0000 | [diff] [blame] | 5799 | #if (DBVER < 44) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5800 | ADD_INT(d, DB_LOCK_DIRTY); |
Gregory P. Smith | 29602d2 | 2006-01-24 09:46:48 +0000 | [diff] [blame] | 5801 | #else |
| 5802 | ADD_INT(d, DB_LOCK_READ_UNCOMMITTED); /* renamed in 4.4 */ |
| 5803 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5804 | ADD_INT(d, DB_LOCK_WWRITE); |
| 5805 | #endif |
| 5806 | |
| 5807 | ADD_INT(d, DB_LOCK_RECORD); |
| 5808 | ADD_INT(d, DB_LOCK_UPGRADE); |
| 5809 | #if (DBVER >= 32) |
| 5810 | ADD_INT(d, DB_LOCK_SWITCH); |
| 5811 | #endif |
| 5812 | #if (DBVER >= 33) |
| 5813 | ADD_INT(d, DB_LOCK_UPGRADE_WRITE); |
| 5814 | #endif |
| 5815 | |
| 5816 | ADD_INT(d, DB_LOCK_NOWAIT); |
| 5817 | ADD_INT(d, DB_LOCK_RECORD); |
| 5818 | ADD_INT(d, DB_LOCK_UPGRADE); |
| 5819 | |
| 5820 | #if (DBVER >= 33) |
| 5821 | ADD_INT(d, DB_LSTAT_ABORTED); |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 5822 | #if (DBVER < 43) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5823 | ADD_INT(d, DB_LSTAT_ERR); |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 5824 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5825 | ADD_INT(d, DB_LSTAT_FREE); |
| 5826 | ADD_INT(d, DB_LSTAT_HELD); |
| 5827 | #if (DBVER == 33) |
| 5828 | ADD_INT(d, DB_LSTAT_NOGRANT); |
| 5829 | #endif |
| 5830 | ADD_INT(d, DB_LSTAT_PENDING); |
| 5831 | ADD_INT(d, DB_LSTAT_WAITING); |
| 5832 | #endif |
| 5833 | |
| 5834 | ADD_INT(d, DB_ARCH_ABS); |
| 5835 | ADD_INT(d, DB_ARCH_DATA); |
| 5836 | ADD_INT(d, DB_ARCH_LOG); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5837 | #if (DBVER >= 42) |
| 5838 | ADD_INT(d, DB_ARCH_REMOVE); |
| 5839 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5840 | |
| 5841 | ADD_INT(d, DB_BTREE); |
| 5842 | ADD_INT(d, DB_HASH); |
| 5843 | ADD_INT(d, DB_RECNO); |
| 5844 | ADD_INT(d, DB_QUEUE); |
| 5845 | ADD_INT(d, DB_UNKNOWN); |
| 5846 | |
| 5847 | ADD_INT(d, DB_DUP); |
| 5848 | ADD_INT(d, DB_DUPSORT); |
| 5849 | ADD_INT(d, DB_RECNUM); |
| 5850 | ADD_INT(d, DB_RENUMBER); |
| 5851 | ADD_INT(d, DB_REVSPLITOFF); |
| 5852 | ADD_INT(d, DB_SNAPSHOT); |
| 5853 | |
| 5854 | ADD_INT(d, DB_JOIN_NOSORT); |
| 5855 | |
| 5856 | ADD_INT(d, DB_AFTER); |
| 5857 | ADD_INT(d, DB_APPEND); |
| 5858 | ADD_INT(d, DB_BEFORE); |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 5859 | #if (DBVER < 45) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5860 | ADD_INT(d, DB_CACHED_COUNTS); |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 5861 | #endif |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 5862 | #if (DBVER >= 41) |
| 5863 | _addIntToDict(d, "DB_CHECKPOINT", 0); |
| 5864 | #else |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5865 | ADD_INT(d, DB_CHECKPOINT); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 5866 | ADD_INT(d, DB_CURLSN); |
| 5867 | #endif |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 5868 | #if ((DBVER >= 33) && (DBVER <= 41)) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5869 | ADD_INT(d, DB_COMMIT); |
| 5870 | #endif |
| 5871 | ADD_INT(d, DB_CONSUME); |
| 5872 | #if (DBVER >= 32) |
| 5873 | ADD_INT(d, DB_CONSUME_WAIT); |
| 5874 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5875 | ADD_INT(d, DB_CURRENT); |
| 5876 | #if (DBVER >= 33) |
| 5877 | ADD_INT(d, DB_FAST_STAT); |
| 5878 | #endif |
| 5879 | ADD_INT(d, DB_FIRST); |
| 5880 | ADD_INT(d, DB_FLUSH); |
| 5881 | ADD_INT(d, DB_GET_BOTH); |
| 5882 | ADD_INT(d, DB_GET_RECNO); |
| 5883 | ADD_INT(d, DB_JOIN_ITEM); |
| 5884 | ADD_INT(d, DB_KEYFIRST); |
| 5885 | ADD_INT(d, DB_KEYLAST); |
| 5886 | ADD_INT(d, DB_LAST); |
| 5887 | ADD_INT(d, DB_NEXT); |
| 5888 | ADD_INT(d, DB_NEXT_DUP); |
| 5889 | ADD_INT(d, DB_NEXT_NODUP); |
| 5890 | ADD_INT(d, DB_NODUPDATA); |
| 5891 | ADD_INT(d, DB_NOOVERWRITE); |
| 5892 | ADD_INT(d, DB_NOSYNC); |
| 5893 | ADD_INT(d, DB_POSITION); |
| 5894 | ADD_INT(d, DB_PREV); |
| 5895 | ADD_INT(d, DB_PREV_NODUP); |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 5896 | #if (DBVER < 45) |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5897 | ADD_INT(d, DB_RECORDCOUNT); |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 5898 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5899 | ADD_INT(d, DB_SET); |
| 5900 | ADD_INT(d, DB_SET_RANGE); |
| 5901 | ADD_INT(d, DB_SET_RECNO); |
| 5902 | ADD_INT(d, DB_WRITECURSOR); |
| 5903 | |
| 5904 | ADD_INT(d, DB_OPFLAGS_MASK); |
| 5905 | ADD_INT(d, DB_RMW); |
| 5906 | #if (DBVER >= 33) |
| 5907 | ADD_INT(d, DB_DIRTY_READ); |
| 5908 | ADD_INT(d, DB_MULTIPLE); |
| 5909 | ADD_INT(d, DB_MULTIPLE_KEY); |
| 5910 | #endif |
| 5911 | |
Gregory P. Smith | 29602d2 | 2006-01-24 09:46:48 +0000 | [diff] [blame] | 5912 | #if (DBVER >= 44) |
| 5913 | ADD_INT(d, DB_READ_UNCOMMITTED); /* replaces DB_DIRTY_READ in 4.4 */ |
| 5914 | ADD_INT(d, DB_READ_COMMITTED); |
| 5915 | #endif |
| 5916 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5917 | #if (DBVER >= 33) |
| 5918 | ADD_INT(d, DB_DONOTINDEX); |
| 5919 | #endif |
| 5920 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 5921 | #if (DBVER >= 41) |
| 5922 | _addIntToDict(d, "DB_INCOMPLETE", 0); |
| 5923 | #else |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5924 | ADD_INT(d, DB_INCOMPLETE); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 5925 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5926 | ADD_INT(d, DB_KEYEMPTY); |
| 5927 | ADD_INT(d, DB_KEYEXIST); |
| 5928 | ADD_INT(d, DB_LOCK_DEADLOCK); |
| 5929 | ADD_INT(d, DB_LOCK_NOTGRANTED); |
| 5930 | ADD_INT(d, DB_NOSERVER); |
| 5931 | ADD_INT(d, DB_NOSERVER_HOME); |
| 5932 | ADD_INT(d, DB_NOSERVER_ID); |
| 5933 | ADD_INT(d, DB_NOTFOUND); |
| 5934 | ADD_INT(d, DB_OLD_VERSION); |
| 5935 | ADD_INT(d, DB_RUNRECOVERY); |
| 5936 | ADD_INT(d, DB_VERIFY_BAD); |
| 5937 | #if (DBVER >= 33) |
| 5938 | ADD_INT(d, DB_PAGE_NOTFOUND); |
| 5939 | ADD_INT(d, DB_SECONDARY_BAD); |
| 5940 | #endif |
| 5941 | #if (DBVER >= 40) |
| 5942 | ADD_INT(d, DB_STAT_CLEAR); |
| 5943 | ADD_INT(d, DB_REGION_INIT); |
| 5944 | ADD_INT(d, DB_NOLOCKING); |
| 5945 | ADD_INT(d, DB_YIELDCPU); |
| 5946 | ADD_INT(d, DB_PANIC_ENVIRONMENT); |
| 5947 | ADD_INT(d, DB_NOPANIC); |
| 5948 | #endif |
| 5949 | |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 5950 | #if (DBVER >= 42) |
| 5951 | ADD_INT(d, DB_TIME_NOTGRANTED); |
| 5952 | ADD_INT(d, DB_TXN_NOT_DURABLE); |
| 5953 | ADD_INT(d, DB_TXN_WRITE_NOSYNC); |
| 5954 | ADD_INT(d, DB_LOG_AUTOREMOVE); |
| 5955 | ADD_INT(d, DB_DIRECT_LOG); |
| 5956 | ADD_INT(d, DB_DIRECT_DB); |
| 5957 | ADD_INT(d, DB_INIT_REP); |
| 5958 | ADD_INT(d, DB_ENCRYPT); |
| 5959 | ADD_INT(d, DB_CHKSUM); |
| 5960 | #endif |
| 5961 | |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 5962 | #if (DBVER >= 43) |
| 5963 | ADD_INT(d, DB_LOG_INMEMORY); |
| 5964 | ADD_INT(d, DB_BUFFER_SMALL); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5965 | ADD_INT(d, DB_SEQ_DEC); |
| 5966 | ADD_INT(d, DB_SEQ_INC); |
| 5967 | ADD_INT(d, DB_SEQ_WRAP); |
Gregory P. Smith | 8b7e917 | 2004-12-13 09:51:23 +0000 | [diff] [blame] | 5968 | #endif |
| 5969 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 5970 | #if (DBVER >= 41) |
| 5971 | ADD_INT(d, DB_ENCRYPT_AES); |
| 5972 | ADD_INT(d, DB_AUTO_COMMIT); |
| 5973 | #else |
| 5974 | /* allow berkeleydb 4.1 aware apps to run on older versions */ |
| 5975 | _addIntToDict(d, "DB_AUTO_COMMIT", 0); |
| 5976 | #endif |
| 5977 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5978 | ADD_INT(d, EINVAL); |
| 5979 | ADD_INT(d, EACCES); |
| 5980 | ADD_INT(d, ENOSPC); |
| 5981 | ADD_INT(d, ENOMEM); |
| 5982 | ADD_INT(d, EAGAIN); |
| 5983 | ADD_INT(d, EBUSY); |
| 5984 | ADD_INT(d, EEXIST); |
| 5985 | ADD_INT(d, ENOENT); |
| 5986 | ADD_INT(d, EPERM); |
| 5987 | |
Barry Warsaw | 1baa982 | 2003-03-31 19:51:29 +0000 | [diff] [blame] | 5988 | #if (DBVER >= 40) |
| 5989 | ADD_INT(d, DB_SET_LOCK_TIMEOUT); |
| 5990 | ADD_INT(d, DB_SET_TXN_TIMEOUT); |
| 5991 | #endif |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 5992 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5993 | /* The exception name must be correct for pickled exception * |
| 5994 | * objects to unpickle properly. */ |
| 5995 | #ifdef PYBSDDB_STANDALONE /* different value needed for standalone pybsddb */ |
| 5996 | #define PYBSDDB_EXCEPTION_BASE "bsddb3.db." |
| 5997 | #else |
| 5998 | #define PYBSDDB_EXCEPTION_BASE "bsddb.db." |
| 5999 | #endif |
| 6000 | |
| 6001 | /* All the rest of the exceptions derive only from DBError */ |
| 6002 | #define MAKE_EX(name) name = PyErr_NewException(PYBSDDB_EXCEPTION_BASE #name, DBError, NULL); \ |
| 6003 | PyDict_SetItemString(d, #name, name) |
| 6004 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6005 | /* The base exception class is DBError */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6006 | DBError = NULL; /* used in MAKE_EX so that it derives from nothing */ |
| 6007 | MAKE_EX(DBError); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6008 | |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 6009 | /* Some magic to make DBNotFoundError and DBKeyEmptyError derive |
| 6010 | * from both DBError and KeyError, since the API only supports |
| 6011 | * using one base class. */ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6012 | PyDict_SetItemString(d, "KeyError", PyExc_KeyError); |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 6013 | { |
| 6014 | PyObject *builtin_mod = PyImport_ImportModule("__builtin__"); |
| 6015 | PyDict_SetItemString(d, "__builtins__", builtin_mod); |
| 6016 | } |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 6017 | PyRun_String("class DBNotFoundError(DBError, KeyError): pass\n" |
| 6018 | "class DBKeyEmptyError(DBError, KeyError): pass", |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6019 | Py_file_input, d, d); |
| 6020 | DBNotFoundError = PyDict_GetItemString(d, "DBNotFoundError"); |
Gregory P. Smith | e947706 | 2005-06-04 06:46:59 +0000 | [diff] [blame] | 6021 | DBKeyEmptyError = PyDict_GetItemString(d, "DBKeyEmptyError"); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6022 | PyDict_DelItemString(d, "KeyError"); |
| 6023 | |
| 6024 | |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6025 | #if !INCOMPLETE_IS_WARNING |
| 6026 | MAKE_EX(DBIncompleteError); |
| 6027 | #endif |
Gregory P. Smith | e276717 | 2003-11-02 08:06:29 +0000 | [diff] [blame] | 6028 | MAKE_EX(DBCursorClosedError); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6029 | MAKE_EX(DBKeyEmptyError); |
| 6030 | MAKE_EX(DBKeyExistError); |
| 6031 | MAKE_EX(DBLockDeadlockError); |
| 6032 | MAKE_EX(DBLockNotGrantedError); |
| 6033 | MAKE_EX(DBOldVersionError); |
| 6034 | MAKE_EX(DBRunRecoveryError); |
| 6035 | MAKE_EX(DBVerifyBadError); |
| 6036 | MAKE_EX(DBNoServerError); |
| 6037 | MAKE_EX(DBNoServerHomeError); |
| 6038 | MAKE_EX(DBNoServerIDError); |
| 6039 | #if (DBVER >= 33) |
| 6040 | MAKE_EX(DBPageNotFoundError); |
| 6041 | MAKE_EX(DBSecondaryBadError); |
| 6042 | #endif |
| 6043 | |
| 6044 | MAKE_EX(DBInvalidArgError); |
| 6045 | MAKE_EX(DBAccessError); |
| 6046 | MAKE_EX(DBNoSpaceError); |
| 6047 | MAKE_EX(DBNoMemoryError); |
| 6048 | MAKE_EX(DBAgainError); |
| 6049 | MAKE_EX(DBBusyError); |
| 6050 | MAKE_EX(DBFileExistsError); |
| 6051 | MAKE_EX(DBNoSuchFileError); |
| 6052 | MAKE_EX(DBPermissionsError); |
| 6053 | |
| 6054 | #undef MAKE_EX |
| 6055 | |
| 6056 | /* Check for errors */ |
| 6057 | if (PyErr_Occurred()) { |
| 6058 | PyErr_Print(); |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 6059 | Py_FatalError("can't initialize module _bsddb"); |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 6060 | } |
| 6061 | } |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 6062 | |
| 6063 | /* allow this module to be named _pybsddb so that it can be installed |
| 6064 | * and imported on top of python >= 2.3 that includes its own older |
| 6065 | * copy of the library named _bsddb without importing the old version. */ |
Neal Norwitz | f6657e6 | 2006-12-28 04:47:50 +0000 | [diff] [blame] | 6066 | PyMODINIT_FUNC init_pybsddb(void) |
Gregory P. Smith | 41631e8 | 2003-09-21 00:08:14 +0000 | [diff] [blame] | 6067 | { |
| 6068 | strncpy(_bsddbModuleName, "_pybsddb", MODULE_NAME_MAX_LEN); |
| 6069 | init_bsddb(); |
| 6070 | } |