summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_shelve.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2019-03-05 08:06:26 (GMT)
committerGitHub <noreply@github.com>2019-03-05 08:06:26 (GMT)
commit5b10b9824780b2181158902067912ee9e7b04657 (patch)
tree1c89bea944e6638eb008c8f106b2ee48cc9448d1 /Lib/test/test_shelve.py
parent9e4861f52349011cd5916eef8e8344575e8ac426 (diff)
downloadcpython-5b10b9824780b2181158902067912ee9e7b04657.zip
cpython-5b10b9824780b2181158902067912ee9e7b04657.tar.gz
cpython-5b10b9824780b2181158902067912ee9e7b04657.tar.bz2
bpo-22831: Use "with" to avoid possible fd leaks in tests (part 2). (GH-10929)
Diffstat (limited to 'Lib/test/test_shelve.py')
-rw-r--r--Lib/test/test_shelve.py45
1 files changed, 20 insertions, 25 deletions
diff --git a/Lib/test/test_shelve.py b/Lib/test/test_shelve.py
index b71af2b..9ffe2cb 100644
--- a/Lib/test/test_shelve.py
+++ b/Lib/test/test_shelve.py
@@ -88,15 +88,13 @@ class TestCase(unittest.TestCase):
def test_in_memory_shelf(self):
d1 = byteskeydict()
- s = shelve.Shelf(d1, protocol=0)
- s['key1'] = (1,2,3,4)
- self.assertEqual(s['key1'], (1,2,3,4))
- s.close()
+ with shelve.Shelf(d1, protocol=0) as s:
+ s['key1'] = (1,2,3,4)
+ self.assertEqual(s['key1'], (1,2,3,4))
d2 = byteskeydict()
- s = shelve.Shelf(d2, protocol=1)
- s['key1'] = (1,2,3,4)
- self.assertEqual(s['key1'], (1,2,3,4))
- s.close()
+ with shelve.Shelf(d2, protocol=1) as s:
+ s['key1'] = (1,2,3,4)
+ self.assertEqual(s['key1'], (1,2,3,4))
self.assertEqual(len(d1), 1)
self.assertEqual(len(d2), 1)
@@ -104,20 +102,18 @@ class TestCase(unittest.TestCase):
def test_mutable_entry(self):
d1 = byteskeydict()
- s = shelve.Shelf(d1, protocol=2, writeback=False)
- s['key1'] = [1,2,3,4]
- self.assertEqual(s['key1'], [1,2,3,4])
- s['key1'].append(5)
- self.assertEqual(s['key1'], [1,2,3,4])
- s.close()
+ with shelve.Shelf(d1, protocol=2, writeback=False) as s:
+ s['key1'] = [1,2,3,4]
+ self.assertEqual(s['key1'], [1,2,3,4])
+ s['key1'].append(5)
+ self.assertEqual(s['key1'], [1,2,3,4])
d2 = byteskeydict()
- s = shelve.Shelf(d2, protocol=2, writeback=True)
- s['key1'] = [1,2,3,4]
- self.assertEqual(s['key1'], [1,2,3,4])
- s['key1'].append(5)
- self.assertEqual(s['key1'], [1,2,3,4,5])
- s.close()
+ with shelve.Shelf(d2, protocol=2, writeback=True) as s:
+ s['key1'] = [1,2,3,4]
+ self.assertEqual(s['key1'], [1,2,3,4])
+ s['key1'].append(5)
+ self.assertEqual(s['key1'], [1,2,3,4,5])
self.assertEqual(len(d1), 1)
self.assertEqual(len(d2), 1)
@@ -140,11 +136,10 @@ class TestCase(unittest.TestCase):
d = {}
key = 'key'
encodedkey = key.encode('utf-8')
- s = shelve.Shelf(d, writeback=True)
- s[key] = [1]
- p1 = d[encodedkey] # Will give a KeyError if backing store not updated
- s['key'].append(2)
- s.close()
+ with shelve.Shelf(d, writeback=True) as s:
+ s[key] = [1]
+ p1 = d[encodedkey] # Will give a KeyError if backing store not updated
+ s['key'].append(2)
p2 = d[encodedkey]
self.assertNotEqual(p1, p2) # Write creates new object in store