Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 918f49e

Browse files
committed
Fix most of the bsddb3 tests.
1 parent eb29e9a commit 918f49e

15 files changed

Lines changed: 279 additions & 294 deletions

Lib/bsddb/dbshelve.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
#------------------------------------------------------------------------
3131

32-
import cPickle
32+
import pickle
3333
try:
3434
from UserDict import DictMixin
3535
except ImportError:
@@ -104,11 +104,11 @@ def __len__(self):
104104

105105
def __getitem__(self, key):
106106
data = self.db[key]
107-
return cPickle.loads(data)
107+
return pickle.loads(data)
108108

109109

110110
def __setitem__(self, key, value):
111-
data = cPickle.dumps(value, self.binary)
111+
data = pickle.dumps(value, self.binary)
112112
self.db[key] = data
113113

114114

@@ -131,7 +131,7 @@ def items(self, txn=None):
131131
newitems = []
132132

133133
for k, v in items:
134-
newitems.append( (k, cPickle.loads(v)) )
134+
newitems.append( (k, pickle.loads(v)) )
135135
return newitems
136136

137137
def values(self, txn=None):
@@ -140,13 +140,13 @@ def values(self, txn=None):
140140
else:
141141
values = self.db.values()
142142

143-
return map(cPickle.loads, values)
143+
return map(pickle.loads, values)
144144

145145
#-----------------------------------
146146
# Other methods
147147

148148
def __append(self, value, txn=None):
149-
data = cPickle.dumps(value, self.binary)
149+
data = pickle.dumps(value, self.binary)
150150
return self.db.append(data, txn)
151151

152152
def append(self, value, txn=None):
@@ -158,7 +158,7 @@ def append(self, value, txn=None):
158158

159159
def associate(self, secondaryDB, callback, flags=0):
160160
def _shelf_callback(priKey, priData, realCallback=callback):
161-
data = cPickle.loads(priData)
161+
data = pickle.loads(priData)
162162
return realCallback(priKey, data)
163163
return self.db.associate(secondaryDB, _shelf_callback, flags)
164164

@@ -171,15 +171,15 @@ def get(self, *args, **kw):
171171
# off.
172172
data = self.db.get(*args, **kw)
173173
try:
174-
return cPickle.loads(data)
175-
except (TypeError, cPickle.UnpicklingError):
174+
return pickle.loads(data)
175+
except (TypeError, pickle.UnpicklingError, EOFError):
176176
return data # we may be getting the default value, or None,
177177
# so it doesn't need unpickled.
178178

179179
def get_both(self, key, value, txn=None, flags=0):
180-
data = cPickle.dumps(value, self.binary)
180+
data = pickle.dumps(value, self.binary)
181181
data = self.db.get(key, data, txn, flags)
182-
return cPickle.loads(data)
182+
return pickle.loads(data)
183183

184184

185185
def cursor(self, txn=None, flags=0):
@@ -189,7 +189,7 @@ def cursor(self, txn=None, flags=0):
189189

190190

191191
def put(self, key, value, txn=None, flags=0):
192-
data = cPickle.dumps(value, self.binary)
192+
data = pickle.dumps(value, self.binary)
193193
return self.db.put(key, data, txn, flags)
194194

195195

@@ -233,7 +233,7 @@ def dup(self, flags=0):
233233

234234

235235
def put(self, key, value, flags=0):
236-
data = cPickle.dumps(value, self.binary)
236+
data = pickle.dumps(value, self.binary)
237237
return self.dbc.put(key, data, flags)
238238

239239

@@ -251,7 +251,7 @@ def get_2(self, key, flags):
251251
return self._extract(rec)
252252

253253
def get_3(self, key, value, flags):
254-
data = cPickle.dumps(value, self.binary)
254+
data = pickle.dumps(value, self.binary)
255255
rec = self.dbc.get(key, flags)
256256
return self._extract(rec)
257257

@@ -268,7 +268,7 @@ def prev_nodup(self, flags=0): return self.get_1(flags|db.DB_PREV_NODUP)
268268

269269

270270
def get_both(self, key, value, flags=0):
271-
data = cPickle.dumps(value, self.binary)
271+
data = pickle.dumps(value, self.binary)
272272
rec = self.dbc.get_both(key, flags)
273273
return self._extract(rec)
274274

@@ -292,7 +292,7 @@ def _extract(self, rec):
292292
return None
293293
else:
294294
key, data = rec
295-
return key, cPickle.loads(data)
295+
return key, pickle.loads(data)
296296

297297
#----------------------------------------------
298298
# Methods allowed to pass-through to self.dbc

Lib/bsddb/dbtables.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ def __call__(self, s):
9090
#
9191
# keys used to store database metadata
9292
#
93-
_table_names_key = '__TABLE_NAMES__' # list of the tables in this db
94-
_columns = '._COLUMNS__' # table_name+this key contains a list of columns
93+
_table_names_key = b'__TABLE_NAMES__' # list of the tables in this db
94+
_columns = b'._COLUMNS__' # table_name+this key contains a list of columns
9595

9696
def _columns_key(table):
9797
return table + _columns

Lib/bsddb/test/test_associate.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ def tearDown(self):
176176
def addDataToDB(self, d, txn=None):
177177
for key, value in musicdata.items():
178178
if type(self.keytype) == type(''):
179-
key = "%02d" % key
180-
d.put(key, '|'.join(value), txn=txn)
179+
key = ("%02d" % key).encode("utf-8")
180+
d.put(key, '|'.join(value).encode("utf-8"), txn=txn)
181181

182182
def createDB(self, txn=None):
183183
self.cur = None
@@ -247,14 +247,14 @@ def test02_associateAfterDB(self):
247247

248248
def finish_test(self, secDB, txn=None):
249249
# 'Blues' should not be in the secondary database
250-
vals = secDB.pget('Blues', txn=txn)
250+
vals = secDB.pget(b'Blues', txn=txn)
251251
assert vals == None, vals
252252

253-
vals = secDB.pget('Unknown', txn=txn)
254-
assert vals[0] == 99 or vals[0] == '99', vals
255-
vals[1].index('Unknown')
256-
vals[1].index('Unnamed')
257-
vals[1].index('unknown')
253+
vals = secDB.pget(b'Unknown', txn=txn)
254+
assert vals[0] == 99 or vals[0] == b'99', vals
255+
vals[1].index(b'Unknown')
256+
vals[1].index(b'Unnamed')
257+
vals[1].index(b'unknown')
258258

259259
if verbose:
260260
print("Primary key traversal:")
@@ -279,18 +279,18 @@ def finish_test(self, secDB, txn=None):
279279
count = 0
280280

281281
# test cursor pget
282-
vals = self.cur.pget('Unknown', flags=db.DB_LAST)
283-
assert vals[1] == 99 or vals[1] == '99', vals
284-
assert vals[0] == 'Unknown'
285-
vals[2].index('Unknown')
286-
vals[2].index('Unnamed')
287-
vals[2].index('unknown')
288-
289-
vals = self.cur.pget('Unknown', data='wrong value', flags=db.DB_GET_BOTH)
282+
vals = self.cur.pget(b'Unknown', flags=db.DB_LAST)
283+
assert vals[1] == 99 or vals[1] == b'99', vals
284+
assert vals[0] == b'Unknown'
285+
vals[2].index(b'Unknown')
286+
vals[2].index(b'Unnamed')
287+
vals[2].index(b'unknown')
288+
289+
vals = self.cur.pget(b'Unknown', data=b'wrong value', flags=db.DB_GET_BOTH)
290290
assert vals == None, vals
291291

292292
rec = self.cur.first()
293-
assert rec[0] == "Jazz"
293+
assert rec[0] == b"Jazz"
294294
while rec is not None:
295295
count = count + 1
296296
if verbose:
@@ -302,14 +302,15 @@ def finish_test(self, secDB, txn=None):
302302
self.cur = None
303303

304304
def getGenre(self, priKey, priData):
305-
assert type(priData) == type("")
305+
assert type(priData) == type(b"")
306+
priData = priData.decode("utf-8")
306307
if verbose:
307308
print('getGenre key: %r data: %r' % (priKey, priData))
308309
genre = priData.split('|')[2]
309310
if genre == 'Blues':
310311
return db.DB_DONOTINDEX
311312
else:
312-
return genre
313+
return genre.encode("utf-8")
313314

314315

315316
#----------------------------------------------------------------------
@@ -382,7 +383,7 @@ def createDB(self):
382383
def addDataToDB(self, d):
383384
for key, value in musicdata.items():
384385
if type(self.keytype) == type(''):
385-
key = "%02d" % key
386+
key = ("%02d" % key).encode("utf-8")
386387
d.put(key, value) # save the value as is this time
387388

388389

@@ -394,7 +395,7 @@ def getGenre(self, priKey, priData):
394395
if genre == 'Blues':
395396
return db.DB_DONOTINDEX
396397
else:
397-
return genre
398+
return genre.encode("utf-8")
398399

399400

400401
class ShelveAssociateHashTestCase(ShelveAssociateTestCase):
@@ -426,7 +427,7 @@ def addDataToDB(self, d):
426427
def writer1(self, d):
427428
for key, value in musicdata.items():
428429
if type(self.keytype) == type(''):
429-
key = "%02d" % key
430+
key = ("%02d" % key).encode("utf-8")
430431
d.put(key, '|'.join(value))
431432

432433
def writer2(self, d):

0 commit comments

Comments
 (0)