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