[Python-3000-checkins] r56790 - in python/branches/py3k-struni/Lib: dumbdbm.py test/test_dumbdbm.py test/test_whichdb.py

martin.v.loewis python-3000-checkins at python.org
Tue Aug 7 07:37:39 CEST 2007


Author: martin.v.loewis
Date: Tue Aug  7 07:37:39 2007
New Revision: 56790

Modified:
   python/branches/py3k-struni/Lib/dumbdbm.py
   python/branches/py3k-struni/Lib/test/test_dumbdbm.py
   python/branches/py3k-struni/Lib/test/test_whichdb.py
Log:
Change dumbdbm to use bytes keys.


Modified: python/branches/py3k-struni/Lib/dumbdbm.py
==============================================================================
--- python/branches/py3k-struni/Lib/dumbdbm.py	(original)
+++ python/branches/py3k-struni/Lib/dumbdbm.py	Tue Aug  7 07:37:39 2007
@@ -115,6 +115,7 @@
     sync = _commit
 
     def __getitem__(self, key):
+        key = key.decode("latin-1")
         pos, siz = self._index[key]     # may raise KeyError
         f = _io.open(self._datfile, 'rb')
         f.seek(pos)
@@ -159,8 +160,9 @@
         f.close()
 
     def __setitem__(self, key, val):
-        if not isinstance(key, basestring):
-            raise TypeError("keys must be strings")
+        if not isinstance(key, bytes):
+            raise TypeError("keys must be bytes")
+        key = key.decode("latin-1") # hashable bytes
         if not isinstance(val, (str8, bytes)):
             raise TypeError("values must be byte strings")
         if key not in self._index:
@@ -188,6 +190,7 @@
             # (so that _commit() never gets called).
 
     def __delitem__(self, key):
+        key = key.decode("latin-1")
         # The blocks used by the associated value are lost.
         del self._index[key]
         # XXX It's unclear why we do a _commit() here (the code always

Modified: python/branches/py3k-struni/Lib/test/test_dumbdbm.py
==============================================================================
--- python/branches/py3k-struni/Lib/test/test_dumbdbm.py	(original)
+++ python/branches/py3k-struni/Lib/test/test_dumbdbm.py	Tue Aug  7 07:37:39 2007
@@ -35,7 +35,7 @@
         f = dumbdbm.open(_fname, 'c')
         self.assertEqual(list(f.keys()), [])
         for key in self._dict:
-            f[key] = self._dict[key]
+            f[key.encode("ascii")] = self._dict[key]
         self.read_helper(f)
         f.close()
 
@@ -65,15 +65,15 @@
 
     def test_close_twice(self):
         f = dumbdbm.open(_fname)
-        f['a'] = b'b'
-        self.assertEqual(f['a'], b'b')
+        f[b'a'] = b'b'
+        self.assertEqual(f[b'a'], b'b')
         f.close()
         f.close()
 
     def test_dumbdbm_modification(self):
         self.init_db()
         f = dumbdbm.open(_fname, 'w')
-        self._dict['g'] = f['g'] = b"indented"
+        self._dict['g'] = f[b'g'] = b"indented"
         self.read_helper(f)
         f.close()
 
@@ -92,8 +92,8 @@
     def test_write_write_read(self):
         # test for bug #482460
         f = dumbdbm.open(_fname)
-        f['1'] = b'hello'
-        f['1'] = b'hello2'
+        f[b'1'] = b'hello'
+        f[b'1'] = b'hello2'
         f.close()
         f = dumbdbm.open(_fname)
         self.assertEqual(f['1'], b'hello2')
@@ -103,8 +103,8 @@
         # test for bug #1172763: dumbdbm would die if the line endings
         # weren't what was expected.
         f = dumbdbm.open(_fname)
-        f['1'] = b'hello'
-        f['2'] = b'hello2'
+        f[b'1'] = b'hello'
+        f[b'2'] = b'hello2'
         f.close()
 
         # Mangle the file by adding \r before each newline
@@ -113,23 +113,23 @@
         io.open(_fname + '.dir', 'wb').write(data)
 
         f = dumbdbm.open(_fname)
-        self.assertEqual(f['1'], b'hello')
-        self.assertEqual(f['2'], b'hello2')
+        self.assertEqual(f[b'1'], b'hello')
+        self.assertEqual(f[b'2'], b'hello2')
 
 
     def read_helper(self, f):
         keys = self.keys_helper(f)
         for key in self._dict:
-            self.assertEqual(self._dict[key], f[key])
+            self.assertEqual(self._dict[key], f[key.encode("ascii")])
 
     def init_db(self):
         f = dumbdbm.open(_fname, 'w')
         for k in self._dict:
-            f[k] = self._dict[k]
+            f[k.encode("ascii")] = self._dict[k]
         f.close()
 
     def keys_helper(self, f):
-        keys = sorted(f.keys())
+        keys = sorted(k.decode("ascii") for k in f.keys())
         dkeys = sorted(self._dict.keys())
         self.assertEqual(keys, dkeys)
         return keys
@@ -146,11 +146,11 @@
                 if random.random() < 0.2:
                     if k in d:
                         del d[k]
-                        del f[k]
+                        del f[k.encode("ascii")]
                 else:
                     v = random.choice((b'a', b'b', b'c')) * random.randrange(10000)
                     d[k] = v
-                    f[k] = v
+                    f[k.encode("ascii")] = v
                     self.assertEqual(f[k], v)
             f.close()
 

Modified: python/branches/py3k-struni/Lib/test/test_whichdb.py
==============================================================================
--- python/branches/py3k-struni/Lib/test/test_whichdb.py	(original)
+++ python/branches/py3k-struni/Lib/test/test_whichdb.py	Tue Aug  7 07:37:39 2007
@@ -51,7 +51,7 @@
         self.assertEqual(name, whichdb.whichdb(_fname))
         # Now add a key
         f = mod.open(_fname, 'w')
-        f["1"] = b"1"
+        f[b"1"] = b"1"
         f.close()
         self.assertEqual(name, whichdb.whichdb(_fname))
     setattr(WhichDBTestCase,"test_whichdb_%s" % name, test_whichdb_name)


More information about the Python-3000-checkins mailing list