Commit 361ed153 authored by Gregory P. Smith's avatar Gregory P. Smith

Require strict bytes objects for all bsddb.db input values.

parent e5a8dc68
...@@ -88,9 +88,9 @@ class TableDBTestCase(unittest.TestCase): ...@@ -88,9 +88,9 @@ class TableDBTestCase(unittest.TestCase):
col1 = 'but can it fly?' col1 = 'but can it fly?'
col2 = 'Species' col2 = 'Species'
testinfo = [ testinfo = [
{col0: pickle.dumps(8, 1), col1: 'no', col2: 'Penguin'}, {col0: pickle.dumps(8, 1), col1: b'no', col2: b'Penguin'},
{col0: pickle.dumps(-1, 1), col1: 'no', col2: 'Turkey'}, {col0: pickle.dumps(-1, 1), col1: b'no', col2: b'Turkey'},
{col0: pickle.dumps(9, 1), col1: 'yes', col2: 'SR-71A Blackbird'} {col0: pickle.dumps(9, 1), col1: b'yes', col2: b'SR-71A Blackbird'}
] ]
try: try:
...@@ -150,22 +150,22 @@ class TableDBTestCase(unittest.TestCase): ...@@ -150,22 +150,22 @@ class TableDBTestCase(unittest.TestCase):
pass pass
self.tdb.Insert(tabname, self.tdb.Insert(tabname,
{'a': '42', {'a': b'42',
'b': "bad", 'b': b'bad',
'c': "meep", 'c': b'meep',
'e': 'Fuzzy wuzzy was a bear'}) 'e': b'Fuzzy wuzzy was a bear'})
self.tdb.Insert(tabname, self.tdb.Insert(tabname,
{'a': '581750', {'a': b'581750',
'b': "good", 'b': b'good',
'd': "bla", 'd': b'bla',
'c': "black", 'c': b'black',
'e': 'fuzzy was here'}) 'e': b'fuzzy was here'})
self.tdb.Insert(tabname, self.tdb.Insert(tabname,
{'a': '800000', {'a': b'800000',
'b': "good", 'b': b'good',
'd': "bla", 'd': b'bla',
'c': "black", 'c': b'black',
'e': 'Fuzzy wuzzy is a bear'}) 'e': b'Fuzzy wuzzy is a bear'})
if verbose: if verbose:
self.tdb._db_print() self.tdb._db_print()
...@@ -202,19 +202,19 @@ class TableDBTestCase(unittest.TestCase): ...@@ -202,19 +202,19 @@ class TableDBTestCase(unittest.TestCase):
try: try:
self.tdb.Insert(tabname, self.tdb.Insert(tabname,
{'a': "", {'a': b"",
'e': pickle.dumps([{4:5, 6:7}, 'foo'], 1), 'e': pickle.dumps([{4:5, 6:7}, 'foo'], 1),
'f': "Zero"}) 'f': b"Zero"})
self.fail("exception not raised") self.fail("exception not raised")
except dbtables.TableDBError: except dbtables.TableDBError:
pass pass
self.tdb.Insert(tabname, {'a': "A", 'b': "B", 'c': "C", 'd': "D", self.tdb.Insert(tabname, {'a': b"A", 'b': b"B", 'c': b"C",
'e': "E"}) 'd': b"D", 'e': b"E"})
self.tdb.Insert(tabname, {'a': "-A", 'b': "-B", 'c': "-C", 'd': "-D", self.tdb.Insert(tabname, {'a': b"-A", 'b': b"-B", 'c': b"-C",
'e': "-E"}) 'd': b"-D", 'e': b"-E"})
self.tdb.Insert(tabname, {'a': "A-", 'b': "B-", 'c': "C-", 'd': "D-", self.tdb.Insert(tabname, {'a': b"A-", 'b': b"B-", 'c': b"C-",
'e': "E-"}) 'd': b"D-", 'e': b"E-"})
if verbose: if verbose:
self.tdb._db_print() self.tdb._db_print()
...@@ -239,9 +239,9 @@ class TableDBTestCase(unittest.TestCase): ...@@ -239,9 +239,9 @@ class TableDBTestCase(unittest.TestCase):
tabname, ['name', 'taste', 'filling', 'alcohol content', 'price']) tabname, ['name', 'taste', 'filling', 'alcohol content', 'price'])
try: try:
self.tdb.Insert(tabname, self.tdb.Insert(tabname,
{'taste': 'crap', {'taste': b'crap',
'filling': 'no', 'filling': b'no',
'is it Guinness?': 'no'}) 'is it Guinness?': b'no'})
self.fail("Insert should've failed due to bad column name") self.fail("Insert should've failed due to bad column name")
except: except:
pass pass
...@@ -249,11 +249,11 @@ class TableDBTestCase(unittest.TestCase): ...@@ -249,11 +249,11 @@ class TableDBTestCase(unittest.TestCase):
['name', 'taste', 'is it Guinness?']) ['name', 'taste', 'is it Guinness?'])
# these should both succeed as the table should contain the union of both sets of columns. # these should both succeed as the table should contain the union of both sets of columns.
self.tdb.Insert(tabname, {'taste': 'crap', 'filling': 'no', self.tdb.Insert(tabname, {'taste': b'crap', 'filling': b'no',
'is it Guinness?': 'no'}) 'is it Guinness?': b'no'})
self.tdb.Insert(tabname, {'taste': 'great', 'filling': 'yes', self.tdb.Insert(tabname, {'taste': b'great', 'filling': b'yes',
'is it Guinness?': 'yes', 'is it Guinness?': b'yes',
'name': 'Guinness'}) 'name': b'Guinness'})
def test_CondObjs(self): def test_CondObjs(self):
...@@ -261,17 +261,17 @@ class TableDBTestCase(unittest.TestCase): ...@@ -261,17 +261,17 @@ class TableDBTestCase(unittest.TestCase):
self.tdb.CreateTable(tabname, ['a', 'b', 'c', 'd', 'e', 'p']) self.tdb.CreateTable(tabname, ['a', 'b', 'c', 'd', 'e', 'p'])
self.tdb.Insert(tabname, {'a': "the letter A", self.tdb.Insert(tabname, {'a': b"the letter A",
'b': "the letter B", 'b': b"the letter B",
'c': "is for cookie"}) 'c': b"is for cookie"})
self.tdb.Insert(tabname, {'a': "is for aardvark", self.tdb.Insert(tabname, {'a': b"is for aardvark",
'e': "the letter E", 'e': b"the letter E",
'c': "is for cookie", 'c': b"is for cookie",
'd': "is for dog"}) 'd': b"is for dog"})
self.tdb.Insert(tabname, {'a': "the letter A", self.tdb.Insert(tabname, {'a': b"the letter A",
'e': "the letter E", 'e': b"the letter E",
'c': "is for cookie", 'c': b"is for cookie",
'p': "is for Python"}) 'p': b"is for Python"})
values = self.tdb.Select( values = self.tdb.Select(
tabname, ['p', 'e'], tabname, ['p', 'e'],
...@@ -309,8 +309,8 @@ class TableDBTestCase(unittest.TestCase): ...@@ -309,8 +309,8 @@ class TableDBTestCase(unittest.TestCase):
# fail if it encountered any rows that did not have values in # fail if it encountered any rows that did not have values in
# every column. # every column.
# Hunted and Squashed by <Donwulff> (Jukka Santala - donwulff@nic.fi) # Hunted and Squashed by <Donwulff> (Jukka Santala - donwulff@nic.fi)
self.tdb.Insert(tabname, {'x': 'X1', 'y':'Y1'}) self.tdb.Insert(tabname, {'x': b'X1', 'y':b'Y1'})
self.tdb.Insert(tabname, {'x': 'X2', 'y':'Y2', 'z': 'Z2'}) self.tdb.Insert(tabname, {'x': b'X2', 'y':b'Y2', 'z': b'Z2'})
self.tdb.Delete(tabname, conditions={'x': dbtables.PrefixCond('X')}) self.tdb.Delete(tabname, conditions={'x': dbtables.PrefixCond('X')})
values = self.tdb.Select(tabname, ['y'], values = self.tdb.Select(tabname, ['y'],
...@@ -321,18 +321,18 @@ class TableDBTestCase(unittest.TestCase): ...@@ -321,18 +321,18 @@ class TableDBTestCase(unittest.TestCase):
tabname = "test_Modify" tabname = "test_Modify"
self.tdb.CreateTable(tabname, ['Name', 'Type', 'Access']) self.tdb.CreateTable(tabname, ['Name', 'Type', 'Access'])
self.tdb.Insert(tabname, {'Name': 'Index to MP3 files.doc', self.tdb.Insert(tabname, {'Name': b'Index to MP3 files.doc',
'Type': 'Word', 'Access': '8'}) 'Type': b'Word', 'Access': b'8'})
self.tdb.Insert(tabname, {'Name': 'Nifty.MP3', 'Access': '1'}) self.tdb.Insert(tabname, {'Name': b'Nifty.MP3', 'Access': b'1'})
self.tdb.Insert(tabname, {'Type': 'Unknown', 'Access': '0'}) self.tdb.Insert(tabname, {'Type': b'Unknown', 'Access': b'0'})
def set_type(type): def set_type(type):
if type == None: if type == None:
return 'MP3' return b'MP3'
return type return type
def increment_access(count): def increment_access(count):
return str(int(count)+1) return bytes(str(int(count)+1))
def remove_value(value): def remove_value(value):
return None return None
...@@ -350,7 +350,7 @@ class TableDBTestCase(unittest.TestCase): ...@@ -350,7 +350,7 @@ class TableDBTestCase(unittest.TestCase):
try: try:
self.tdb.Modify(tabname, self.tdb.Modify(tabname,
conditions={'Name': dbtables.LikeCond('%')}, conditions={'Name': dbtables.LikeCond('%')},
mappings={'Access': 'What is your quest?'}) mappings={'Access': b'What is your quest?'})
except TypeError: except TypeError:
# success, the string value in mappings isn't callable # success, the string value in mappings isn't callable
pass pass
......
...@@ -55,7 +55,7 @@ class LockingTestCase(unittest.TestCase): ...@@ -55,7 +55,7 @@ class LockingTestCase(unittest.TestCase):
anID = self.env.lock_id() anID = self.env.lock_id()
if verbose: if verbose:
print("locker ID: %s" % anID) print("locker ID: %s" % anID)
lock = self.env.lock_get(anID, "some locked thing", db.DB_LOCK_WRITE) lock = self.env.lock_get(anID, b"some locked thing", db.DB_LOCK_WRITE)
if verbose: if verbose:
print("Aquired lock: %s" % lock) print("Aquired lock: %s" % lock)
time.sleep(1) time.sleep(1)
...@@ -115,7 +115,7 @@ class LockingTestCase(unittest.TestCase): ...@@ -115,7 +115,7 @@ class LockingTestCase(unittest.TestCase):
if verbose: if verbose:
print("%s: locker ID: %s" % (name, anID)) print("%s: locker ID: %s" % (name, anID))
lock = self.env.lock_get(anID, "some locked thing", lockType) lock = self.env.lock_get(anID, b"some locked thing", lockType)
if verbose: if verbose:
print("%s: Aquired %s lock: %s" % (name, lt, lock)) print("%s: Aquired %s lock: %s" % (name, lt, lock))
......
...@@ -47,14 +47,14 @@ class SimpleQueueTestCase(unittest.TestCase): ...@@ -47,14 +47,14 @@ class SimpleQueueTestCase(unittest.TestCase):
pprint(d.stat()) pprint(d.stat())
for x in letters: for x in letters:
d.append(x * 40) d.append(bytes(x) * 40)
assert len(d) == 52 assert len(d) == 52
d.put(100, "some more data") d.put(100, b"some more data")
d.put(101, "and some more ") d.put(101, b"and some more ")
d.put(75, "out of order") d.put(75, b"out of order")
d.put(1, "replacement data") d.put(1, b"replacement data")
assert len(d) == 55 assert len(d) == 55
...@@ -71,7 +71,7 @@ class SimpleQueueTestCase(unittest.TestCase): ...@@ -71,7 +71,7 @@ class SimpleQueueTestCase(unittest.TestCase):
print("after open" + '-' * 30) print("after open" + '-' * 30)
pprint(d.stat()) pprint(d.stat())
d.append("one more") d.append(b"one more")
c = d.cursor() c = d.cursor()
if verbose: if verbose:
...@@ -119,14 +119,14 @@ class SimpleQueueTestCase(unittest.TestCase): ...@@ -119,14 +119,14 @@ class SimpleQueueTestCase(unittest.TestCase):
pprint(d.stat()) pprint(d.stat())
for x in letters: for x in letters:
d.append(x * 40) d.append(bytes(x) * 40)
assert len(d) == 52 assert len(d) == 52
d.put(100, "some more data") d.put(100, b"some more data")
d.put(101, "and some more ") d.put(101, b"and some more ")
d.put(75, "out of order") d.put(75, b"out of order")
d.put(1, "replacement data") d.put(1, b"replacement data")
assert len(d) == 55 assert len(d) == 55
...@@ -144,7 +144,7 @@ class SimpleQueueTestCase(unittest.TestCase): ...@@ -144,7 +144,7 @@ class SimpleQueueTestCase(unittest.TestCase):
print("after open" + '-' * 30) print("after open" + '-' * 30)
pprint(d.stat()) pprint(d.stat())
d.append("one more") d.append(b"one more")
if verbose: if verbose:
print("after append" + '-' * 30) print("after append" + '-' * 30)
......
...@@ -41,7 +41,7 @@ class SimpleRecnoTestCase(unittest.TestCase): ...@@ -41,7 +41,7 @@ class SimpleRecnoTestCase(unittest.TestCase):
d.open(self.filename, db.DB_RECNO, db.DB_CREATE) d.open(self.filename, db.DB_RECNO, db.DB_CREATE)
for x in letters: for x in letters:
recno = d.append(x * 60) recno = d.append(bytes(x) * 60)
assert type(recno) == type(0) assert type(recno) == type(0)
assert recno >= 1 assert recno >= 1
if verbose: if verbose:
...@@ -219,7 +219,7 @@ class SimpleRecnoTestCase(unittest.TestCase): ...@@ -219,7 +219,7 @@ class SimpleRecnoTestCase(unittest.TestCase):
data = "The quick brown fox jumped over the lazy dog".split() data = "The quick brown fox jumped over the lazy dog".split()
for datum in data: for datum in data:
d.append(datum) d.append(bytes(datum))
d.sync() d.sync()
d.close() d.close()
...@@ -238,8 +238,8 @@ class SimpleRecnoTestCase(unittest.TestCase): ...@@ -238,8 +238,8 @@ class SimpleRecnoTestCase(unittest.TestCase):
d.set_re_source(source) d.set_re_source(source)
d.open(self.filename, db.DB_RECNO) d.open(self.filename, db.DB_RECNO)
d[3] = 'reddish-brown' d[3] = b'reddish-brown'
d[8] = 'comatose' d[8] = b'comatose'
d.sync() d.sync()
d.close() d.close()
...@@ -261,12 +261,12 @@ class SimpleRecnoTestCase(unittest.TestCase): ...@@ -261,12 +261,12 @@ class SimpleRecnoTestCase(unittest.TestCase):
d.open(self.filename, db.DB_RECNO, db.DB_CREATE) d.open(self.filename, db.DB_RECNO, db.DB_CREATE)
for x in letters: for x in letters:
d.append(x * 35) # These will be padded d.append(bytes(x) * 35) # These will be padded
d.append('.' * 40) # this one will be exact d.append(b'.' * 40) # this one will be exact
try: # this one will fail try: # this one will fail
d.append('bad' * 20) d.append(b'bad' * 20)
except db.DBInvalidArgError as val: except db.DBInvalidArgError as val:
assert val.args[0] == db.EINVAL assert val.args[0] == db.EINVAL
if verbose: print(val) if verbose: print(val)
......
...@@ -380,11 +380,13 @@ static int make_dbt(PyObject* obj, DBT* dbt) ...@@ -380,11 +380,13 @@ static int make_dbt(PyObject* obj, DBT* dbt)
if (obj == Py_None) { if (obj == Py_None) {
/* no need to do anything, the structure has already been zeroed */ /* no need to do anything, the structure has already been zeroed */
} }
else if (!PyArg_Parse(obj, "s#", &dbt->data, &dbt->size)) { else if (!PyBytes_Check(obj)) {
PyErr_SetString(PyExc_TypeError, PyErr_SetString(PyExc_TypeError,
"Data values must be of type string or None."); "Data values must be of type bytes or None.");
return 0; return 0;
} }
dbt->data = PyBytes_AS_STRING(obj);
dbt->size = PyBytes_GET_SIZE(obj);
return 1; return 1;
} }
...@@ -4737,7 +4739,7 @@ DBTxn_prepare(DBTxnObject* self, PyObject* args) ...@@ -4737,7 +4739,7 @@ DBTxn_prepare(DBTxnObject* self, PyObject* args)
char* gid=NULL; char* gid=NULL;
int gid_size=0; int gid_size=0;
if (!PyArg_ParseTuple(args, "s#:prepare", &gid, &gid_size)) if (!PyArg_ParseTuple(args, "y#:prepare", &gid, &gid_size))
return NULL; return NULL;
if (gid_size != DB_XIDDATASIZE) { if (gid_size != DB_XIDDATASIZE) {
...@@ -5915,6 +5917,7 @@ PyMODINIT_FUNC init_bsddb(void) ...@@ -5915,6 +5917,7 @@ PyMODINIT_FUNC init_bsddb(void)
#if (DBVER >= 33) #if (DBVER >= 33)
ADD_INT(d, DB_DONOTINDEX); ADD_INT(d, DB_DONOTINDEX);
ADD_INT(d, DB_XIDDATASIZE);
#endif #endif
#if (DBVER >= 41) #if (DBVER >= 41)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment