~pythonregexp2.7/python/issue2636-01+09-01-01

« back to all changes in this revision

Viewing changes to Lib/bsddb/dbshelve.py

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-09-22 00:02:12 UTC
  • mfrom: (39022.1.34 Regexp-2.7)
  • Revision ID: darklord@timehorse.com-20080922000212-7r0q4f4ugiq57jph
Merged in changes from the Atomic Grouping / Possessive Qualifiers branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
#------------------------------------------------------------------------
31
31
 
32
32
import cPickle
33
 
import db
34
 
import sys
35
 
 
36
 
#At version 2.3 cPickle switched to using protocol instead of bin and
37
 
#DictMixin was added
 
33
import sys
 
34
 
 
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
38
44
if sys.version_info[:3] >= (2, 3, 0):
39
45
    HIGHEST_PROTOCOL = cPickle.HIGHEST_PROTOCOL
40
46
# In python 2.3.*, "cPickle.dumps" accepts no
47
53
        def _dumps(object, protocol):
48
54
            return cPickle.dumps(object, protocol=protocol)
49
55
 
50
 
    from UserDict import DictMixin
51
 
 
52
56
else:
53
57
    HIGHEST_PROTOCOL = None
54
58
    def _dumps(object, protocol):
55
59
        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
57
72
 
58
73
#------------------------------------------------------------------------
59
74
 
96
111
class DBShelveError(db.DBError): pass
97
112
 
98
113
 
99
 
class DBShelf(DictMixin):
 
114
class DBShelf(MutableMapping):
100
115
    """A shelf to hold pickled objects, built upon a bsddb DB object.  It
101
116
    automatically pickles/unpickles data objects going to/from the DB.
102
117
    """
147
162
        else:
148
163
            return self.db.keys()
149
164
 
 
165
    if sys.version_info[0:2] >= (2, 6) :
 
166
        def __iter__(self) :
 
167
            return self.db.__iter__()
 
168
 
150
169
 
151
170
    def open(self, *args, **kwargs):
152
171
        self.db.open(*args, **kwargs)
199
218
 
200
219
    def associate(self, secondaryDB, callback, flags=0):
201
220
        def _shelf_callback(priKey, priData, realCallback=callback):
202
 
            data = cPickle.loads(priData)
 
221
            # Safe in Python 2.x because expresion short circuit
 
222
            if sys.version_info[0] < 3 or isinstance(priData, bytes) :
 
223
                data = cPickle.loads(priData)
 
224
            else :
 
225
                data = cPickle.loads(bytes(priData, "iso8859-1"))  # 8 bits
203
226
            return realCallback(priKey, data)
 
227
 
204
228
        return self.db.associate(secondaryDB, _shelf_callback, flags)
205
229
 
206
230
 
213
237
        data = apply(self.db.get, args, kw)
214
238
        try:
215
239
            return cPickle.loads(data)
216
 
        except (TypeError, cPickle.UnpicklingError):
 
240
        except (EOFError, TypeError, cPickle.UnpicklingError):
217
241
            return data  # we may be getting the default value, or None,
218
242
                         # so it doesn't need unpickled.
219
243
 
331
355
            return None
332
356
        else:
333
357
            key, data = rec
334
 
            return key, cPickle.loads(data)
 
358
            # Safe in Python 2.x because expresion short circuit
 
359
            if sys.version_info[0] < 3 or isinstance(data, bytes) :
 
360
                return key, cPickle.loads(data)
 
361
            else :
 
362
                return key, cPickle.loads(bytes(data, "iso8859-1"))  # 8 bits
335
363
 
336
364
    #----------------------------------------------
337
365
    # Methods allowed to pass-through to self.dbc