diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2018-04-29 09:38:06 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-29 09:38:06 (GMT) |
commit | 2e38cc39330bd7f3003652869b644110a97a78d8 (patch) | |
tree | 5f1a63d9c23d486cdbf8a588e442d3c307917b07 /Lib/test | |
parent | 577948329976985ea9bef23d9a6c3dd7108211bf (diff) | |
download | cpython-2e38cc39330bd7f3003652869b644110a97a78d8.zip cpython-2e38cc39330bd7f3003652869b644110a97a78d8.tar.gz cpython-2e38cc39330bd7f3003652869b644110a97a78d8.tar.bz2 |
bpo-33383: Fix crash in get() of the dbm.ndbm database object. (#6630)
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_dbm.py | 9 | ||||
-rw-r--r-- | Lib/test/test_dbm_dumb.py | 9 | ||||
-rw-r--r-- | Lib/test/test_dbm_gnu.py | 5 | ||||
-rw-r--r-- | Lib/test/test_dbm_ndbm.py | 10 |
4 files changed, 31 insertions, 2 deletions
diff --git a/Lib/test/test_dbm.py b/Lib/test/test_dbm.py index f0a428d..fb89807 100644 --- a/Lib/test/test_dbm.py +++ b/Lib/test/test_dbm.py @@ -86,12 +86,21 @@ class AnyDBMTestCase: f = dbm.open(_fname, 'c') self._dict['g'] = f[b'g'] = b"indented" self.read_helper(f) + # setdefault() works as in the dict interface + self.assertEqual(f.setdefault(b'xxx', b'foo'), b'foo') + self.assertEqual(f[b'xxx'], b'foo') f.close() def test_anydbm_read(self): self.init_db() f = dbm.open(_fname, 'r') self.read_helper(f) + # get() works as in the dict interface + self.assertEqual(f.get(b'a'), self._dict['a']) + self.assertEqual(f.get(b'xxx', b'foo'), b'foo') + self.assertIsNone(f.get(b'xxx')) + with self.assertRaises(KeyError): + f[b'xxx'] f.close() def test_anydbm_keys(self): diff --git a/Lib/test/test_dbm_dumb.py b/Lib/test/test_dbm_dumb.py index 652a355..58b9d17 100644 --- a/Lib/test/test_dbm_dumb.py +++ b/Lib/test/test_dbm_dumb.py @@ -73,6 +73,9 @@ class DumbDBMTestCase(unittest.TestCase): f = dumbdbm.open(_fname, 'w') self._dict[b'g'] = f[b'g'] = b"indented" self.read_helper(f) + # setdefault() works as in the dict interface + self.assertEqual(f.setdefault(b'xxx', b'foo'), b'foo') + self.assertEqual(f[b'xxx'], b'foo') f.close() def test_dumbdbm_read(self): @@ -85,6 +88,12 @@ class DumbDBMTestCase(unittest.TestCase): with self.assertRaisesRegex(ValueError, 'The database is opened for reading only'): del f[b'a'] + # get() works as in the dict interface + self.assertEqual(f.get(b'a'), self._dict[b'a']) + self.assertEqual(f.get(b'xxx', b'foo'), b'foo') + self.assertIsNone(f.get(b'xxx')) + with self.assertRaises(KeyError): + f[b'xxx'] f.close() def test_dumbdbm_keys(self): diff --git a/Lib/test/test_dbm_gnu.py b/Lib/test/test_dbm_gnu.py index d96df92..463d343 100644 --- a/Lib/test/test_dbm_gnu.py +++ b/Lib/test/test_dbm_gnu.py @@ -32,9 +32,12 @@ class TestGdbm(unittest.TestCase): self.assertIn(key, key_set) key_set.remove(key) key = self.g.nextkey(key) - self.assertRaises(KeyError, lambda: self.g['xxx']) # get() and setdefault() work as in the dict interface + self.assertEqual(self.g.get(b'a'), b'b') + self.assertIsNone(self.g.get(b'xxx')) self.assertEqual(self.g.get(b'xxx', b'foo'), b'foo') + with self.assertRaises(KeyError): + self.g['xxx'] self.assertEqual(self.g.setdefault(b'xxx', b'foo'), b'foo') self.assertEqual(self.g[b'xxx'], b'foo') diff --git a/Lib/test/test_dbm_ndbm.py b/Lib/test/test_dbm_ndbm.py index fb7d0e8..f921167 100644 --- a/Lib/test/test_dbm_ndbm.py +++ b/Lib/test/test_dbm_ndbm.py @@ -18,7 +18,7 @@ class DbmTestCase(unittest.TestCase): def test_keys(self): self.d = dbm.ndbm.open(self.filename, 'c') - self.assertTrue(self.d.keys() == []) + self.assertEqual(self.d.keys(), []) self.d['a'] = 'b' self.d[b'bytes'] = b'data' self.d['12345678910'] = '019237410982340912840198242' @@ -26,6 +26,14 @@ class DbmTestCase(unittest.TestCase): self.assertIn('a', self.d) self.assertIn(b'a', self.d) self.assertEqual(self.d[b'bytes'], b'data') + # get() and setdefault() work as in the dict interface + self.assertEqual(self.d.get(b'a'), b'b') + self.assertIsNone(self.d.get(b'xxx')) + self.assertEqual(self.d.get(b'xxx', b'foo'), b'foo') + with self.assertRaises(KeyError): + self.d['xxx'] + self.assertEqual(self.d.setdefault(b'xxx', b'foo'), b'foo') + self.assertEqual(self.d[b'xxx'], b'foo') self.d.close() def test_modes(self): |