2929
3030#------------------------------------------------------------------------
3131
32- import cPickle
33- import sys
34-
3532import sys
3633absolute_import = (sys .version_info [0 ] >= 3 )
3734if absolute_import :
4037else :
4138 import db
4239
40+ if sys .version_info [0 ] >= 3 :
41+ import cPickle # Will be converted to "pickle" by "2to3"
42+ else :
43+ if sys .version_info < (2 , 6 ) :
44+ import cPickle
45+ else :
46+ # When we drop support for python 2.3 and 2.4
47+ # we could use: (in 2.5 we need a __future__ statement)
48+ #
49+ # with warnings.catch_warnings():
50+ # warnings.filterwarnings(...)
51+ # ...
52+ #
53+ # We can not use "with" as is, because it would be invalid syntax
54+ # in python 2.3, 2.4 and (with no __future__) 2.5.
55+ # Here we simulate "with" following PEP 343 :
56+ import warnings
57+ w = warnings .catch_warnings ()
58+ w .__enter__ ()
59+ try :
60+ warnings .filterwarnings ('ignore' ,
61+ message = 'the cPickle module has been removed in Python 3.0' ,
62+ category = DeprecationWarning )
63+ import cPickle
64+ finally :
65+ w .__exit__ ()
66+ del w
67+
4368#At version 2.3 cPickle switched to using protocol instead of bin
44- if sys .version_info [: 3 ] >= (2 , 3 , 0 ):
69+ if sys .version_info >= (2 , 3 ):
4570 HIGHEST_PROTOCOL = cPickle .HIGHEST_PROTOCOL
4671# In python 2.3.*, "cPickle.dumps" accepts no
4772# named parameters. "pickle.dumps" accepts them,
4873# so this seems a bug.
49- if sys .version_info [: 3 ] < (2 , 4 , 0 ):
74+ if sys .version_info < (2 , 4 ):
5075 def _dumps (object , protocol ):
5176 return cPickle .dumps (object , protocol )
5277 else :
@@ -59,11 +84,16 @@ def _dumps(object, protocol):
5984 return cPickle .dumps (object , bin = protocol )
6085
6186
62- try :
63- from UserDict import DictMixin
64- except ImportError :
65- # DictMixin is new in Python 2.3
66- class DictMixin : pass
87+ if sys .version_info < (2 , 6 ) :
88+ try :
89+ from UserDict import DictMixin
90+ except ImportError :
91+ # DictMixin is new in Python 2.3
92+ class DictMixin : pass
93+ MutableMapping = DictMixin
94+ else :
95+ import collections
96+ MutableMapping = collections .MutableMapping
6797
6898#------------------------------------------------------------------------
6999
@@ -106,7 +136,7 @@ def open(filename, flags=db.DB_CREATE, mode=0660, filetype=db.DB_HASH,
106136class DBShelveError (db .DBError ): pass
107137
108138
109- class DBShelf (DictMixin ):
139+ class DBShelf (MutableMapping ):
110140 """A shelf to hold pickled objects, built upon a bsddb DB object. It
111141 automatically pickles/unpickles data objects going to/from the DB.
112142 """
@@ -157,6 +187,17 @@ def keys(self, txn=None):
157187 else :
158188 return self .db .keys ()
159189
190+ if sys .version_info >= (2 , 6 ) :
191+ def __iter__ (self ) : # XXX: Load all keys in memory :-(
192+ for k in self .db .keys () :
193+ yield k
194+
195+ # Do this when "DB" support iteration
196+ # Or is it enough to pass thru "getattr"?
197+ #
198+ # def __iter__(self) :
199+ # return self.db.__iter__()
200+
160201
161202 def open (self , * args , ** kwargs ):
162203 self .db .open (* args , ** kwargs )
0 commit comments