mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
make operations on closed dumb databases raise a consistent exception (closes #19385)
Patch by Claudiu Popa.
This commit is contained in:
parent
d4992dccfb
commit
e3083d3ad5
3 changed files with 40 additions and 4 deletions
|
@ -3,10 +3,12 @@
|
|||
"""
|
||||
|
||||
import io
|
||||
import operator
|
||||
import os
|
||||
import unittest
|
||||
import dbm.dumb as dumbdbm
|
||||
from test import support
|
||||
from functools import partial
|
||||
|
||||
_fname = support.TESTFN
|
||||
|
||||
|
@ -190,12 +192,31 @@ class DumbDBMTestCase(unittest.TestCase):
|
|||
with dumbdbm.open(_fname, 'r') as db:
|
||||
self.assertEqual(list(db.keys()), [b"dumbdbm context manager"])
|
||||
|
||||
# This currently just raises AttributeError rather than a specific
|
||||
# exception like the GNU or NDBM based implementations. See
|
||||
# http://bugs.python.org/issue19385 for details.
|
||||
with self.assertRaises(Exception):
|
||||
with self.assertRaises(dumbdbm.error):
|
||||
db.keys()
|
||||
|
||||
def test_check_closed(self):
|
||||
f = dumbdbm.open(_fname, 'c')
|
||||
f.close()
|
||||
|
||||
for meth in (partial(operator.delitem, f),
|
||||
partial(operator.setitem, f, 'b'),
|
||||
partial(operator.getitem, f),
|
||||
partial(operator.contains, f)):
|
||||
with self.assertRaises(dumbdbm.error) as cm:
|
||||
meth('test')
|
||||
self.assertEqual(str(cm.exception),
|
||||
"DBM object has already been closed")
|
||||
|
||||
for meth in (operator.methodcaller('keys'),
|
||||
operator.methodcaller('iterkeys'),
|
||||
operator.methodcaller('items'),
|
||||
len):
|
||||
with self.assertRaises(dumbdbm.error) as cm:
|
||||
meth(f)
|
||||
self.assertEqual(str(cm.exception),
|
||||
"DBM object has already been closed")
|
||||
|
||||
def tearDown(self):
|
||||
_delete_files()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue