]> granicus.if.org Git - python/commitdiff
Treat empty dat/dir pairs as dumbdbm. Fixes #744687.
authorMartin v. Löwis <martin@v.loewis.de>
Sat, 14 Jun 2003 08:16:34 +0000 (08:16 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Sat, 14 Jun 2003 08:16:34 +0000 (08:16 +0000)
Lib/test/test_whichdb.py
Lib/whichdb.py

index 000c9993657a3768f6d2d242c8696b4ea73197f5..f2652c1679adb2d4d0c3ad4c9100fb6ba3ec23a8 100644 (file)
@@ -45,7 +45,12 @@ for name in anydbm._names:
     def test_whichdb_name(self, name=name, mod=mod):
         # Check whether whichdb correctly guesses module name
         # for databases opened with module mod.
+        # Try with empty files first
         f = mod.open(_fname, 'c')
+        f.close()
+        self.assertEqual(name, whichdb.whichdb(_fname))
+        # Now add a key
+        f = mod.open(_fname, 'w')
         f["1"] = "1"
         f.close()
         self.assertEqual(name, whichdb.whichdb(_fname))
index fed58e943b07d3c3820e434b6f762fecbce5354b..92996367ee429b50a681f59106fdbbe8ebd41216 100644 (file)
@@ -50,15 +50,19 @@ def whichdb(filename):
 
     # Check for dumbdbm next -- this has a .dir and and a .dat file
     try:
-        f = open(filename + os.extsep + "dat", "rb")
-        f.close()
+        # First check for presence of files
+        sizes = os.stat(filename + os.extsep + "dat").st_size, \
+                os.stat(filename + os.extsep + "dir").st_size
+        # dumbdbm files with no keys are empty
+        if sizes == (0, 0):
+            return "dumbdbm"
         f = open(filename + os.extsep + "dir", "rb")
         try:
             if f.read(1) in ["'", '"']:
                 return "dumbdbm"
         finally:
             f.close()
-    except IOError:
+    except (OSError, IOError):
         pass
 
     # See if the file exists, return None if not