From 41631e8f668c9c2c724731d207edcc39c6fcf38c Mon Sep 17 00:00:00 2001
From: "Gregory P. Smith" <greg@mad-scientist.com>
Date: Sun, 21 Sep 2003 00:08:14 +0000
Subject: Adds basic support for BerkeleyDB 4.2.x.  Compiles and passes tests;
 new features in BerkeleyDB not exposed.  notably: the DB_MPOOLFILE interface
 has not yet been wrapped in an object.

Adds support for building and installing bsddb3 in python2.3 that has
an older version of this module installed as bsddb without conflicts.
The pybsddb.sf.net build/packaged version of the module uses a
dynamicly loadable module called _pybsddb rather than _bsddb.
---
 Lib/bsddb/__init__.py            | 11 +++++++--
 Lib/bsddb/db.py                  | 15 ++++++++----
 Lib/bsddb/dbshelve.py            |  7 +-----
 Lib/bsddb/dbtables.py            |  6 ++---
 Lib/bsddb/dbutils.py             |  7 +-----
 Lib/bsddb/test/test_all.py       |  6 ++---
 Lib/bsddb/test/test_associate.py |  6 ++---
 Lib/bsddb/test/test_basics.py    |  6 ++---
 Lib/bsddb/test/test_compat.py    |  6 ++---
 Lib/bsddb/test/test_dbobj.py     |  6 ++---
 Lib/bsddb/test/test_dbshelve.py  |  6 ++---
 Lib/bsddb/test/test_dbtables.py  |  6 ++---
 Lib/bsddb/test/test_env_close.py |  6 ++---
 Lib/bsddb/test/test_get_none.py  |  6 ++---
 Lib/bsddb/test/test_join.py      |  6 ++---
 Lib/bsddb/test/test_lock.py      |  6 ++---
 Lib/bsddb/test/test_misc.py      |  6 ++---
 Lib/bsddb/test/test_queue.py     |  6 ++---
 Lib/bsddb/test/test_recno.py     |  6 ++---
 Lib/bsddb/test/test_thread.py    |  6 ++---
 Modules/_bsddb.c                 | 49 ++++++++++++++++++++++++++++++++++++----
 21 files changed, 114 insertions(+), 71 deletions(-)

diff --git a/Lib/bsddb/__init__.py b/Lib/bsddb/__init__.py
index 6b4f965..4671acc 100644
--- a/Lib/bsddb/__init__.py
+++ b/Lib/bsddb/__init__.py
@@ -33,11 +33,18 @@
 #----------------------------------------------------------------------
 
 
-"""Support for BerkeleyDB 3.1 through 4.1.
+"""Support for BerkeleyDB 3.2 through 4.2.
 """
 
 try:
-    import _bsddb
+    if __name__ == 'bsddb3':
+        # import _pybsddb binary as it should be the more recent version from
+        # a standalone pybsddb addon package than the version included with
+        # python as bsddb._bsddb.
+        import _pybsddb
+        _bsddb = _pybsddb
+    else:
+        import _bsddb
 except ImportError:
     # Remove ourselves from sys.modules
     import sys
diff --git a/Lib/bsddb/db.py b/Lib/bsddb/db.py
index c1749df..b2ee14e 100644
--- a/Lib/bsddb/db.py
+++ b/Lib/bsddb/db.py
@@ -37,8 +37,15 @@
 # case we ever want to augment the stuff in _db in any way.  For now
 # it just simply imports everything from _db.
 
-from _bsddb import *
-from _bsddb import __version__
+if __name__[:len('bsddb3.')] == 'bsddb3.':
+    # import _pybsddb binary as it should be the more recent version from
+    # a standalone pybsddb addon package than the version included with
+    # python as bsddb._bsddb.
+    from _pybsddb import *
+    from _pybsddb import __version__
+else:
+    from _bsddb import *
+    from _bsddb import __version__
 
-if version() < (3, 1, 0):
-    raise ImportError, "BerkeleyDB 3.x symbols not found.  Perhaps python was statically linked with an older version?"
+if version() < (3, 2, 0):
+    raise ImportError, "correct BerkeleyDB symbols not found.  Perhaps python was statically linked with an older version?"
diff --git a/Lib/bsddb/dbshelve.py b/Lib/bsddb/dbshelve.py
index fe4c4d1..003038b 100644
--- a/Lib/bsddb/dbshelve.py
+++ b/Lib/bsddb/dbshelve.py
@@ -35,12 +35,7 @@ try:
 except ImportError:
     # DictMixin is new in Python 2.3
     class DictMixin: pass
-try:
-    # For Python 2.3
-    from bsddb import db
-except ImportError:
-    # For earlier Pythons w/distutils pybsddb
-    from bsddb3 import db
+import db
 
 #------------------------------------------------------------------------
 
diff --git a/Lib/bsddb/dbtables.py b/Lib/bsddb/dbtables.py
index d052ca5..6edfd29 100644
--- a/Lib/bsddb/dbtables.py
+++ b/Lib/bsddb/dbtables.py
@@ -26,11 +26,11 @@ from types import ListType, StringType
 import cPickle as pickle
 
 try:
+    # For Pythons w/distutils pybsddb
+    from bsddb3.db import *
+except ImportError:
     # For Python 2.3
     from bsddb.db import *
-except ImportError:
-    # For earlier Pythons w/distutils pybsddb
-    from bsddb3.db import *
 
 
 class TableDBError(StandardError):
diff --git a/Lib/bsddb/dbutils.py b/Lib/bsddb/dbutils.py
index 3568b44..3f63842 100644
--- a/Lib/bsddb/dbutils.py
+++ b/Lib/bsddb/dbutils.py
@@ -26,12 +26,7 @@
 #
 from time import sleep as _sleep
 
-try:
-    # For Python 2.3
-    from bsddb import db
-except ImportError:
-    # For earlier Pythons w/distutils pybsddb
-    from bsddb3 import db
+import db
 
 # always sleep at least N seconds between retrys
 _deadlock_MinSleepTime = 1.0/64
diff --git a/Lib/bsddb/test/test_all.py b/Lib/bsddb/test/test_all.py
index 358f5d1..3bf70d7 100644
--- a/Lib/bsddb/test/test_all.py
+++ b/Lib/bsddb/test/test_all.py
@@ -17,11 +17,11 @@ if 'silent' in sys.argv:  # take care of old flag, just in case
 
 def print_versions():
     try:
+        # For Pythons w/distutils pybsddb
+        from bsddb3 import db
+    except ImportError:
         # For Python 2.3
         from bsddb import db
-    except ImportError:
-        # For earlier Pythons w/distutils pybsddb
-        from bsddb3 import db
     print
     print '-=' * 38
     print db.DB_VERSION_STRING
diff --git a/Lib/bsddb/test/test_associate.py b/Lib/bsddb/test/test_associate.py
index 5fe41a9..f810eb0 100644
--- a/Lib/bsddb/test/test_associate.py
+++ b/Lib/bsddb/test/test_associate.py
@@ -17,11 +17,11 @@ import unittest
 from test_all import verbose
 
 try:
+    # For Pythons w/distutils pybsddb
+    from bsddb3 import db, dbshelve
+except ImportError:
     # For Python 2.3
     from bsddb import db, dbshelve
-except ImportError:
-    # For earlier Pythons w/distutils pybsddb
-    from bsddb3 import db, dbshelve
 
 
 #----------------------------------------------------------------------
diff --git a/Lib/bsddb/test/test_basics.py b/Lib/bsddb/test/test_basics.py
index dbab231..2e6b922 100644
--- a/Lib/bsddb/test/test_basics.py
+++ b/Lib/bsddb/test/test_basics.py
@@ -13,11 +13,11 @@ from pprint import pprint
 import unittest
 
 try:
+    # For Pythons w/distutils pybsddb
+    from bsddb3 import db
+except ImportError:
     # For Python 2.3
     from bsddb import db
-except ImportError:
-    # For earlier Pythons w/distutils pybsddb
-    from bsddb3 import db
 
 from test_all import verbose
 
diff --git a/Lib/bsddb/test/test_compat.py b/Lib/bsddb/test/test_compat.py
index 645cbd7..12464ca 100644
--- a/Lib/bsddb/test/test_compat.py
+++ b/Lib/bsddb/test/test_compat.py
@@ -10,11 +10,11 @@ import tempfile
 from test_all import verbose
 
 try:
+    # For Pythons w/distutils pybsddb
+    from bsddb3 import db, hashopen, btopen, rnopen
+except ImportError:
     # For Python 2.3
     from bsddb import db, hashopen, btopen, rnopen
-except ImportError:
-    # For earlier Pythons w/distutils pybsddb
-    from bsddb3 import db, hashopen, btopen, rnopen
 
 
 class CompatibilityTestCase(unittest.TestCase):
diff --git a/Lib/bsddb/test/test_dbobj.py b/Lib/bsddb/test/test_dbobj.py
index 93ea7ba..6799fc9 100644
--- a/Lib/bsddb/test/test_dbobj.py
+++ b/Lib/bsddb/test/test_dbobj.py
@@ -4,11 +4,11 @@ import unittest
 import glob
 
 try:
+    # For Pythons w/distutils pybsddb
+    from bsddb3 import db, dbobj
+except ImportError:
     # For Python 2.3
     from bsddb import db, dbobj
-except ImportError:
-    # For earlier Pythons w/distutils pybsddb
-    from bsddb3 import db, dbobj
 
 
 #----------------------------------------------------------------------
diff --git a/Lib/bsddb/test/test_dbshelve.py b/Lib/bsddb/test/test_dbshelve.py
index 7491cb7..722ee5b 100644
--- a/Lib/bsddb/test/test_dbshelve.py
+++ b/Lib/bsddb/test/test_dbshelve.py
@@ -9,11 +9,11 @@ from types import *
 import unittest
 
 try:
+    # For Pythons w/distutils pybsddb
+    from bsddb3 import db, dbshelve
+except ImportError:
     # For Python 2.3
     from bsddb import db, dbshelve
-except ImportError:
-    # For earlier Pythons w/distutils pybsddb
-    from bsddb3 import db, dbshelve
 
 from test_all import verbose
 
diff --git a/Lib/bsddb/test/test_dbtables.py b/Lib/bsddb/test/test_dbtables.py
index 685b08d..eb5758f 100644
--- a/Lib/bsddb/test/test_dbtables.py
+++ b/Lib/bsddb/test/test_dbtables.py
@@ -31,11 +31,11 @@ import unittest
 from test_all import verbose
 
 try:
+    # For Pythons w/distutils pybsddb
+    from bsddb3 import db, dbtables
+except ImportError:
     # For Python 2.3
     from bsddb import db, dbtables
-except ImportError:
-    # For earlier Pythons w/distutils pybsddb
-    from bsddb3 import db, dbtables
 
 
 
diff --git a/Lib/bsddb/test/test_env_close.py b/Lib/bsddb/test/test_env_close.py
index 105be44..c112941 100644
--- a/Lib/bsddb/test/test_env_close.py
+++ b/Lib/bsddb/test/test_env_close.py
@@ -9,11 +9,11 @@ import glob
 import unittest
 
 try:
+    # For Pythons w/distutils pybsddb
+    from bsddb3 import db
+except ImportError:
     # For Python 2.3
     from bsddb import db
-except ImportError:
-    # For earlier Pythons w/distutils pybsddb
-    from bsddb3 import db
 
 from test_all import verbose
 
diff --git a/Lib/bsddb/test/test_get_none.py b/Lib/bsddb/test/test_get_none.py
index 40b224e..5f09cec 100644
--- a/Lib/bsddb/test/test_get_none.py
+++ b/Lib/bsddb/test/test_get_none.py
@@ -8,11 +8,11 @@ from pprint import pprint
 import unittest
 
 try:
+    # For Pythons w/distutils pybsddb
+    from bsddb3 import db
+except ImportError:
     # For Python 2.3
     from bsddb import db
-except ImportError:
-    # For earlier Pythons w/distutils pybsddb
-    from bsddb3 import db
 
 from test_all import verbose
 
diff --git a/Lib/bsddb/test/test_join.py b/Lib/bsddb/test/test_join.py
index 838ffe7..73edd11 100644
--- a/Lib/bsddb/test/test_join.py
+++ b/Lib/bsddb/test/test_join.py
@@ -16,11 +16,11 @@ import unittest
 from test_all import verbose
 
 try:
+    # For Pythons w/distutils pybsddb
+    from bsddb3 import db, dbshelve
+except ImportError:
     # For Python 2.3
     from bsddb import db, dbshelve
-except ImportError:
-    # For earlier Pythons w/distutils pybsddb
-    from bsddb3 import db, dbshelve
 
 
 #----------------------------------------------------------------------
diff --git a/Lib/bsddb/test/test_lock.py b/Lib/bsddb/test/test_lock.py
index 008f89d..4a5adc4 100644
--- a/Lib/bsddb/test/test_lock.py
+++ b/Lib/bsddb/test/test_lock.py
@@ -19,11 +19,11 @@ import unittest
 from test_all import verbose
 
 try:
+    # For Pythons w/distutils pybsddb
+    from bsddb3 import db
+except ImportError:
     # For Python 2.3
     from bsddb import db
-except ImportError:
-    # For earlier Pythons w/distutils pybsddb
-    from bsddb3 import db
 
 
 #----------------------------------------------------------------------
diff --git a/Lib/bsddb/test/test_misc.py b/Lib/bsddb/test/test_misc.py
index 43d194a..a66b1de 100644
--- a/Lib/bsddb/test/test_misc.py
+++ b/Lib/bsddb/test/test_misc.py
@@ -6,11 +6,11 @@ import sys
 import unittest
 
 try:
+    # For Pythons w/distutils pybsddb
+    from bsddb3 import db, dbshelve
+except ImportError:
     # For Python 2.3
     from bsddb import db, dbshelve
-except ImportError:
-    # For earlier Pythons w/distutils pybsddb
-    from bsddb3 import db, dbshelve
 
 #----------------------------------------------------------------------
 
diff --git a/Lib/bsddb/test/test_queue.py b/Lib/bsddb/test/test_queue.py
index 24b86cb..95cf20d 100644
--- a/Lib/bsddb/test/test_queue.py
+++ b/Lib/bsddb/test/test_queue.py
@@ -8,11 +8,11 @@ from pprint import pprint
 import unittest
 
 try:
+    # For Pythons w/distutils pybsddb
+    from bsddb3 import db
+except ImportError:
     # For Python 2.3
     from bsddb import db
-except ImportError:
-    # For earlier Pythons w/distutils pybsddb
-    from bsddb3 import db
 
 from test_all import verbose
 
diff --git a/Lib/bsddb/test/test_recno.py b/Lib/bsddb/test/test_recno.py
index 3e517c1..87446d3 100644
--- a/Lib/bsddb/test/test_recno.py
+++ b/Lib/bsddb/test/test_recno.py
@@ -11,11 +11,11 @@ import unittest
 from test_all import verbose
 
 try:
+    # For Pythons w/distutils pybsddb
+    from bsddb3 import db
+except ImportError:
     # For Python 2.3
     from bsddb import db
-except ImportError:
-    # For earlier Pythons w/distutils pybsddb
-    from bsddb3 import db
 
 letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
 
diff --git a/Lib/bsddb/test/test_thread.py b/Lib/bsddb/test/test_thread.py
index cf4237c..59af027 100644
--- a/Lib/bsddb/test/test_thread.py
+++ b/Lib/bsddb/test/test_thread.py
@@ -28,11 +28,11 @@ import unittest
 from test_all import verbose
 
 try:
+    # For Pythons w/distutils pybsddb
+    from bsddb3 import db, dbutils
+except ImportError:
     # For Python 2.3
     from bsddb import db, dbutils
-except ImportError:
-    # For earlier Pythons w/distutils pybsddb
-    from bsddb3 import db, dbutils
 
 
 #----------------------------------------------------------------------
diff --git a/Modules/_bsddb.c b/Modules/_bsddb.c
index dcbcdb3..f24c713 100644
--- a/Modules/_bsddb.c
+++ b/Modules/_bsddb.c
@@ -36,7 +36,7 @@
 /*
  * Handwritten code to wrap version 3.x of the Berkeley DB library,
  * written to replace a SWIG-generated file.  It has since been updated
- * to compile with BerkeleyDB versions 3.2 through 4.1.
+ * to compile with BerkeleyDB versions 3.2 through 4.2.
  *
  * This module was started by Andrew Kuchling to remove the dependency
  * on SWIG in a package by Gregory P. Smith <greg@electricrain.com> who
@@ -93,7 +93,7 @@
 /* 40 = 4.0, 33 = 3.3; this will break if the second number is > 9 */
 #define DBVER (DB_VERSION_MAJOR * 10 + DB_VERSION_MINOR)
 
-#define PY_BSDDB_VERSION "4.1.6"
+#define PY_BSDDB_VERSION "4.2.0"
 static char *rcs_id = "$Id$";
 
 
@@ -2167,6 +2167,17 @@ DB_verify(DBObject* self, PyObject* args, PyObject* kwargs)
     MYDB_END_ALLOW_THREADS;
     if (outFileName)
         fclose(outFile);
+
+    /* DB.verify acts as a DB handle destructor (like close); this was
+     * documented in BerkeleyDB 4.2 but had the undocumented effect
+     * of not being safe in prior versions while still requiring an explicit
+     * DB.close call afterwards.  Lets call close for the user to emulate
+     * the safe 4.2 behaviour. */
+#if (DBVER <= 41)
+    self->db->close(self->db, 0);
+#endif
+    self->db = NULL;
+
     RETURN_IF_ERR();
     RETURN_NONE();
 }
@@ -4340,7 +4351,8 @@ static PyMethodDef bsddb_methods[] = {
  */
 #define ADD_INT(dict, NAME)         _addIntToDict(dict, #NAME, NAME)
 
-
+#define MODULE_NAME_MAX_LEN     11
+static char _bsddbModuleName[MODULE_NAME_MAX_LEN+1] = "_bsddb";
 
 DL_EXPORT(void) init_bsddb(void)
 {
@@ -4365,7 +4377,7 @@ DL_EXPORT(void) init_bsddb(void)
 #endif
 
     /* Create the module and add the functions */
-    m = Py_InitModule("_bsddb", bsddb_methods);
+    m = Py_InitModule(_bsddbModuleName, bsddb_methods);
 
     /* Add some symbolic constants to the module */
     d = PyModule_GetDict(m);
@@ -4386,7 +4398,13 @@ DL_EXPORT(void) init_bsddb(void)
     ADD_INT(d, DB_MAX_PAGES);
     ADD_INT(d, DB_MAX_RECORDS);
 
+#if (DBVER >= 42)
+    ADD_INT(d, DB_RPCCLIENT);
+#else
     ADD_INT(d, DB_CLIENT);
+    /* allow apps to be written using DB_RPCCLIENT on older BerkeleyDB */
+    _addIntToDict(d, "DB_RPCCLIENT", DB_CLIENT);
+#endif
     ADD_INT(d, DB_XA_CREATE);
 
     ADD_INT(d, DB_CREATE);
@@ -4535,7 +4553,7 @@ DL_EXPORT(void) init_bsddb(void)
     ADD_INT(d, DB_CHECKPOINT);
     ADD_INT(d, DB_CURLSN);
 #endif
-#if (DBVER >= 33)
+#if ((DBVER >= 33) && (DBVER <= 41))
     ADD_INT(d, DB_COMMIT);
 #endif
     ADD_INT(d, DB_CONSUME);
@@ -4610,6 +4628,18 @@ DL_EXPORT(void) init_bsddb(void)
     ADD_INT(d, DB_NOPANIC);
 #endif
 
+#if (DBVER >= 42)
+    ADD_INT(d, DB_TIME_NOTGRANTED);
+    ADD_INT(d, DB_TXN_NOT_DURABLE);
+    ADD_INT(d, DB_TXN_WRITE_NOSYNC);
+    ADD_INT(d, DB_LOG_AUTOREMOVE);
+    ADD_INT(d, DB_DIRECT_LOG);
+    ADD_INT(d, DB_DIRECT_DB);
+    ADD_INT(d, DB_INIT_REP);
+    ADD_INT(d, DB_ENCRYPT);
+    ADD_INT(d, DB_CHKSUM);
+#endif
+
 #if (DBVER >= 41)
     ADD_INT(d, DB_ENCRYPT_AES);
     ADD_INT(d, DB_AUTO_COMMIT);
@@ -4686,3 +4716,12 @@ DL_EXPORT(void) init_bsddb(void)
         Py_FatalError("can't initialize module _bsddb");
     }
 }
+
+/* allow this module to be named _pybsddb so that it can be installed
+ * and imported on top of python >= 2.3 that includes its own older
+ * copy of the library named _bsddb without importing the old version. */
+DL_EXPORT(void) init_pybsddb(void)
+{
+    strncpy(_bsddbModuleName, "_pybsddb", MODULE_NAME_MAX_LEN);
+    init_bsddb();
+}
-- 
cgit v0.12

