Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(43161)

Unified Diff: Lib/test/test_shelve.py

Issue 5178: Add context manager for temporary directory
Patch Set: Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Lib/test/test_commands.py ('k') | Lib/test/test_tempfile.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Lib/test/test_shelve.py
===================================================================
--- Lib/test/test_shelve.py (revision 69412)
+++ Lib/test/test_shelve.py (working copy)
@@ -2,6 +2,7 @@
import unittest
import shelve
import glob
+import tempfile
from test import test_support
class TestCase(unittest.TestCase):
@@ -24,34 +25,25 @@
self.fail('Closed shelf should not find a key')
def test_ascii_file_shelf(self):
- try:
- s = shelve.open(self.fn, protocol=0)
+ with tempfile.TemporaryDirectory() as tmpdir:
+ s = shelve.open(os.path.join(tmpdir, self.fn), protocol=0)
s['key1'] = (1,2,3,4)
self.assertEqual(s['key1'], (1,2,3,4))
s.close()
- finally:
- for f in glob.glob(self.fn+"*"):
- os.unlink(f)
def test_binary_file_shelf(self):
- try:
- s = shelve.open(self.fn, protocol=1)
+ with tempfile.TemporaryDirectory() as tmpdir:
+ s = shelve.open(os.path.join(tmpdir, self.fn), protocol=1)
s['key1'] = (1,2,3,4)
self.assertEqual(s['key1'], (1,2,3,4))
s.close()
- finally:
- for f in glob.glob(self.fn+"*"):
- os.unlink(f)
def test_proto2_file_shelf(self):
- try:
- s = shelve.open(self.fn, protocol=2)
+ with tempfile.TemporaryDirectory() as tmpdir:
+ s = shelve.open(os.path.join(tmpdir, self.fn), protocol=2)
s['key1'] = (1,2,3,4)
self.assertEqual(s['key1'], (1,2,3,4))
s.close()
- finally:
- for f in glob.glob(self.fn+"*"):
- os.unlink(f)
def test_in_memory_shelf(self):
d1 = {}
@@ -93,9 +85,9 @@
class TestShelveBase(mapping_tests.BasicTestMappingProtocol):
fn = "shelftemp.db"
- counter = 0
def __init__(self, *args, **kw):
self._db = []
+ self._dirnames = []
mapping_tests.BasicTestMappingProtocol.__init__(self, *args, **kw)
type2test = shelve.Shelf
def _reference(self):
@@ -104,17 +96,18 @@
if self._in_mem:
x= shelve.Shelf({}, **self._args)
else:
- self.counter+=1
- x= shelve.open(self.fn+str(self.counter), **self._args)
+ tmpdir = tempfile.mkdtemp()
+ x= shelve.open(os.path.join(tmpdir, self.fn), **self._args)
+ self._dirnames.append(tmpdir)
self._db.append(x)
return x
def tearDown(self):
for db in self._db:
db.close()
self._db = []
- if not self._in_mem:
- for f in glob.glob(self.fn+"*"):
- test_support.unlink(f)
+ for tmpdir in self._dirnames:
+ test_support.rmtree(tmpdir)
+ self._dirnames = []
class TestAsciiFileShelve(TestShelveBase):
_args={'protocol':0}
« no previous file with comments | « Lib/test/test_commands.py ('k') | Lib/test/test_tempfile.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+