Require strict bytes objects for all bsddb.db input values.
authorGregory P. Smith <greg@mad-scientist.com>
Thu, 23 Aug 2007 07:32:27 +0000 (07:32 +0000)
committerGregory P. Smith <greg@mad-scientist.com>
Thu, 23 Aug 2007 07:32:27 +0000 (07:32 +0000)
Lib/bsddb/test/test_dbtables.py
Lib/bsddb/test/test_lock.py
Lib/bsddb/test/test_queue.py
Lib/bsddb/test/test_recno.py
Modules/_bsddb.c

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