Skip to content

Commit c5a11fa

Browse files
committed
bsddb module updated to version 4.7.2devel9.
This patch publishes the work done until now for Python 3.0 compatibility. Still a lot to be done. When possible, we use 3.0 features in Python 2.6, easing development and testing, and exposing internal changes to a wider audience, for better test coverage. Some mode details: http://www.jcea.es/programacion/pybsddb.htm#bsddb3-4.7.2
1 parent 30e208d commit c5a11fa

16 files changed

Lines changed: 1361 additions & 718 deletions

‎Lib/bsddb/__init__.py‎

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,25 @@
3333
#----------------------------------------------------------------------
3434

3535

36-
"""Support for Berkeley DB 4.x with a simple interface.
36+
"""Support for Berkeley DB 4.0 through 4.7 with a simple interface.
3737
3838
For the full featured object oriented interface use the bsddb.db module
3939
instead. It mirrors the Oracle Berkeley DB C API.
4040
"""
4141

42+
import sys
43+
absolute_import = (sys.version_info[0] >= 3)
44+
4245
try:
4346
if __name__ == 'bsddb3':
4447
# import _pybsddb binary as it should be the more recent version from
4548
# a standalone pybsddb addon package than the version included with
4649
# python as bsddb._bsddb.
47-
import _pybsddb
50+
if absolute_import :
51+
# Because this syntaxis is not valid before Python 2.5
52+
exec("from . import _pybsddb")
53+
else :
54+
import _pybsddb
4855
_bsddb = _pybsddb
4956
from bsddb3.dbutils import DeadlockWrap as _DeadlockWrap
5057
else:
@@ -66,9 +73,16 @@
6673

6774
import sys, os
6875

69-
import UserDict
7076
from weakref import ref
71-
class _iter_mixin(UserDict.DictMixin):
77+
78+
if sys.version_info[0:2] <= (2, 5) :
79+
import UserDict
80+
MutableMapping = UserDict.DictMixin
81+
else :
82+
import collections
83+
MutableMapping = collections.MutableMapping
84+
85+
class _iter_mixin(MutableMapping):
7286
def _make_iter_cursor(self):
7387
cur = _DeadlockWrap(self.db.cursor)
7488
key = id(cur)
@@ -115,8 +129,12 @@ def __iter__(self):
115129
except _bsddb.DBCursorClosedError:
116130
# the database was modified during iteration. abort.
117131
pass
118-
finally:
132+
# When Python 2.3 not supported in bsddb3, we can change this to "finally"
133+
except :
119134
self._in_iter -= 1
135+
raise
136+
137+
self._in_iter -= 1
120138

121139
def iteritems(self):
122140
if not self.db:
@@ -154,8 +172,12 @@ def iteritems(self):
154172
except _bsddb.DBCursorClosedError:
155173
# the database was modified during iteration. abort.
156174
pass
157-
finally:
175+
# When Python 2.3 not supported in bsddb3, we can change this to "finally"
176+
except :
158177
self._in_iter -= 1
178+
raise
179+
180+
self._in_iter -= 1
159181

160182

161183
class _DBWithCursor(_iter_mixin):
@@ -228,6 +250,12 @@ def __len__(self):
228250
self._checkOpen()
229251
return _DeadlockWrap(lambda: len(self.db)) # len(self.db)
230252

253+
if sys.version_info[0:2] >= (2, 6) :
254+
def __repr__(self) :
255+
if self.isOpen() :
256+
return repr(dict(_DeadlockWrap(self.db.items)))
257+
return repr(dict())
258+
231259
def __getitem__(self, key):
232260
self._checkOpen()
233261
return _DeadlockWrap(lambda: self.db[key]) # self.db[key]
@@ -407,8 +435,6 @@ def _checkflag(flag, file):
407435
try:
408436
import thread
409437
del thread
410-
if db.version() < (3, 3, 0):
411-
db.DB_THREAD = 0
412438
except ImportError:
413439
db.DB_THREAD = 0
414440

‎Lib/bsddb/db.py‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,24 @@
3737
# case we ever want to augment the stuff in _db in any way. For now
3838
# it just simply imports everything from _db.
3939

40-
if __name__.startswith('bsddb3.'):
41-
# import _pybsddb binary as it should be the more recent version from
42-
# a standalone pybsddb addon package than the version included with
43-
# python as bsddb._bsddb.
44-
from _pybsddb import *
45-
from _pybsddb import __version__
46-
else:
47-
from _bsddb import *
48-
from _bsddb import __version__
40+
import sys
41+
absolute_import = (sys.version_info[0] >= 3)
4942

50-
if version() < (3, 2, 0):
51-
raise ImportError, "correct Berkeley DB symbols not found. Perhaps python was statically linked with an older version?"
43+
if not absolute_import :
44+
if __name__.startswith('bsddb3.') :
45+
# import _pybsddb binary as it should be the more recent version from
46+
# a standalone pybsddb addon package than the version included with
47+
# python as bsddb._bsddb.
48+
from _pybsddb import *
49+
from _pybsddb import __version__
50+
else:
51+
from _bsddb import *
52+
from _bsddb import __version__
53+
else :
54+
# Because this syntaxis is not valid before Python 2.5
55+
if __name__.startswith('bsddb3.') :
56+
exec("from ._pybsddb import *")
57+
exec("from ._pybsddb import __version__")
58+
else :
59+
exec("from ._bsddb import *")
60+
exec("from ._bsddb import __version__")

‎Lib/bsddb/dbobj.py‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,24 @@
2121
# added to _bsddb.c.
2222
#
2323

24-
import db
24+
import sys
25+
absolute_import = (sys.version_info[0] >= 3)
26+
if absolute_import :
27+
# Because this syntaxis is not valid before Python 2.5
28+
exec("from . import db")
29+
else :
30+
import db
2531

26-
try:
27-
from UserDict import DictMixin
28-
except ImportError:
29-
# DictMixin is new in Python 2.3
30-
class DictMixin: pass
32+
if sys.version_info[0:2] <= (2, 5) :
33+
try:
34+
from UserDict import DictMixin
35+
except ImportError:
36+
# DictMixin is new in Python 2.3
37+
class DictMixin: pass
38+
MutableMapping = DictMixin
39+
else :
40+
import collections
41+
MutableMapping = collections.MutableMapping
3142

3243
class DBEnv:
3344
def __init__(self, *args, **kwargs):
@@ -96,9 +107,8 @@ def log_archive(self, *args, **kwargs):
96107
def set_get_returns_none(self, *args, **kwargs):
97108
return apply(self._cobj.set_get_returns_none, args, kwargs)
98109

99-
if db.version() >= (4,0):
100-
def log_stat(self, *args, **kwargs):
101-
return apply(self._cobj.log_stat, args, kwargs)
110+
def log_stat(self, *args, **kwargs):
111+
return apply(self._cobj.log_stat, args, kwargs)
102112

103113
if db.version() >= (4,1):
104114
def dbremove(self, *args, **kwargs):
@@ -113,7 +123,7 @@ def lsn_reset(self, *args, **kwargs):
113123
return apply(self._cobj.lsn_reset, args, kwargs)
114124

115125

116-
class DB(DictMixin):
126+
class DB(MutableMapping):
117127
def __init__(self, dbenv, *args, **kwargs):
118128
# give it the proper DBEnv C object that its expecting
119129
self._cobj = apply(db.DB, (dbenv._cobj,) + args, kwargs)
@@ -128,6 +138,10 @@ def __setitem__(self, key, value):
128138
def __delitem__(self, arg):
129139
del self._cobj[arg]
130140

141+
if sys.version_info[0:2] >= (2, 6) :
142+
def __iter__(self) :
143+
return self._cobj.__iter__()
144+
131145
def append(self, *args, **kwargs):
132146
return apply(self._cobj.append, args, kwargs)
133147
def associate(self, *args, **kwargs):

‎Lib/bsddb/dbshelve.py‎

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,17 @@
3030
#------------------------------------------------------------------------
3131

3232
import cPickle
33-
import db
3433
import sys
3534

36-
#At version 2.3 cPickle switched to using protocol instead of bin and
37-
#DictMixin was added
35+
import sys
36+
absolute_import = (sys.version_info[0] >= 3)
37+
if absolute_import :
38+
# Because this syntaxis is not valid before Python 2.5
39+
exec("from . import db")
40+
else :
41+
import db
42+
43+
#At version 2.3 cPickle switched to using protocol instead of bin
3844
if sys.version_info[:3] >= (2, 3, 0):
3945
HIGHEST_PROTOCOL = cPickle.HIGHEST_PROTOCOL
4046
# In python 2.3.*, "cPickle.dumps" accepts no
@@ -47,13 +53,22 @@ def _dumps(object, protocol):
4753
def _dumps(object, protocol):
4854
return cPickle.dumps(object, protocol=protocol)
4955

50-
from UserDict import DictMixin
51-
5256
else:
5357
HIGHEST_PROTOCOL = None
5458
def _dumps(object, protocol):
5559
return cPickle.dumps(object, bin=protocol)
56-
class DictMixin: pass
60+
61+
62+
if sys.version_info[0:2] <= (2, 5) :
63+
try:
64+
from UserDict import DictMixin
65+
except ImportError:
66+
# DictMixin is new in Python 2.3
67+
class DictMixin: pass
68+
MutableMapping = DictMixin
69+
else :
70+
import collections
71+
MutableMapping = collections.MutableMapping
5772

5873
#------------------------------------------------------------------------
5974

@@ -96,7 +111,7 @@ def open(filename, flags=db.DB_CREATE, mode=0660, filetype=db.DB_HASH,
96111
class DBShelveError(db.DBError): pass
97112

98113

99-
class DBShelf(DictMixin):
114+
class DBShelf(MutableMapping):
100115
"""A shelf to hold pickled objects, built upon a bsddb DB object. It
101116
automatically pickles/unpickles data objects going to/from the DB.
102117
"""
@@ -147,6 +162,10 @@ def keys(self, txn=None):
147162
else:
148163
return self.db.keys()
149164

165+
if sys.version_info[0:2] >= (2, 6) :
166+
def __iter__(self) :
167+
return self.db.__iter__()
168+
150169

151170
def open(self, *args, **kwargs):
152171
self.db.open(*args, **kwargs)

‎Lib/bsddb/dbtables.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import copy
2323
import random
2424
import struct
25-
from types import ListType, StringType
2625
import cPickle as pickle
2726

2827
try:
@@ -229,7 +228,7 @@ def CreateTable(self, table, columns):
229228
230229
raises TableDBError if it already exists or for other DB errors.
231230
"""
232-
assert isinstance(columns, ListType)
231+
assert isinstance(columns, list)
233232
txn = None
234233
try:
235234
# checking sanity of the table and column names here on
@@ -270,7 +269,7 @@ def ListTableColumns(self, table):
270269
"""Return a list of columns in the given table.
271270
[] if the table doesn't exist.
272271
"""
273-
assert isinstance(table, StringType)
272+
assert isinstance(table, str)
274273
if contains_metastrings(table):
275274
raise ValueError, "bad table name: contains reserved metastrings"
276275

@@ -300,7 +299,7 @@ def CreateOrExtendTable(self, table, columns):
300299
additional columns present in the given list as well as
301300
all of its current columns.
302301
"""
303-
assert isinstance(columns, ListType)
302+
assert isinstance(columns, list)
304303
try:
305304
self.CreateTable(table, columns)
306305
except TableAlreadyExists:

‎Lib/bsddb/dbutils.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@
2626
#
2727
from time import sleep as _sleep
2828

29-
import db
29+
import sys
30+
absolute_import = (sys.version_info[0] >= 3)
31+
if absolute_import :
32+
# Because this syntaxis is not valid before Python 2.5
33+
exec("from . import db")
34+
else :
35+
import db
3036

3137
# always sleep at least N seconds between retrys
3238
_deadlock_MinSleepTime = 1.0/128

‎Lib/bsddb/test/test_all.py‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,24 @@
77
try:
88
# For Pythons w/distutils pybsddb
99
from bsddb3 import db
10+
import bsddb3 as bsddb
1011
except ImportError:
1112
# For Python 2.3
1213
from bsddb import db
14+
import bsddb
1315

1416
try:
1517
from bsddb3 import test_support
1618
except ImportError:
1719
from test import test_support
1820

21+
try:
22+
from threading import Thread, currentThread
23+
del Thread, currentThread
24+
have_threads = True
25+
except ImportError:
26+
have_threads = False
27+
1928
verbose = 0
2029
if 'verbose' in sys.argv:
2130
verbose = 1
@@ -33,6 +42,8 @@ def print_versions():
3342
print 'bsddb.db.version(): %s' % (db.version(), )
3443
print 'bsddb.db.__version__: %s' % db.__version__
3544
print 'bsddb.db.cvsid: %s' % db.cvsid
45+
print 'py module: %s' % bsddb.__file__
46+
print 'extension module: %s' % bsddb._bsddb.__file__
3647
print 'python version: %s' % sys.version
3748
print 'My pid: %s' % os.getpid()
3849
print '-=' * 38
@@ -81,11 +92,11 @@ def set_test_path_prefix(path) :
8192
def remove_test_path_directory() :
8293
test_support.rmtree(get_new_path.prefix)
8394

84-
try :
95+
if have_threads :
8596
import threading
8697
get_new_path.mutex=threading.Lock()
8798
del threading
88-
except ImportError:
99+
else :
89100
class Lock(object) :
90101
def acquire(self) :
91102
pass
@@ -104,8 +115,12 @@ def testPrintVersions(self):
104115
# This little hack is for when this module is run as main and all the
105116
# other modules import it so they will still be able to get the right
106117
# verbose setting. It's confusing but it works.
107-
import test_all
108-
test_all.verbose = verbose
118+
if sys.version_info[0] < 3 :
119+
import test_all
120+
test_all.verbose = verbose
121+
else :
122+
import sys
123+
print >>sys.stderr, "Work to do!"
109124

110125

111126
def suite(module_prefix='', timing_check=None):

0 commit comments

Comments
 (0)