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

Skip to content

Commit 361ed15

Browse files
committed
Require strict bytes objects for all bsddb.db input values.
1 parent e5a8dc6 commit 361ed15

5 files changed

Lines changed: 80 additions & 77 deletions

File tree

Lib/bsddb/test/test_dbtables.py

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ def test02(self):
8888
col1 = 'but can it fly?'
8989
col2 = 'Species'
9090
testinfo = [
91-
{col0: pickle.dumps(8, 1), col1: 'no', col2: 'Penguin'},
92-
{col0: pickle.dumps(-1, 1), col1: 'no', col2: 'Turkey'},
93-
{col0: pickle.dumps(9, 1), col1: 'yes', col2: 'SR-71A Blackbird'}
91+
{col0: pickle.dumps(8, 1), col1: b'no', col2: b'Penguin'},
92+
{col0: pickle.dumps(-1, 1), col1: b'no', col2: b'Turkey'},
93+
{col0: pickle.dumps(9, 1), col1: b'yes', col2: b'SR-71A Blackbird'}
9494
]
9595

9696
try:
@@ -150,22 +150,22 @@ def test03(self):
150150
pass
151151

152152
self.tdb.Insert(tabname,
153-
{'a': '42',
154-
'b': "bad",
155-
'c': "meep",
156-
'e': 'Fuzzy wuzzy was a bear'})
153+
{'a': b'42',
154+
'b': b'bad',
155+
'c': b'meep',
156+
'e': b'Fuzzy wuzzy was a bear'})
157157
self.tdb.Insert(tabname,
158-
{'a': '581750',
159-
'b': "good",
160-
'd': "bla",
161-
'c': "black",
162-
'e': 'fuzzy was here'})
158+
{'a': b'581750',
159+
'b': b'good',
160+
'd': b'bla',
161+
'c': b'black',
162+
'e': b'fuzzy was here'})
163163
self.tdb.Insert(tabname,
164-
{'a': '800000',
165-
'b': "good",
166-
'd': "bla",
167-
'c': "black",
168-
'e': 'Fuzzy wuzzy is a bear'})
164+
{'a': b'800000',
165+
'b': b'good',
166+
'd': b'bla',
167+
'c': b'black',
168+
'e': b'Fuzzy wuzzy is a bear'})
169169

170170
if verbose:
171171
self.tdb._db_print()
@@ -202,19 +202,19 @@ def test04_MultiCondSelect(self):
202202

203203
try:
204204
self.tdb.Insert(tabname,
205-
{'a': "",
205+
{'a': b"",
206206
'e': pickle.dumps([{4:5, 6:7}, 'foo'], 1),
207-
'f': "Zero"})
207+
'f': b"Zero"})
208208
self.fail("exception not raised")
209209
except dbtables.TableDBError:
210210
pass
211211

212-
self.tdb.Insert(tabname, {'a': "A", 'b': "B", 'c': "C", 'd': "D",
213-
'e': "E"})
214-
self.tdb.Insert(tabname, {'a': "-A", 'b': "-B", 'c': "-C", 'd': "-D",
215-
'e': "-E"})
216-
self.tdb.Insert(tabname, {'a': "A-", 'b': "B-", 'c': "C-", 'd': "D-",
217-
'e': "E-"})
212+
self.tdb.Insert(tabname, {'a': b"A", 'b': b"B", 'c': b"C",
213+
'd': b"D", 'e': b"E"})
214+
self.tdb.Insert(tabname, {'a': b"-A", 'b': b"-B", 'c': b"-C",
215+
'd': b"-D", 'e': b"-E"})
216+
self.tdb.Insert(tabname, {'a': b"A-", 'b': b"B-", 'c': b"C-",
217+
'd': b"D-", 'e': b"E-"})
218218

219219
if verbose:
220220
self.tdb._db_print()
@@ -239,39 +239,39 @@ def test_CreateOrExtend(self):
239239
tabname, ['name', 'taste', 'filling', 'alcohol content', 'price'])
240240
try:
241241
self.tdb.Insert(tabname,
242-
{'taste': 'crap',
243-
'filling': 'no',
244-
'is it Guinness?': 'no'})
242+
{'taste': b'crap',
243+
'filling': b'no',
244+
'is it Guinness?': b'no'})
245245
self.fail("Insert should've failed due to bad column name")
246246
except:
247247
pass
248248
self.tdb.CreateOrExtendTable(tabname,
249249
['name', 'taste', 'is it Guinness?'])
250250

251251
# these should both succeed as the table should contain the union of both sets of columns.
252-
self.tdb.Insert(tabname, {'taste': 'crap', 'filling': 'no',
253-
'is it Guinness?': 'no'})
254-
self.tdb.Insert(tabname, {'taste': 'great', 'filling': 'yes',
255-
'is it Guinness?': 'yes',
256-
'name': 'Guinness'})
252+
self.tdb.Insert(tabname, {'taste': b'crap', 'filling': b'no',
253+
'is it Guinness?': b'no'})
254+
self.tdb.Insert(tabname, {'taste': b'great', 'filling': b'yes',
255+
'is it Guinness?': b'yes',
256+
'name': b'Guinness'})
257257

258258

259259
def test_CondObjs(self):
260260
tabname = "test_CondObjs"
261261

262262
self.tdb.CreateTable(tabname, ['a', 'b', 'c', 'd', 'e', 'p'])
263263

264-
self.tdb.Insert(tabname, {'a': "the letter A",
265-
'b': "the letter B",
266-
'c': "is for cookie"})
267-
self.tdb.Insert(tabname, {'a': "is for aardvark",
268-
'e': "the letter E",
269-
'c': "is for cookie",
270-
'd': "is for dog"})
271-
self.tdb.Insert(tabname, {'a': "the letter A",
272-
'e': "the letter E",
273-
'c': "is for cookie",
274-
'p': "is for Python"})
264+
self.tdb.Insert(tabname, {'a': b"the letter A",
265+
'b': b"the letter B",
266+
'c': b"is for cookie"})
267+
self.tdb.Insert(tabname, {'a': b"is for aardvark",
268+
'e': b"the letter E",
269+
'c': b"is for cookie",
270+
'd': b"is for dog"})
271+
self.tdb.Insert(tabname, {'a': b"the letter A",
272+
'e': b"the letter E",
273+
'c': b"is for cookie",
274+
'p': b"is for Python"})
275275

276276
values = self.tdb.Select(
277277
tabname, ['p', 'e'],
@@ -309,8 +309,8 @@ def test_Delete(self):
309309
# fail if it encountered any rows that did not have values in
310310
# every column.
311311
# Hunted and Squashed by <Donwulff> (Jukka Santala - [email protected])
312-
self.tdb.Insert(tabname, {'x': 'X1', 'y':'Y1'})
313-
self.tdb.Insert(tabname, {'x': 'X2', 'y':'Y2', 'z': 'Z2'})
312+
self.tdb.Insert(tabname, {'x': b'X1', 'y':b'Y1'})
313+
self.tdb.Insert(tabname, {'x': b'X2', 'y':b'Y2', 'z': b'Z2'})
314314

315315
self.tdb.Delete(tabname, conditions={'x': dbtables.PrefixCond('X')})
316316
values = self.tdb.Select(tabname, ['y'],
@@ -321,18 +321,18 @@ def test_Modify(self):
321321
tabname = "test_Modify"
322322
self.tdb.CreateTable(tabname, ['Name', 'Type', 'Access'])
323323

324-
self.tdb.Insert(tabname, {'Name': 'Index to MP3 files.doc',
325-
'Type': 'Word', 'Access': '8'})
326-
self.tdb.Insert(tabname, {'Name': 'Nifty.MP3', 'Access': '1'})
327-
self.tdb.Insert(tabname, {'Type': 'Unknown', 'Access': '0'})
324+
self.tdb.Insert(tabname, {'Name': b'Index to MP3 files.doc',
325+
'Type': b'Word', 'Access': b'8'})
326+
self.tdb.Insert(tabname, {'Name': b'Nifty.MP3', 'Access': b'1'})
327+
self.tdb.Insert(tabname, {'Type': b'Unknown', 'Access': b'0'})
328328

329329
def set_type(type):
330330
if type == None:
331-
return 'MP3'
331+
return b'MP3'
332332
return type
333333

334334
def increment_access(count):
335-
return str(int(count)+1)
335+
return bytes(str(int(count)+1))
336336

337337
def remove_value(value):
338338
return None
@@ -350,7 +350,7 @@ def remove_value(value):
350350
try:
351351
self.tdb.Modify(tabname,
352352
conditions={'Name': dbtables.LikeCond('%')},
353-
mappings={'Access': 'What is your quest?'})
353+
mappings={'Access': b'What is your quest?'})
354354
except TypeError:
355355
# success, the string value in mappings isn't callable
356356
pass

Lib/bsddb/test/test_lock.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def test01_simple(self):
5555
anID = self.env.lock_id()
5656
if verbose:
5757
print("locker ID: %s" % anID)
58-
lock = self.env.lock_get(anID, "some locked thing", db.DB_LOCK_WRITE)
58+
lock = self.env.lock_get(anID, b"some locked thing", db.DB_LOCK_WRITE)
5959
if verbose:
6060
print("Aquired lock: %s" % lock)
6161
time.sleep(1)
@@ -115,7 +115,7 @@ def theThread(self, sleepTime, lockType):
115115
if verbose:
116116
print("%s: locker ID: %s" % (name, anID))
117117

118-
lock = self.env.lock_get(anID, "some locked thing", lockType)
118+
lock = self.env.lock_get(anID, b"some locked thing", lockType)
119119
if verbose:
120120
print("%s: Aquired %s lock: %s" % (name, lt, lock))
121121

Lib/bsddb/test/test_queue.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ def test01_basic(self):
4747
pprint(d.stat())
4848

4949
for x in letters:
50-
d.append(x * 40)
50+
d.append(bytes(x) * 40)
5151

5252
assert len(d) == 52
5353

54-
d.put(100, "some more data")
55-
d.put(101, "and some more ")
56-
d.put(75, "out of order")
57-
d.put(1, "replacement data")
54+
d.put(100, b"some more data")
55+
d.put(101, b"and some more ")
56+
d.put(75, b"out of order")
57+
d.put(1, b"replacement data")
5858

5959
assert len(d) == 55
6060

@@ -71,7 +71,7 @@ def test01_basic(self):
7171
print("after open" + '-' * 30)
7272
pprint(d.stat())
7373

74-
d.append("one more")
74+
d.append(b"one more")
7575
c = d.cursor()
7676

7777
if verbose:
@@ -119,14 +119,14 @@ def test02_basicPost32(self):
119119
pprint(d.stat())
120120

121121
for x in letters:
122-
d.append(x * 40)
122+
d.append(bytes(x) * 40)
123123

124124
assert len(d) == 52
125125

126-
d.put(100, "some more data")
127-
d.put(101, "and some more ")
128-
d.put(75, "out of order")
129-
d.put(1, "replacement data")
126+
d.put(100, b"some more data")
127+
d.put(101, b"and some more ")
128+
d.put(75, b"out of order")
129+
d.put(1, b"replacement data")
130130

131131
assert len(d) == 55
132132

@@ -144,7 +144,7 @@ def test02_basicPost32(self):
144144
print("after open" + '-' * 30)
145145
pprint(d.stat())
146146

147-
d.append("one more")
147+
d.append(b"one more")
148148

149149
if verbose:
150150
print("after append" + '-' * 30)

Lib/bsddb/test/test_recno.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def test01_basic(self):
4141
d.open(self.filename, db.DB_RECNO, db.DB_CREATE)
4242

4343
for x in letters:
44-
recno = d.append(x * 60)
44+
recno = d.append(bytes(x) * 60)
4545
assert type(recno) == type(0)
4646
assert recno >= 1
4747
if verbose:
@@ -219,7 +219,7 @@ def test02_WithSource(self):
219219

220220
data = "The quick brown fox jumped over the lazy dog".split()
221221
for datum in data:
222-
d.append(datum)
222+
d.append(bytes(datum))
223223
d.sync()
224224
d.close()
225225

@@ -238,8 +238,8 @@ def test02_WithSource(self):
238238
d.set_re_source(source)
239239
d.open(self.filename, db.DB_RECNO)
240240

241-
d[3] = 'reddish-brown'
242-
d[8] = 'comatose'
241+
d[3] = b'reddish-brown'
242+
d[8] = b'comatose'
243243

244244
d.sync()
245245
d.close()
@@ -261,12 +261,12 @@ def test03_FixedLength(self):
261261
d.open(self.filename, db.DB_RECNO, db.DB_CREATE)
262262

263263
for x in letters:
264-
d.append(x * 35) # These will be padded
264+
d.append(bytes(x) * 35) # These will be padded
265265

266-
d.append('.' * 40) # this one will be exact
266+
d.append(b'.' * 40) # this one will be exact
267267

268268
try: # this one will fail
269-
d.append('bad' * 20)
269+
d.append(b'bad' * 20)
270270
except db.DBInvalidArgError as val:
271271
assert val.args[0] == db.EINVAL
272272
if verbose: print(val)

Modules/_bsddb.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,11 +380,13 @@ static int make_dbt(PyObject* obj, DBT* dbt)
380380
if (obj == Py_None) {
381381
/* no need to do anything, the structure has already been zeroed */
382382
}
383-
else if (!PyArg_Parse(obj, "s#", &dbt->data, &dbt->size)) {
383+
else if (!PyBytes_Check(obj)) {
384384
PyErr_SetString(PyExc_TypeError,
385-
"Data values must be of type string or None.");
385+
"Data values must be of type bytes or None.");
386386
return 0;
387387
}
388+
dbt->data = PyBytes_AS_STRING(obj);
389+
dbt->size = PyBytes_GET_SIZE(obj);
388390
return 1;
389391
}
390392

@@ -4737,7 +4739,7 @@ DBTxn_prepare(DBTxnObject* self, PyObject* args)
47374739
char* gid=NULL;
47384740
int gid_size=0;
47394741

4740-
if (!PyArg_ParseTuple(args, "s#:prepare", &gid, &gid_size))
4742+
if (!PyArg_ParseTuple(args, "y#:prepare", &gid, &gid_size))
47414743
return NULL;
47424744

47434745
if (gid_size != DB_XIDDATASIZE) {
@@ -5915,6 +5917,7 @@ PyMODINIT_FUNC init_bsddb(void)
59155917

59165918
#if (DBVER >= 33)
59175919
ADD_INT(d, DB_DONOTINDEX);
5920+
ADD_INT(d, DB_XIDDATASIZE);
59185921
#endif
59195922

59205923
#if (DBVER >= 41)

0 commit comments

Comments
 (0)