diff options
author | Martin v. Löwis <martin@v.loewis.de> | 2007-08-08 22:08:30 (GMT) |
---|---|---|
committer | Martin v. Löwis <martin@v.loewis.de> | 2007-08-08 22:08:30 (GMT) |
commit | 918f49e645474382251bfddbb0a2e030051083ef (patch) | |
tree | 01fada4bbee6dd26f5503a7a7295b1c9f9f63a9c /Lib/bsddb | |
parent | eb29e9ab2b037db198591a9cacb5993385c7f83d (diff) | |
download | cpython-918f49e645474382251bfddbb0a2e030051083ef.zip cpython-918f49e645474382251bfddbb0a2e030051083ef.tar.gz cpython-918f49e645474382251bfddbb0a2e030051083ef.tar.bz2 |
Fix most of the bsddb3 tests.
Diffstat (limited to 'Lib/bsddb')
-rw-r--r-- | Lib/bsddb/dbshelve.py | 32 | ||||
-rw-r--r-- | Lib/bsddb/dbtables.py | 4 | ||||
-rw-r--r-- | Lib/bsddb/test/test_associate.py | 45 | ||||
-rw-r--r-- | Lib/bsddb/test/test_basics.py | 171 | ||||
-rw-r--r-- | Lib/bsddb/test/test_compat.py | 45 | ||||
-rw-r--r-- | Lib/bsddb/test/test_cursor_pget_bug.py | 19 | ||||
-rw-r--r-- | Lib/bsddb/test/test_dbobj.py | 16 | ||||
-rw-r--r-- | Lib/bsddb/test/test_dbshelve.py | 81 | ||||
-rw-r--r-- | Lib/bsddb/test/test_get_none.py | 23 | ||||
-rw-r--r-- | Lib/bsddb/test/test_join.py | 22 | ||||
-rw-r--r-- | Lib/bsddb/test/test_recno.py | 16 | ||||
-rw-r--r-- | Lib/bsddb/test/test_sequence.py | 16 | ||||
-rw-r--r-- | Lib/bsddb/test/test_thread.py | 14 |
13 files changed, 244 insertions, 260 deletions
diff --git a/Lib/bsddb/dbshelve.py b/Lib/bsddb/dbshelve.py index 8eb7ed2..354229d 100644 --- a/Lib/bsddb/dbshelve.py +++ b/Lib/bsddb/dbshelve.py @@ -29,7 +29,7 @@ storage. #------------------------------------------------------------------------ -import cPickle +import pickle try: from UserDict import DictMixin except ImportError: @@ -104,11 +104,11 @@ class DBShelf(DictMixin): def __getitem__(self, key): data = self.db[key] - return cPickle.loads(data) + return pickle.loads(data) def __setitem__(self, key, value): - data = cPickle.dumps(value, self.binary) + data = pickle.dumps(value, self.binary) self.db[key] = data @@ -131,7 +131,7 @@ class DBShelf(DictMixin): newitems = [] for k, v in items: - newitems.append( (k, cPickle.loads(v)) ) + newitems.append( (k, pickle.loads(v)) ) return newitems def values(self, txn=None): @@ -140,13 +140,13 @@ class DBShelf(DictMixin): else: values = self.db.values() - return map(cPickle.loads, values) + return map(pickle.loads, values) #----------------------------------- # Other methods def __append(self, value, txn=None): - data = cPickle.dumps(value, self.binary) + data = pickle.dumps(value, self.binary) return self.db.append(data, txn) def append(self, value, txn=None): @@ -158,7 +158,7 @@ class DBShelf(DictMixin): def associate(self, secondaryDB, callback, flags=0): def _shelf_callback(priKey, priData, realCallback=callback): - data = cPickle.loads(priData) + data = pickle.loads(priData) return realCallback(priKey, data) return self.db.associate(secondaryDB, _shelf_callback, flags) @@ -171,15 +171,15 @@ class DBShelf(DictMixin): # off. data = self.db.get(*args, **kw) try: - return cPickle.loads(data) - except (TypeError, cPickle.UnpicklingError): + return pickle.loads(data) + except (TypeError, pickle.UnpicklingError, EOFError): return data # we may be getting the default value, or None, # so it doesn't need unpickled. def get_both(self, key, value, txn=None, flags=0): - data = cPickle.dumps(value, self.binary) + data = pickle.dumps(value, self.binary) data = self.db.get(key, data, txn, flags) - return cPickle.loads(data) + return pickle.loads(data) def cursor(self, txn=None, flags=0): @@ -189,7 +189,7 @@ class DBShelf(DictMixin): def put(self, key, value, txn=None, flags=0): - data = cPickle.dumps(value, self.binary) + data = pickle.dumps(value, self.binary) return self.db.put(key, data, txn, flags) @@ -233,7 +233,7 @@ class DBShelfCursor: def put(self, key, value, flags=0): - data = cPickle.dumps(value, self.binary) + data = pickle.dumps(value, self.binary) return self.dbc.put(key, data, flags) @@ -251,7 +251,7 @@ class DBShelfCursor: return self._extract(rec) def get_3(self, key, value, flags): - data = cPickle.dumps(value, self.binary) + data = pickle.dumps(value, self.binary) rec = self.dbc.get(key, flags) return self._extract(rec) @@ -268,7 +268,7 @@ class DBShelfCursor: def get_both(self, key, value, flags=0): - data = cPickle.dumps(value, self.binary) + data = pickle.dumps(value, self.binary) rec = self.dbc.get_both(key, flags) return self._extract(rec) @@ -292,7 +292,7 @@ class DBShelfCursor: return None else: key, data = rec - return key, cPickle.loads(data) + return key, pickle.loads(data) #---------------------------------------------- # Methods allowed to pass-through to self.dbc diff --git a/Lib/bsddb/dbtables.py b/Lib/bsddb/dbtables.py index 690e280..fda1273 100644 --- a/Lib/bsddb/dbtables.py +++ b/Lib/bsddb/dbtables.py @@ -90,8 +90,8 @@ class LikeCond(Cond): # # keys used to store database metadata # -_table_names_key = '__TABLE_NAMES__' # list of the tables in this db -_columns = '._COLUMNS__' # table_name+this key contains a list of columns +_table_names_key = b'__TABLE_NAMES__' # list of the tables in this db +_columns = b'._COLUMNS__' # table_name+this key contains a list of columns def _columns_key(table): return table + _columns diff --git a/Lib/bsddb/test/test_associate.py b/Lib/bsddb/test/test_associate.py index d8c9c0c..f0dbae7 100644 --- a/Lib/bsddb/test/test_associate.py +++ b/Lib/bsddb/test/test_associate.py @@ -176,8 +176,8 @@ class AssociateTestCase(unittest.TestCase): def addDataToDB(self, d, txn=None): for key, value in musicdata.items(): if type(self.keytype) == type(''): - key = "%02d" % key - d.put(key, '|'.join(value), txn=txn) + key = ("%02d" % key).encode("utf-8") + d.put(key, '|'.join(value).encode("utf-8"), txn=txn) def createDB(self, txn=None): self.cur = None @@ -247,14 +247,14 @@ class AssociateTestCase(unittest.TestCase): def finish_test(self, secDB, txn=None): # 'Blues' should not be in the secondary database - vals = secDB.pget('Blues', txn=txn) + vals = secDB.pget(b'Blues', txn=txn) assert vals == None, vals - vals = secDB.pget('Unknown', txn=txn) - assert vals[0] == 99 or vals[0] == '99', vals - vals[1].index('Unknown') - vals[1].index('Unnamed') - vals[1].index('unknown') + vals = secDB.pget(b'Unknown', txn=txn) + assert vals[0] == 99 or vals[0] == b'99', vals + vals[1].index(b'Unknown') + vals[1].index(b'Unnamed') + vals[1].index(b'unknown') if verbose: print("Primary key traversal:") @@ -279,18 +279,18 @@ class AssociateTestCase(unittest.TestCase): count = 0 # test cursor pget - vals = self.cur.pget('Unknown', flags=db.DB_LAST) - assert vals[1] == 99 or vals[1] == '99', vals - assert vals[0] == 'Unknown' - vals[2].index('Unknown') - vals[2].index('Unnamed') - vals[2].index('unknown') - - vals = self.cur.pget('Unknown', data='wrong value', flags=db.DB_GET_BOTH) + vals = self.cur.pget(b'Unknown', flags=db.DB_LAST) + assert vals[1] == 99 or vals[1] == b'99', vals + assert vals[0] == b'Unknown' + vals[2].index(b'Unknown') + vals[2].index(b'Unnamed') + vals[2].index(b'unknown') + + vals = self.cur.pget(b'Unknown', data=b'wrong value', flags=db.DB_GET_BOTH) assert vals == None, vals rec = self.cur.first() - assert rec[0] == "Jazz" + assert rec[0] == b"Jazz" while rec is not None: count = count + 1 if verbose: @@ -302,14 +302,15 @@ class AssociateTestCase(unittest.TestCase): self.cur = None def getGenre(self, priKey, priData): - assert type(priData) == type("") + assert type(priData) == type(b"") + priData = priData.decode("utf-8") if verbose: print('getGenre key: %r data: %r' % (priKey, priData)) genre = priData.split('|')[2] if genre == 'Blues': return db.DB_DONOTINDEX else: - return genre + return genre.encode("utf-8") #---------------------------------------------------------------------- @@ -382,7 +383,7 @@ class ShelveAssociateTestCase(AssociateTestCase): def addDataToDB(self, d): for key, value in musicdata.items(): if type(self.keytype) == type(''): - key = "%02d" % key + key = ("%02d" % key).encode("utf-8") d.put(key, value) # save the value as is this time @@ -394,7 +395,7 @@ class ShelveAssociateTestCase(AssociateTestCase): if genre == 'Blues': return db.DB_DONOTINDEX else: - return genre + return genre.encode("utf-8") class ShelveAssociateHashTestCase(ShelveAssociateTestCase): @@ -426,7 +427,7 @@ class ThreadedAssociateTestCase(AssociateTestCase): def writer1(self, d): for key, value in musicdata.items(): if type(self.keytype) == type(''): - key = "%02d" % key + key = ("%02d" % key).encode("utf-8") d.put(key, '|'.join(value)) def writer2(self, d): diff --git a/Lib/bsddb/test/test_basics.py b/Lib/bsddb/test/test_basics.py index 13f8ded..4f8eaea 100644 --- a/Lib/bsddb/test/test_basics.py +++ b/Lib/bsddb/test/test_basics.py @@ -22,7 +22,7 @@ except ImportError: from .test_all import verbose -DASH = '-' +DASH = b'-' #---------------------------------------------------------------------- @@ -116,13 +116,15 @@ class BasicTestCase(unittest.TestCase): for x in range(self._numKeys//2): key = '%04d' % (self._numKeys - x) # insert keys in reverse order + key = key.encode("utf-8") data = self.makeData(key) d.put(key, data, _txn) - d.put('empty value', '', _txn) + d.put(b'empty value', b'', _txn) for x in range(self._numKeys//2-1): key = '%04d' % x # and now some in forward order + key = key.encode("utf-8") data = self.makeData(key) d.put(key, data, _txn) @@ -148,20 +150,20 @@ class BasicTestCase(unittest.TestCase): print('\n', '-=' * 30) print("Running %s.test01_GetsAndPuts..." % self.__class__.__name__) - for key in ['0001', '0100', '0400', '0700', '0999']: + for key in [b'0001', b'0100', b'0400', b'0700', b'0999']: data = d.get(key) if verbose: print(data) - assert d.get('0321') == '0321-0321-0321-0321-0321' + assert d.get(b'0321') == b'0321-0321-0321-0321-0321' # By default non-existant keys return None... - assert d.get('abcd') == None + assert d.get(b'abcd') == None # ...but they raise exceptions in other situations. Call # set_get_returns_none() to change it. try: - d.delete('abcd') + d.delete(b'abcd') except db.DBNotFoundError as val: assert val.args[0] == db.DB_NOTFOUND if verbose: print(val) @@ -169,18 +171,18 @@ class BasicTestCase(unittest.TestCase): self.fail("expected exception") - d.put('abcd', 'a new record') - assert d.get('abcd') == 'a new record' + d.put(b'abcd', b'a new record') + assert d.get(b'abcd') == b'a new record' - d.put('abcd', 'same key') + d.put(b'abcd', b'same key') if self.dbsetflags & db.DB_DUP: - assert d.get('abcd') == 'a new record' + assert d.get(b'abcd') == b'a new record' else: - assert d.get('abcd') == 'same key' + assert d.get(b'abcd') == b'same key' try: - d.put('abcd', 'this should fail', flags=db.DB_NOOVERWRITE) + d.put(b'abcd', b'this should fail', flags=db.DB_NOOVERWRITE) except db.DBKeyExistError as val: assert val.args[0] == db.DB_KEYEXIST if verbose: print(val) @@ -188,9 +190,9 @@ class BasicTestCase(unittest.TestCase): self.fail("expected exception") if self.dbsetflags & db.DB_DUP: - assert d.get('abcd') == 'a new record' + assert d.get(b'abcd') == b'a new record' else: - assert d.get('abcd') == 'same key' + assert d.get(b'abcd') == b'same key' d.sync() @@ -204,24 +206,24 @@ class BasicTestCase(unittest.TestCase): self.d.open(self.filename) d = self.d - assert d.get('0321') == '0321-0321-0321-0321-0321' + assert d.get(b'0321') == b'0321-0321-0321-0321-0321' if self.dbsetflags & db.DB_DUP: - assert d.get('abcd') == 'a new record' + assert d.get(b'abcd') == b'a new record' else: - assert d.get('abcd') == 'same key' + assert d.get(b'abcd') == b'same key' - rec = d.get_both('0555', '0555-0555-0555-0555-0555') + rec = d.get_both(b'0555', b'0555-0555-0555-0555-0555') if verbose: print(rec) - assert d.get_both('0555', 'bad data') == None + assert d.get_both(b'0555', b'bad data') == None # test default value - data = d.get('bad key', 'bad data') - assert data == 'bad data' + data = d.get(b'bad key', b'bad data') + assert data == b'bad data' # any object can pass through - data = d.get('bad key', self) + data = d.get(b'bad key', self) assert data == self s = d.stat() @@ -241,7 +243,7 @@ class BasicTestCase(unittest.TestCase): print("Running %s.test02_DictionaryMethods..." % \ self.__class__.__name__) - for key in ['0002', '0101', '0401', '0701', '0998']: + for key in [b'0002', b'0101', b'0401', b'0701', b'0998']: data = d[key] assert data == self.makeData(key) if verbose: @@ -252,12 +254,12 @@ class BasicTestCase(unittest.TestCase): assert len(keys) == self._numKeys assert type(keys) == type([]) - d['new record'] = 'a new record' + d[b'new record'] = b'a new record' assert len(d) == self._numKeys+1 keys = d.keys() assert len(keys) == self._numKeys+1 - d['new record'] = 'a replacement record' + d[b'new record'] = b'a replacement record' assert len(d) == self._numKeys+1 keys = d.keys() assert len(keys) == self._numKeys+1 @@ -266,10 +268,10 @@ class BasicTestCase(unittest.TestCase): print("the first 10 keys are:") pprint(keys[:10]) - assert d['new record'] == 'a replacement record' + assert d[b'new record'] == b'a replacement record' - assert d.has_key('0001') == 1 - assert d.has_key('spam') == 0 + assert d.has_key(b'0001') == 1 + assert d.has_key(b'spam') == 0 items = d.items() assert len(items) == self._numKeys+1 @@ -343,20 +345,20 @@ class BasicTestCase(unittest.TestCase): assert count == self._numKeys - rec = c.set('0505') + rec = c.set(b'0505') rec2 = c.current() - assert rec == rec2 - assert rec[0] == '0505' - assert rec[1] == self.makeData('0505') + assert rec == rec2, (repr(rec),repr(rec2)) + assert rec[0] == b'0505' + assert rec[1] == self.makeData(b'0505') assert c.get_current_size() == len(rec[1]) # make sure we get empty values properly - rec = c.set('empty value') - assert rec[1] == '' + rec = c.set(b'empty value') + assert rec[1] == b'' assert c.get_current_size() == 0 try: - n = c.set('bad key') + n = c.set(b'bad key') except db.DBNotFoundError as val: assert val.args[0] == db.DB_NOTFOUND if verbose: print(val) @@ -366,11 +368,11 @@ class BasicTestCase(unittest.TestCase): if n != None: self.fail("expected None: %r" % (n,)) - rec = c.get_both('0404', self.makeData('0404')) - assert rec == ('0404', self.makeData('0404')) + rec = c.get_both(b'0404', self.makeData(b'0404')) + assert rec == (b'0404', self.makeData(b'0404')) try: - n = c.get_both('0404', 'bad data') + n = c.get_both(b'0404', b'bad data') except db.DBNotFoundError as val: assert val.args[0] == db.DB_NOTFOUND if verbose: print(val) @@ -381,21 +383,21 @@ class BasicTestCase(unittest.TestCase): self.fail("expected None: %r" % (n,)) if self.d.get_type() == db.DB_BTREE: - rec = c.set_range('011') + rec = c.set_range(b'011') if verbose: print("searched for '011', found: ", rec) - rec = c.set_range('011',dlen=0,doff=0) + rec = c.set_range(b'011',dlen=0,doff=0) if verbose: print("searched (partial) for '011', found: ", rec) - if rec[1] != '': self.fail('expected empty data portion') + if rec[1] != b'': self.fail('expected empty data portion') - ev = c.set_range('empty value') + ev = c.set_range(b'empty value') if verbose: print("search for 'empty value' returned", ev) - if ev[1] != '': self.fail('empty value lookup failed') + if ev[1] != b'': self.fail('empty value lookup failed') - c.set('0499') + c.set(b'0499') c.delete() try: rec = c.current() @@ -413,12 +415,12 @@ class BasicTestCase(unittest.TestCase): c2 = c.dup(db.DB_POSITION) assert c.current() == c2.current() - c2.put('', 'a new value', db.DB_CURRENT) + c2.put(b'', b'a new value', db.DB_CURRENT) assert c.current() == c2.current() - assert c.current()[1] == 'a new value' + assert c.current()[1] == b'a new value' - c2.put('', 'er', db.DB_CURRENT, dlen=0, doff=5) - assert c2.current()[1] == 'a newer value' + c2.put(b'', b'er', db.DB_CURRENT, dlen=0, doff=5) + assert c2.current()[1] == b'a newer value' c.close() c2.close() @@ -507,14 +509,14 @@ class BasicTestCase(unittest.TestCase): print("Running %s.test04_PartialGetAndPut..." % \ self.__class__.__name__) - key = "partialTest" - data = "1" * 1000 + "2" * 1000 + key = b"partialTest" + data = b"1" * 1000 + b"2" * 1000 d.put(key, data) assert d.get(key) == data - assert d.get(key, dlen=20, doff=990) == ("1" * 10) + ("2" * 10) + assert d.get(key, dlen=20, doff=990) == (b"1" * 10) + (b"2" * 10) - d.put("partialtest2", ("1" * 30000) + "robin" ) - assert d.get("partialtest2", dlen=5, doff=30000) == "robin" + d.put(b"partialtest2", (b"1" * 30000) + b"robin" ) + assert d.get(b"partialtest2", dlen=5, doff=30000) == b"robin" # There seems to be a bug in DB here... Commented out the test for # now. @@ -522,11 +524,11 @@ class BasicTestCase(unittest.TestCase): if self.dbsetflags != db.DB_DUP: # Partial put with duplicate records requires a cursor - d.put(key, "0000", dlen=2000, doff=0) - assert d.get(key) == "0000" + d.put(key, b"0000", dlen=2000, doff=0) + assert d.get(key) == b"0000" - d.put(key, "1111", dlen=1, doff=2) - assert d.get(key) == "0011110" + d.put(key, b"1111", dlen=1, doff=2) + assert d.get(key) == b"0011110" #---------------------------------------- @@ -537,9 +539,9 @@ class BasicTestCase(unittest.TestCase): print("Running %s.test05_GetSize..." % self.__class__.__name__) for i in range(1, 50000, 500): - key = "size%s" % i + key = ("size%s" % i).encode("utf-8") #print "before ", i, - d.put(key, "1" * i) + d.put(key, b"1" * i) #print "after", assert d.get_size(key) == i #print "done" @@ -556,7 +558,7 @@ class BasicTestCase(unittest.TestCase): print('\n', '-=' * 30) print("Running %s.test99_Truncate..." % self.__class__.__name__) - d.put("abcde", "ABCDE"); + d.put(b"abcde", b"ABCDE"); num = d.truncate() assert num >= 1, "truncate returned <= 0 on non-empty database" num = d.truncate() @@ -650,19 +652,19 @@ class BasicTransactionTestCase(BasicTestCase): print('\n', '-=' * 30) print("Running %s.test06_Transactions..." % self.__class__.__name__) - assert d.get('new rec', txn=self.txn) == None - d.put('new rec', 'this is a new record', self.txn) - assert d.get('new rec', txn=self.txn) == 'this is a new record' + assert d.get(b'new rec', txn=self.txn) == None + d.put(b'new rec', b'this is a new record', self.txn) + assert d.get(b'new rec', txn=self.txn) == b'this is a new record' self.txn.abort() - assert d.get('new rec') == None + assert d.get(b'new rec') == None self.txn = self.env.txn_begin() - assert d.get('new rec', txn=self.txn) == None - d.put('new rec', 'this is a new record', self.txn) - assert d.get('new rec', txn=self.txn) == 'this is a new record' + assert d.get(b'new rec', txn=self.txn) == None + d.put(b'new rec', b'this is a new record', self.txn) + assert d.get(b'new rec', txn=self.txn) == b'this is a new record' self.txn.commit() - assert d.get('new rec') == 'this is a new record' + assert d.get(b'new rec') == b'this is a new record' self.txn = self.env.txn_begin() c = d.cursor(self.txn) @@ -715,7 +717,7 @@ class BasicTransactionTestCase(BasicTestCase): print('\n', '-=' * 30) print("Running %s.test07_TxnTruncate..." % self.__class__.__name__) - d.put("abcde", "ABCDE"); + d.put(b"abcde", b"ABCDE"); txn = self.env.txn_begin() num = d.truncate(txn) assert num >= 1, "truncate returned <= 0 on non-empty database" @@ -772,7 +774,7 @@ class BTreeRecnoTestCase(BasicTestCase): print("Record #200 is ", rec) c = d.cursor() - c.set('0200') + c.set(b'0200') num = c.get_recno() assert type(num) == type(1) if verbose: @@ -800,39 +802,40 @@ class BasicDUPTestCase(BasicTestCase): print("Running %s.test08_DuplicateKeys..." % \ self.__class__.__name__) - d.put("dup0", "before") + d.put(b"dup0", b"before") for x in "The quick brown fox jumped over the lazy dog.".split(): - d.put("dup1", x) - d.put("dup2", "after") + x = x.encode("ascii") + d.put(b"dup1", x) + d.put(b"dup2", b"after") - data = d.get("dup1") - assert data == "The" + data = d.get(b"dup1") + assert data == b"The" if verbose: print(data) c = d.cursor() - rec = c.set("dup1") - assert rec == ('dup1', 'The') + rec = c.set(b"dup1") + assert rec == (b'dup1', b'The') next = c.next() - assert next == ('dup1', 'quick') + assert next == (b'dup1', b'quick') - rec = c.set("dup1") + rec = c.set(b"dup1") count = c.count() assert count == 9 next_dup = c.next_dup() - assert next_dup == ('dup1', 'quick') + assert next_dup == (b'dup1', b'quick') - rec = c.set('dup1') + rec = c.set(b'dup1') while rec is not None: if verbose: print(rec) rec = c.next_dup() - c.set('dup1') + c.set(b'dup1') rec = c.next_nodup() - assert rec[0] != 'dup1' + assert rec[0] != b'dup1' if verbose: print(rec) @@ -880,9 +883,11 @@ class BasicMultiDBTestCase(BasicTestCase): self.dbopenflags|db.DB_CREATE) for x in "The quick brown fox jumped over the lazy dog".split(): + x = x.encode("ascii") d2.put(x, self.makeData(x)) for x in string.letters: + x = x.encode("ascii") d3.put(x, x*70) d1.sync() diff --git a/Lib/bsddb/test/test_compat.py b/Lib/bsddb/test/test_compat.py index 2908844..751a2bb 100644 --- a/Lib/bsddb/test/test_compat.py +++ b/Lib/bsddb/test/test_compat.py @@ -9,12 +9,7 @@ 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 +from bsddb import db, hashopen, btopen, rnopen class CompatibilityTestCase(unittest.TestCase): @@ -41,31 +36,31 @@ class CompatibilityTestCase(unittest.TestCase): f = rnopen(self.filename, 'c') for x in range(len(data)): - f[x+1] = data[x] + f[x+1] = data[x].encode("ascii") getTest = (f[1], f[2], f[3]) if verbose: print('%s %s %s' % getTest) - assert getTest[1] == 'quick', 'data mismatch!' + assert getTest[1] == b'quick', 'data mismatch!' rv = f.set_location(3) - if rv != (3, 'brown'): + if rv != (3, b'brown'): self.fail('recno database set_location failed: '+repr(rv)) - f[25] = 'twenty-five' + f[25] = b'twenty-five' f.close() del f f = rnopen(self.filename, 'w') - f[20] = 'twenty' + f[20] = b'twenty' def noRec(f): rec = f[15] self.assertRaises(KeyError, noRec, f) def badKey(f): - rec = f['a string'] + rec = f[b'a string'] self.assertRaises(TypeError, badKey, f) del f[3] @@ -101,20 +96,20 @@ class CompatibilityTestCase(unittest.TestCase): else: if verbose: print("truth test: false") - f['0'] = '' - f['a'] = 'Guido' - f['b'] = 'van' - f['c'] = 'Rossum' - f['d'] = 'invented' + f[b'0'] = b'' + f[b'a'] = b'Guido' + f[b'b'] = b'van' + f[b'c'] = b'Rossum' + f[b'd'] = b'invented' # 'e' intentionally left out - f['f'] = 'Python' + f[b'f'] = b'Python' if verbose: print('%s %s %s' % (f['a'], f['b'], f['c'])) if verbose: print('key ordering...') start = f.set_location(f.first()[0]) - if start != ('0', ''): + if start != (b'0', b''): self.fail("incorrect first() result: "+repr(start)) while 1: try: @@ -126,17 +121,17 @@ class CompatibilityTestCase(unittest.TestCase): if verbose: print(rec) - assert f.has_key('f'), 'Error, missing key!' + assert f.has_key(b'f'), 'Error, missing key!' # test that set_location() returns the next nearest key, value # on btree databases and raises KeyError on others. if factory == btopen: - e = f.set_location('e') - if e != ('f', 'Python'): + e = f.set_location(b'e') + if e != (b'f', b'Python'): self.fail('wrong key,value returned: '+repr(e)) else: try: - e = f.set_location('e') + e = f.set_location(b'e') except KeyError: pass else: @@ -160,7 +155,7 @@ class CompatibilityTestCase(unittest.TestCase): if verbose: print('modification...') f = factory(self.filename, 'w') - f['d'] = 'discovered' + f[b'd'] = b'discovered' if verbose: print('access...') @@ -170,7 +165,7 @@ class CompatibilityTestCase(unittest.TestCase): print(word) def noRec(f): - rec = f['no such key'] + rec = f[b'no such key'] self.assertRaises(KeyError, noRec, f) def badKey(f): diff --git a/Lib/bsddb/test/test_cursor_pget_bug.py b/Lib/bsddb/test/test_cursor_pget_bug.py index de47e6d..ac60cb4 100644 --- a/Lib/bsddb/test/test_cursor_pget_bug.py +++ b/Lib/bsddb/test/test_cursor_pget_bug.py @@ -1,12 +1,7 @@ import unittest import sys, os, glob -try: - # For Pythons w/distutils pybsddb - from bsddb3 import db -except ImportError: - # For Python 2.3 - from bsddb import db +from bsddb import db #---------------------------------------------------------------------- @@ -29,9 +24,9 @@ class pget_bugTestCase(unittest.TestCase): self.secondary_db.set_flags(db.DB_DUP) self.secondary_db.open(self.db_name, 'secondary', db.DB_BTREE, db.DB_CREATE) self.primary_db.associate(self.secondary_db, lambda key, data: data) - self.primary_db.put('salad', 'eggs') - self.primary_db.put('spam', 'ham') - self.primary_db.put('omelet', 'eggs') + self.primary_db.put(b'salad', b'eggs') + self.primary_db.put(b'spam', b'ham') + self.primary_db.put(b'omelet', b'eggs') def tearDown(self): @@ -48,11 +43,11 @@ class pget_bugTestCase(unittest.TestCase): def test_pget(self): cursor = self.secondary_db.cursor() - self.assertEquals(('eggs', 'salad', 'eggs'), cursor.pget(key='eggs', flags=db.DB_SET)) - self.assertEquals(('eggs', 'omelet', 'eggs'), cursor.pget(db.DB_NEXT_DUP)) + self.assertEquals((b'eggs', b'salad', b'eggs'), cursor.pget(key=b'eggs', flags=db.DB_SET)) + self.assertEquals((b'eggs', b'omelet', b'eggs'), cursor.pget(db.DB_NEXT_DUP)) self.assertEquals(None, cursor.pget(db.DB_NEXT_DUP)) - self.assertEquals(('ham', 'spam', 'ham'), cursor.pget('ham', 'spam', flags=db.DB_SET)) + self.assertEquals((b'ham', b'spam', b'ham'), cursor.pget(b'ham', b'spam', flags=db.DB_SET)) self.assertEquals(None, cursor.pget(db.DB_NEXT_DUP)) cursor.close() diff --git a/Lib/bsddb/test/test_dbobj.py b/Lib/bsddb/test/test_dbobj.py index 27fffe0..f8353f3 100644 --- a/Lib/bsddb/test/test_dbobj.py +++ b/Lib/bsddb/test/test_dbobj.py @@ -38,17 +38,17 @@ class dbobjTestCase(unittest.TestCase): class TestDBEnv(dbobj.DBEnv): pass class TestDB(dbobj.DB): def put(self, key, *args, **kwargs): - key = key.upper() + key = key.decode("ascii").upper().encode("ascii") # call our parent classes put method with an upper case key return dbobj.DB.put(self, key, *args, **kwargs) self.env = TestDBEnv() self.env.open(self.homeDir, db.DB_CREATE | db.DB_INIT_MPOOL) self.db = TestDB(self.env) self.db.open(self.db_name, db.DB_HASH, db.DB_CREATE) - self.db.put('spam', 'eggs') - assert self.db.get('spam') == None, \ + self.db.put(b'spam', b'eggs') + assert self.db.get(b'spam') == None, \ "overridden dbobj.DB.put() method failed [1]" - assert self.db.get('SPAM') == 'eggs', \ + assert self.db.get(b'SPAM') == b'eggs', \ "overridden dbobj.DB.put() method failed [2]" self.db.close() self.env.close() @@ -59,14 +59,14 @@ class dbobjTestCase(unittest.TestCase): self.db = dbobj.DB(self.env) self.db.open(self.db_name+'02', db.DB_HASH, db.DB_CREATE) # __setitem__ - self.db['spam'] = 'eggs' + self.db[b'spam'] = b'eggs' # __len__ assert len(self.db) == 1 # __getitem__ - assert self.db['spam'] == 'eggs' + assert self.db[b'spam'] == b'eggs' # __del__ - del self.db['spam'] - assert self.db.get('spam') == None, "dbobj __del__ failed" + del self.db[b'spam'] + assert self.db.get(b'spam') == None, "dbobj __del__ failed" self.db.close() self.env.close() diff --git a/Lib/bsddb/test/test_dbshelve.py b/Lib/bsddb/test/test_dbshelve.py index e75e9c8..f773156 100644 --- a/Lib/bsddb/test/test_dbshelve.py +++ b/Lib/bsddb/test/test_dbshelve.py @@ -8,12 +8,7 @@ from pprint import pprint 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 +from bsddb import db, dbshelve from .test_all import verbose @@ -56,15 +51,15 @@ class DBShelveTestCase(unittest.TestCase): def populateDB(self, d): for x in string.letters: - d['S' + x] = 10 * x # add a string - d['I' + x] = ord(x) # add an integer - d['L' + x] = [x] * 10 # add a list + d[('S' + x).encode("ascii")] = 10 * x # add a string + d[('I' + x).encode("ascii")] = ord(x) # add an integer + d[('L' + x).encode("ascii")] = [x] * 10 # add a list inst = DataClass() # add an instance inst.S = 10 * x inst.I = ord(x) inst.L = [x] * 10 - d['O' + x] = inst + d[('O' + x).encode("ascii")] = inst # overridable in derived classes to affect how the shelf is created/opened @@ -98,14 +93,14 @@ class DBShelveTestCase(unittest.TestCase): print("keys:", k) print("stats:", s) - assert 0 == d.has_key('bad key') - assert 1 == d.has_key('IA') - assert 1 == d.has_key('OA') + assert 0 == d.has_key(b'bad key') + assert 1 == d.has_key(b'IA') + assert 1 == d.has_key(b'OA') - d.delete('IA') - del d['OA'] - assert 0 == d.has_key('IA') - assert 0 == d.has_key('OA') + d.delete(b'IA') + del d[b'OA'] + assert 0 == d.has_key(b'IA') + assert 0 == d.has_key(b'OA') assert len(d) == l-2 values = [] @@ -127,18 +122,18 @@ class DBShelveTestCase(unittest.TestCase): for key, value in items: self.checkrec(key, value) - assert d.get('bad key') == None - assert d.get('bad key', None) == None - assert d.get('bad key', 'a string') == 'a string' - assert d.get('bad key', [1, 2, 3]) == [1, 2, 3] + assert d.get(b'bad key') == None + assert d.get(b'bad key', None) == None + assert d.get(b'bad key', b'a string') == b'a string' + assert d.get(b'bad key', [1, 2, 3]) == [1, 2, 3] d.set_get_returns_none(0) - self.assertRaises(db.DBNotFoundError, d.get, 'bad key') + self.assertRaises(db.DBNotFoundError, d.get, b'bad key') d.set_get_returns_none(1) - d.put('new key', 'new data') - assert d.get('new key') == 'new data' - assert d['new key'] == 'new data' + d.put(b'new key', b'new data') + assert d.get(b'new key') == b'new data' + assert d[b'new key'] == b'new data' @@ -156,7 +151,7 @@ class DBShelveTestCase(unittest.TestCase): while rec is not None: count = count + 1 if verbose: - print(rec) + print(repr(rec)) key, value = rec self.checkrec(key, value) rec = c.next() @@ -177,34 +172,32 @@ class DBShelveTestCase(unittest.TestCase): assert count == len(d) - c.set('SS') + c.set(b'SS') key, value = c.current() self.checkrec(key, value) del c - - def checkrec(self, key, value): - x = key[1] - if key[0] == 'S': - assert type(value) == StringType - assert value == 10 * x + x = key[1:] + if key[0:1] == b'S': + self.assertEquals(type(value), str) + self.assertEquals(value, 10 * x.decode("ascii")) - elif key[0] == 'I': - assert type(value) == IntType - assert value == ord(x) + elif key[0:1] == b'I': + self.assertEquals(type(value), int) + self.assertEquals(value, ord(x)) - elif key[0] == 'L': - assert type(value) == ListType - assert value == [x] * 10 + elif key[0:1] == b'L': + self.assertEquals(type(value), list) + self.assertEquals(value, [x.decode("ascii")] * 10) - elif key[0] == 'O': - assert value.S == 10 * x - assert value.I == ord(x) - assert value.L == [x] * 10 + elif key[0:1] == b'O': + self.assertEquals(value.S, 10 * x.decode("ascii")) + self.assertEquals(value.I, ord(x)) + self.assertEquals(value.L, [x.decode("ascii")] * 10) else: - raise AssertionError, 'Unknown key type, fix the test' + self.fail('Unknown key type, fix the test') #---------------------------------------------------------------------- diff --git a/Lib/bsddb/test/test_get_none.py b/Lib/bsddb/test/test_get_none.py index d1b69c7..63130d5 100644 --- a/Lib/bsddb/test/test_get_none.py +++ b/Lib/bsddb/test/test_get_none.py @@ -7,12 +7,7 @@ import tempfile 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 +from bsddb import db from .test_all import verbose @@ -36,13 +31,14 @@ class GetReturnsNoneTestCase(unittest.TestCase): d.set_get_returns_none(1) for x in string.letters: + x = x.encode("ascii") d.put(x, x * 40) - data = d.get('bad key') + data = d.get(b'bad key') assert data == None - data = d.get('a') - assert data == 'a'*40 + data = d.get(b'a') + assert data == b'a'*40 count = 0 c = d.cursor() @@ -64,13 +60,14 @@ class GetReturnsNoneTestCase(unittest.TestCase): d.set_get_returns_none(0) for x in string.letters: + x = x.encode("ascii") d.put(x, x * 40) - self.assertRaises(db.DBNotFoundError, d.get, 'bad key') - self.assertRaises(KeyError, d.get, 'bad key') + self.assertRaises(db.DBNotFoundError, d.get, b'bad key') + self.assertRaises(KeyError, d.get, b'bad key') - data = d.get('a') - assert data == 'a'*40 + data = d.get(b'a') + assert data == b'a'*40 count = 0 exceptionHappened = 0 diff --git a/Lib/bsddb/test/test_join.py b/Lib/bsddb/test/test_join.py index 46178b0..0e41152 100644 --- a/Lib/bsddb/test/test_join.py +++ b/Lib/bsddb/test/test_join.py @@ -15,12 +15,7 @@ except ImportError: 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 +from bsddb import db, dbshelve, StringKeys #---------------------------------------------------------------------- @@ -44,6 +39,9 @@ ColorIndex = [ ('black', "shotgun"), ] +def ASCII(s): + return s.encode("ascii") + class JoinTestCase(unittest.TestCase): keytype = '' @@ -72,13 +70,13 @@ class JoinTestCase(unittest.TestCase): # create and populate primary index priDB = db.DB(self.env) priDB.open(self.filename, "primary", db.DB_BTREE, db.DB_CREATE) - [priDB.put(*t) for t in ProductIndex] + [priDB.put(ASCII(k),ASCII(v)) for k,v in ProductIndex] # create and populate secondary index secDB = db.DB(self.env) secDB.set_flags(db.DB_DUP | db.DB_DUPSORT) secDB.open(self.filename, "secondary", db.DB_BTREE, db.DB_CREATE) - [secDB.put(*t) for t in ColorIndex] + [secDB.put(ASCII(k),ASCII(v)) for k,v in ColorIndex] sCursor = None jCursor = None @@ -87,7 +85,7 @@ class JoinTestCase(unittest.TestCase): sCursor = secDB.cursor() # Don't do the .set() in an assert, or you can get a bogus failure # when running python -O - tmp = sCursor.set('red') + tmp = sCursor.set(b'red') assert tmp # FIXME: jCursor doesn't properly hold a reference to its @@ -95,11 +93,11 @@ class JoinTestCase(unittest.TestCase): # can cause a crash. jCursor = priDB.join([sCursor]) - if jCursor.get(0) != ('apple', "Convenience Store"): + if jCursor.get(0) != (b'apple', b"Convenience Store"): self.fail("join cursor positioned wrong") - if jCursor.join_item() != 'chainsaw': + if jCursor.join_item() != b'chainsaw': self.fail("DBCursor.join_item returned wrong item") - if jCursor.get(0)[0] != 'strawberry': + if jCursor.get(0)[0] != b'strawberry': self.fail("join cursor returned wrong thing") if jCursor.get(0): # there were only three red items to return self.fail("join cursor returned too many items") diff --git a/Lib/bsddb/test/test_recno.py b/Lib/bsddb/test/test_recno.py index a05f23c..3da5006 100644 --- a/Lib/bsddb/test/test_recno.py +++ b/Lib/bsddb/test/test_recno.py @@ -58,7 +58,7 @@ class SimpleRecnoTestCase(unittest.TestCase): if verbose: print(data) - assert type(data) == type("") + assert type(data) == bytes assert data == d.get(recno) try: @@ -106,7 +106,7 @@ class SimpleRecnoTestCase(unittest.TestCase): assert type(items[0]) == type(()) assert len(items[0]) == 2 assert type(items[0][0]) == type(123) - assert type(items[0][1]) == type("") + assert type(items[0][1]) == bytes assert len(items) == len(d) assert d.has_key(25) @@ -117,8 +117,8 @@ class SimpleRecnoTestCase(unittest.TestCase): d.delete(13) assert not d.has_key(13) - data = d.get_both(26, "z" * 60) - assert data == "z" * 60, 'was %r' % data + data = d.get_both(26, b"z" * 60) + assert data == b"z" * 60, 'was %r' % data if verbose: print(data) @@ -138,11 +138,11 @@ class SimpleRecnoTestCase(unittest.TestCase): if verbose: print(rec) - c.put(-1, "a replacement record", db.DB_CURRENT) + c.put(-1, b"a replacement record", db.DB_CURRENT) c.set(50) rec = c.current() - assert rec == (50, "a replacement record") + assert rec == (50, b"a replacement record") if verbose: print(rec) @@ -165,8 +165,8 @@ class SimpleRecnoTestCase(unittest.TestCase): c = d.cursor() # put a record beyond the consecutive end of the recno's - d[100] = "way out there" - assert d[100] == "way out there" + d[100] = b"way out there" + assert d[100] == b"way out there" try: data = d[99] diff --git a/Lib/bsddb/test/test_sequence.py b/Lib/bsddb/test/test_sequence.py index 350d926..3329b76 100644 --- a/Lib/bsddb/test/test_sequence.py +++ b/Lib/bsddb/test/test_sequence.py @@ -50,39 +50,39 @@ class DBSequenceTest(unittest.TestCase): start_value = 10 * self.int_32_max self.assertEqual(0xA00000000, start_value) self.assertEquals(None, self.seq.init_value(start_value)) - self.assertEquals(None, self.seq.open(key='id', txn=None, flags=db.DB_CREATE)) + self.assertEquals(None, self.seq.open(key=b'id', txn=None, flags=db.DB_CREATE)) self.assertEquals(start_value, self.seq.get(5)) self.assertEquals(start_value + 5, self.seq.get()) def test_remove(self): self.seq = db.DBSequence(self.d, flags=0) - self.assertEquals(None, self.seq.open(key='foo', txn=None, flags=db.DB_CREATE)) + self.assertEquals(None, self.seq.open(key=b'foo', txn=None, flags=db.DB_CREATE)) self.assertEquals(None, self.seq.remove(txn=None, flags=0)) del self.seq def test_get_key(self): self.seq = db.DBSequence(self.d, flags=0) - key = 'foo' + key = b'foo' self.assertEquals(None, self.seq.open(key=key, txn=None, flags=db.DB_CREATE)) self.assertEquals(key, self.seq.get_key()) def test_get_dbp(self): self.seq = db.DBSequence(self.d, flags=0) - self.assertEquals(None, self.seq.open(key='foo', txn=None, flags=db.DB_CREATE)) + self.assertEquals(None, self.seq.open(key=b'foo', txn=None, flags=db.DB_CREATE)) self.assertEquals(self.d, self.seq.get_dbp()) def test_cachesize(self): self.seq = db.DBSequence(self.d, flags=0) cashe_size = 10 self.assertEquals(None, self.seq.set_cachesize(cashe_size)) - self.assertEquals(None, self.seq.open(key='foo', txn=None, flags=db.DB_CREATE)) + self.assertEquals(None, self.seq.open(key=b'foo', txn=None, flags=db.DB_CREATE)) self.assertEquals(cashe_size, self.seq.get_cachesize()) def test_flags(self): self.seq = db.DBSequence(self.d, flags=0) flag = db.DB_SEQ_WRAP; self.assertEquals(None, self.seq.set_flags(flag)) - self.assertEquals(None, self.seq.open(key='foo', txn=None, flags=db.DB_CREATE)) + self.assertEquals(None, self.seq.open(key=b'foo', txn=None, flags=db.DB_CREATE)) self.assertEquals(flag, self.seq.get_flags() & flag) def test_range(self): @@ -90,12 +90,12 @@ class DBSequenceTest(unittest.TestCase): seq_range = (10 * self.int_32_max, 11 * self.int_32_max - 1) self.assertEquals(None, self.seq.set_range(seq_range)) self.seq.init_value(seq_range[0]) - self.assertEquals(None, self.seq.open(key='foo', txn=None, flags=db.DB_CREATE)) + self.assertEquals(None, self.seq.open(key=b'foo', txn=None, flags=db.DB_CREATE)) self.assertEquals(seq_range, self.seq.get_range()) def test_stat(self): self.seq = db.DBSequence(self.d, flags=0) - self.assertEquals(None, self.seq.open(key='foo', txn=None, flags=db.DB_CREATE)) + self.assertEquals(None, self.seq.open(key=b'foo', txn=None, flags=db.DB_CREATE)) stat = self.seq.stat() for param in ('nowait', 'min', 'max', 'value', 'current', 'flags', 'cache_size', 'last_value', 'wait'): diff --git a/Lib/bsddb/test/test_thread.py b/Lib/bsddb/test/test_thread.py index df112b7..07133d3 100644 --- a/Lib/bsddb/test/test_thread.py +++ b/Lib/bsddb/test/test_thread.py @@ -10,7 +10,7 @@ import tempfile from pprint import pprint from random import random -DASH = '-' +DASH = b'-' try: from threading import Thread, currentThread @@ -120,7 +120,7 @@ class ConcurrentDataStoreBase(BaseThreadedTestCase): print("%s: creating records %d - %d" % (name, start, stop)) for x in range(start, stop): - key = '%04d' % x + key = ('%04d' % x).encode("ascii") dbutils.DeadlockWrap(d.put, key, self.makeData(key), max_retries=12) if verbose and x % 100 == 0: @@ -224,7 +224,7 @@ class SimpleThreadedBase(BaseThreadedTestCase): # create a bunch of records for x in range(start, stop): - key = '%04d' % x + key = ('%04d' % x).encode("ascii") dbutils.DeadlockWrap(d.put, key, self.makeData(key), max_retries=12) @@ -234,7 +234,7 @@ class SimpleThreadedBase(BaseThreadedTestCase): # do a bit or reading too if random() <= 0.05: for y in range(start, x): - key = '%04d' % x + key = ('%04d' % x).encode("ascii") data = dbutils.DeadlockWrap(d.get, key, max_retries=12) self.assertEqual(data, self.makeData(key)) @@ -247,7 +247,7 @@ class SimpleThreadedBase(BaseThreadedTestCase): # read them back, deleting a few for x in range(start, stop): - key = '%04d' % x + key = ('%04d' % x).encode("ascii") data = dbutils.DeadlockWrap(d.get, key, max_retries=12) if verbose and x % 100 == 0: print("%s: fetched record (%s, %s)" % (name, key, data)) @@ -348,7 +348,7 @@ class ThreadedTransactionsBase(BaseThreadedTestCase): try: txn = self.env.txn_begin(None, self.txnFlag) for x in range(start, stop): - key = '%04d' % x + key = ('%04d' % x).encode("ascii") d.put(key, self.makeData(key), txn) if verbose and x % 100 == 0: print("%s: records %d - %d finished" % (name, start, x)) @@ -383,7 +383,7 @@ class ThreadedTransactionsBase(BaseThreadedTestCase): txn = self.env.txn_begin(None, self.txnFlag) for x in range(10): key = int(random() * howMany) + start - key = '%04d' % key + key = ('%04d' % key).encode("ascii") data = d.get(key, None, txn, db.DB_RMW) if data is not None: d.delete(key, txn) |