summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorXiang Zhang <angwerzx@126.com>2018-12-12 12:46:55 (GMT)
committerGitHub <noreply@github.com>2018-12-12 12:46:55 (GMT)
commit4fb0b8bc25c52aae8dcb4353e69c1c88999a9a53 (patch)
treeaa3b18ad394fbd5056af5a366e2b4ca6b91d5296 /Lib
parent5a718e918db6211b633a7afb2bf537eb5b56cb1b (diff)
downloadcpython-4fb0b8bc25c52aae8dcb4353e69c1c88999a9a53.zip
cpython-4fb0b8bc25c52aae8dcb4353e69c1c88999a9a53.tar.gz
cpython-4fb0b8bc25c52aae8dcb4353e69c1c88999a9a53.tar.bz2
bpo-33106: change dbm key deletion error for readonly file from KeyError to dbm.error (#6295)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/dbm/dumb.py4
-rw-r--r--Lib/test/test_dbm_dumb.py4
-rw-r--r--Lib/test/test_dbm_gnu.py11
-rw-r--r--Lib/test/test_dbm_ndbm.py11
4 files changed, 26 insertions, 4 deletions
diff --git a/Lib/dbm/dumb.py b/Lib/dbm/dumb.py
index e5c17f5..6cef72a 100644
--- a/Lib/dbm/dumb.py
+++ b/Lib/dbm/dumb.py
@@ -185,7 +185,7 @@ class _Database(collections.abc.MutableMapping):
def __setitem__(self, key, val):
if self._readonly:
- raise ValueError('The database is opened for reading only')
+ raise error('The database is opened for reading only')
if isinstance(key, str):
key = key.encode('utf-8')
elif not isinstance(key, (bytes, bytearray)):
@@ -222,7 +222,7 @@ class _Database(collections.abc.MutableMapping):
def __delitem__(self, key):
if self._readonly:
- raise ValueError('The database is opened for reading only')
+ raise error('The database is opened for reading only')
if isinstance(key, str):
key = key.encode('utf-8')
self._verify_open()
diff --git a/Lib/test/test_dbm_dumb.py b/Lib/test/test_dbm_dumb.py
index 58b9d17..e8e827e 100644
--- a/Lib/test/test_dbm_dumb.py
+++ b/Lib/test/test_dbm_dumb.py
@@ -82,10 +82,10 @@ class DumbDBMTestCase(unittest.TestCase):
self.init_db()
f = dumbdbm.open(_fname, 'r')
self.read_helper(f)
- with self.assertRaisesRegex(ValueError,
+ with self.assertRaisesRegex(dumbdbm.error,
'The database is opened for reading only'):
f[b'g'] = b'x'
- with self.assertRaisesRegex(ValueError,
+ with self.assertRaisesRegex(dumbdbm.error,
'The database is opened for reading only'):
del f[b'a']
# get() works as in the dict interface
diff --git a/Lib/test/test_dbm_gnu.py b/Lib/test/test_dbm_gnu.py
index 16b7fe6..f1c7d34 100644
--- a/Lib/test/test_dbm_gnu.py
+++ b/Lib/test/test_dbm_gnu.py
@@ -131,6 +131,17 @@ class TestGdbm(unittest.TestCase):
self.assertEqual(db['Unicode key \U0001f40d'],
'Unicode value \U0001f40d'.encode())
+ def test_write_readonly_file(self):
+ with gdbm.open(filename, 'c') as db:
+ db[b'bytes key'] = b'bytes value'
+ with gdbm.open(filename, 'r') as db:
+ with self.assertRaises(gdbm.error):
+ del db[b'not exist key']
+ with self.assertRaises(gdbm.error):
+ del db[b'bytes key']
+ with self.assertRaises(gdbm.error):
+ db[b'not exist key'] = b'not exist value'
+
@unittest.skipUnless(TESTFN_NONASCII,
'requires OS support of non-ASCII encodings')
def test_nonascii_filename(self):
diff --git a/Lib/test/test_dbm_ndbm.py b/Lib/test/test_dbm_ndbm.py
index bd411da..7ac75c5 100644
--- a/Lib/test/test_dbm_ndbm.py
+++ b/Lib/test/test_dbm_ndbm.py
@@ -90,6 +90,17 @@ class DbmTestCase(unittest.TestCase):
self.assertEqual(db['Unicode key \U0001f40d'],
'Unicode value \U0001f40d'.encode())
+ def test_write_readonly_file(self):
+ with dbm.ndbm.open(self.filename, 'c') as db:
+ db[b'bytes key'] = b'bytes value'
+ with dbm.ndbm.open(self.filename, 'r') as db:
+ with self.assertRaises(error):
+ del db[b'not exist key']
+ with self.assertRaises(error):
+ del db[b'bytes key']
+ with self.assertRaises(error):
+ db[b'not exist key'] = b'not exist value'
+
@unittest.skipUnless(support.TESTFN_NONASCII,
'requires OS support of non-ASCII encodings')
def test_nonascii_filename(self):