mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00
bpo-22831: Use "with" to avoid possible fd leaks in tests (part 1). (GH-10928)
This commit is contained in:
parent
b727239575
commit
9e4861f523
5 changed files with 247 additions and 291 deletions
|
@ -2,6 +2,7 @@
|
|||
Original by Roger E. Masse
|
||||
"""
|
||||
|
||||
import contextlib
|
||||
import io
|
||||
import operator
|
||||
import os
|
||||
|
@ -32,12 +33,11 @@ class DumbDBMTestCase(unittest.TestCase):
|
|||
}
|
||||
|
||||
def test_dumbdbm_creation(self):
|
||||
f = dumbdbm.open(_fname, 'c')
|
||||
self.assertEqual(list(f.keys()), [])
|
||||
for key in self._dict:
|
||||
f[key] = self._dict[key]
|
||||
self.read_helper(f)
|
||||
f.close()
|
||||
with contextlib.closing(dumbdbm.open(_fname, 'c')) as f:
|
||||
self.assertEqual(list(f.keys()), [])
|
||||
for key in self._dict:
|
||||
f[key] = self._dict[key]
|
||||
self.read_helper(f)
|
||||
|
||||
@unittest.skipUnless(hasattr(os, 'umask'), 'test needs os.umask()')
|
||||
def test_dumbdbm_creation_mode(self):
|
||||
|
@ -69,78 +69,70 @@ class DumbDBMTestCase(unittest.TestCase):
|
|||
|
||||
def test_dumbdbm_modification(self):
|
||||
self.init_db()
|
||||
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()
|
||||
with contextlib.closing(dumbdbm.open(_fname, 'w')) as f:
|
||||
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')
|
||||
|
||||
def test_dumbdbm_read(self):
|
||||
self.init_db()
|
||||
f = dumbdbm.open(_fname, 'r')
|
||||
self.read_helper(f)
|
||||
with self.assertRaisesRegex(dumbdbm.error,
|
||||
'The database is opened for reading only'):
|
||||
f[b'g'] = b'x'
|
||||
with self.assertRaisesRegex(dumbdbm.error,
|
||||
'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()
|
||||
with contextlib.closing(dumbdbm.open(_fname, 'r')) as f:
|
||||
self.read_helper(f)
|
||||
with self.assertRaisesRegex(dumbdbm.error,
|
||||
'The database is opened for reading only'):
|
||||
f[b'g'] = b'x'
|
||||
with self.assertRaisesRegex(dumbdbm.error,
|
||||
'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']
|
||||
|
||||
def test_dumbdbm_keys(self):
|
||||
self.init_db()
|
||||
f = dumbdbm.open(_fname)
|
||||
keys = self.keys_helper(f)
|
||||
f.close()
|
||||
with contextlib.closing(dumbdbm.open(_fname)) as f:
|
||||
keys = self.keys_helper(f)
|
||||
|
||||
def test_write_contains(self):
|
||||
f = dumbdbm.open(_fname)
|
||||
f[b'1'] = b'hello'
|
||||
self.assertIn(b'1', f)
|
||||
f.close()
|
||||
with contextlib.closing(dumbdbm.open(_fname)) as f:
|
||||
f[b'1'] = b'hello'
|
||||
self.assertIn(b'1', f)
|
||||
|
||||
def test_write_write_read(self):
|
||||
# test for bug #482460
|
||||
f = dumbdbm.open(_fname)
|
||||
f[b'1'] = b'hello'
|
||||
f[b'1'] = b'hello2'
|
||||
f.close()
|
||||
f = dumbdbm.open(_fname)
|
||||
self.assertEqual(f[b'1'], b'hello2')
|
||||
f.close()
|
||||
with contextlib.closing(dumbdbm.open(_fname)) as f:
|
||||
f[b'1'] = b'hello'
|
||||
f[b'1'] = b'hello2'
|
||||
with contextlib.closing(dumbdbm.open(_fname)) as f:
|
||||
self.assertEqual(f[b'1'], b'hello2')
|
||||
|
||||
def test_str_read(self):
|
||||
self.init_db()
|
||||
f = dumbdbm.open(_fname, 'r')
|
||||
self.assertEqual(f['\u00fc'], self._dict['\u00fc'.encode('utf-8')])
|
||||
with contextlib.closing(dumbdbm.open(_fname, 'r')) as f:
|
||||
self.assertEqual(f['\u00fc'], self._dict['\u00fc'.encode('utf-8')])
|
||||
|
||||
def test_str_write_contains(self):
|
||||
self.init_db()
|
||||
f = dumbdbm.open(_fname)
|
||||
f['\u00fc'] = b'!'
|
||||
f['1'] = 'a'
|
||||
f.close()
|
||||
f = dumbdbm.open(_fname, 'r')
|
||||
self.assertIn('\u00fc', f)
|
||||
self.assertEqual(f['\u00fc'.encode('utf-8')],
|
||||
self._dict['\u00fc'.encode('utf-8')])
|
||||
self.assertEqual(f[b'1'], b'a')
|
||||
with contextlib.closing(dumbdbm.open(_fname)) as f:
|
||||
f['\u00fc'] = b'!'
|
||||
f['1'] = 'a'
|
||||
with contextlib.closing(dumbdbm.open(_fname, 'r')) as f:
|
||||
self.assertIn('\u00fc', f)
|
||||
self.assertEqual(f['\u00fc'.encode('utf-8')],
|
||||
self._dict['\u00fc'.encode('utf-8')])
|
||||
self.assertEqual(f[b'1'], b'a')
|
||||
|
||||
def test_line_endings(self):
|
||||
# test for bug #1172763: dumbdbm would die if the line endings
|
||||
# weren't what was expected.
|
||||
f = dumbdbm.open(_fname)
|
||||
f[b'1'] = b'hello'
|
||||
f[b'2'] = b'hello2'
|
||||
f.close()
|
||||
with contextlib.closing(dumbdbm.open(_fname)) as f:
|
||||
f[b'1'] = b'hello'
|
||||
f[b'2'] = b'hello2'
|
||||
|
||||
# Mangle the file by changing the line separator to Windows or Unix
|
||||
with io.open(_fname + '.dir', 'rb') as file:
|
||||
|
@ -163,10 +155,9 @@ class DumbDBMTestCase(unittest.TestCase):
|
|||
self.assertEqual(self._dict[key], f[key])
|
||||
|
||||
def init_db(self):
|
||||
f = dumbdbm.open(_fname, 'n')
|
||||
for k in self._dict:
|
||||
f[k] = self._dict[k]
|
||||
f.close()
|
||||
with contextlib.closing(dumbdbm.open(_fname, 'n')) as f:
|
||||
for k in self._dict:
|
||||
f[k] = self._dict[k]
|
||||
|
||||
def keys_helper(self, f):
|
||||
keys = sorted(f.keys())
|
||||
|
@ -180,25 +171,23 @@ class DumbDBMTestCase(unittest.TestCase):
|
|||
import random
|
||||
d = {} # mirror the database
|
||||
for dummy in range(5):
|
||||
f = dumbdbm.open(_fname)
|
||||
for dummy in range(100):
|
||||
k = random.choice('abcdefghijklm')
|
||||
if random.random() < 0.2:
|
||||
if k in d:
|
||||
del d[k]
|
||||
del f[k]
|
||||
else:
|
||||
v = random.choice((b'a', b'b', b'c')) * random.randrange(10000)
|
||||
d[k] = v
|
||||
f[k] = v
|
||||
self.assertEqual(f[k], v)
|
||||
f.close()
|
||||
with contextlib.closing(dumbdbm.open(_fname)) as f:
|
||||
for dummy in range(100):
|
||||
k = random.choice('abcdefghijklm')
|
||||
if random.random() < 0.2:
|
||||
if k in d:
|
||||
del d[k]
|
||||
del f[k]
|
||||
else:
|
||||
v = random.choice((b'a', b'b', b'c')) * random.randrange(10000)
|
||||
d[k] = v
|
||||
f[k] = v
|
||||
self.assertEqual(f[k], v)
|
||||
|
||||
f = dumbdbm.open(_fname)
|
||||
expected = sorted((k.encode("latin-1"), v) for k, v in d.items())
|
||||
got = sorted(f.items())
|
||||
self.assertEqual(expected, got)
|
||||
f.close()
|
||||
with contextlib.closing(dumbdbm.open(_fname)) as f:
|
||||
expected = sorted((k.encode("latin-1"), v) for k, v in d.items())
|
||||
got = sorted(f.items())
|
||||
self.assertEqual(expected, got)
|
||||
|
||||
def test_context_manager(self):
|
||||
with dumbdbm.open(_fname, 'c') as db:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue