PageRenderTime 89ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/Modules/_bsddb.c

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