mirror of
https://github.com/python/cpython.git
synced 2025-11-02 19:12:55 +00:00
Treat empty dat/dir pairs as dumbdbm. Fixes #744687.
This commit is contained in:
parent
8316feb155
commit
17fb50790d
2 changed files with 12 additions and 3 deletions
|
|
@ -45,7 +45,12 @@ for name in anydbm._names:
|
||||||
def test_whichdb_name(self, name=name, mod=mod):
|
def test_whichdb_name(self, name=name, mod=mod):
|
||||||
# Check whether whichdb correctly guesses module name
|
# Check whether whichdb correctly guesses module name
|
||||||
# for databases opened with module mod.
|
# for databases opened with module mod.
|
||||||
|
# Try with empty files first
|
||||||
f = mod.open(_fname, 'c')
|
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["1"] = "1"
|
||||||
f.close()
|
f.close()
|
||||||
self.assertEqual(name, whichdb.whichdb(_fname))
|
self.assertEqual(name, whichdb.whichdb(_fname))
|
||||||
|
|
|
||||||
|
|
@ -50,15 +50,19 @@ def whichdb(filename):
|
||||||
|
|
||||||
# Check for dumbdbm next -- this has a .dir and and a .dat file
|
# Check for dumbdbm next -- this has a .dir and and a .dat file
|
||||||
try:
|
try:
|
||||||
f = open(filename + os.extsep + "dat", "rb")
|
# First check for presence of files
|
||||||
f.close()
|
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")
|
f = open(filename + os.extsep + "dir", "rb")
|
||||||
try:
|
try:
|
||||||
if f.read(1) in ["'", '"']:
|
if f.read(1) in ["'", '"']:
|
||||||
return "dumbdbm"
|
return "dumbdbm"
|
||||||
finally:
|
finally:
|
||||||
f.close()
|
f.close()
|
||||||
except IOError:
|
except (OSError, IOError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# See if the file exists, return None if not
|
# See if the file exists, return None if not
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue