Skip to content

Commit a5436d9

Browse files
bpo-45502: Fix test_shelve
Run test_shelve with all underlying dbm implementations and pickle protocols. Also make test_shelve discoverable.
1 parent 11b2ae7 commit a5436d9

File tree

1 file changed

+46
-58
lines changed

1 file changed

+46
-58
lines changed

Lib/test/test_shelve.py

Lines changed: 46 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
import dbm
23
import shelve
34
import glob
45
import pickle
@@ -44,12 +45,8 @@ def copy(self):
4445

4546

4647
class TestCase(unittest.TestCase):
47-
48-
fn = "shelftemp.db"
49-
50-
def tearDown(self):
51-
for f in glob.glob(self.fn+"*"):
52-
os_helper.unlink(f)
48+
dirname = os_helper.TESTFN
49+
fn = os.path.join(os_helper.TESTFN, "shelftemp.db")
5350

5451
def test_close(self):
5552
d1 = {}
@@ -67,6 +64,8 @@ def test_close(self):
6764
self.fail('Closed shelf should not find a key')
6865

6966
def test_open_template(self, filename=None, protocol=None):
67+
os.mkdir(self.dirname)
68+
self.addCleanup(os_helper.rmtree, self.dirname)
7069
s = shelve.open(filename=filename if filename is not None else self.fn,
7170
protocol=protocol)
7271
try:
@@ -168,63 +167,52 @@ def test_default_protocol(self):
168167
with shelve.Shelf({}) as s:
169168
self.assertEqual(s._protocol, pickle.DEFAULT_PROTOCOL)
170169

171-
from test import mapping_tests
172170

173-
class TestShelveBase(mapping_tests.BasicTestMappingProtocol):
174-
fn = "shelftemp.db"
175-
counter = 0
176-
def __init__(self, *args, **kw):
177-
self._db = []
178-
mapping_tests.BasicTestMappingProtocol.__init__(self, *args, **kw)
171+
class TestShelveBase:
179172
type2test = shelve.Shelf
173+
180174
def _reference(self):
181175
return {"key1":"value1", "key2":2, "key3":(1,2,3)}
176+
177+
178+
class TestShelveInMemBase(TestShelveBase):
182179
def _empty_mapping(self):
183-
if self._in_mem:
184-
x= shelve.Shelf(byteskeydict(), **self._args)
185-
else:
186-
self.counter+=1
187-
x= shelve.open(self.fn+str(self.counter), **self._args)
188-
self._db.append(x)
180+
return shelve.Shelf(byteskeydict(), **self._args)
181+
182+
183+
class TestShelveFileBase(TestShelveBase):
184+
counter = 0
185+
186+
def _empty_mapping(self):
187+
self.counter += 1
188+
x = shelve.open(self.base_path + str(self.counter), **self._args)
189+
self.addCleanup(x.close)
189190
return x
190-
def tearDown(self):
191-
for db in self._db:
192-
db.close()
193-
self._db = []
194-
if not self._in_mem:
195-
for f in glob.glob(self.fn+"*"):
196-
os_helper.unlink(f)
197-
198-
class TestAsciiFileShelve(TestShelveBase):
199-
_args={'protocol':0}
200-
_in_mem = False
201-
class TestBinaryFileShelve(TestShelveBase):
202-
_args={'protocol':1}
203-
_in_mem = False
204-
class TestProto2FileShelve(TestShelveBase):
205-
_args={'protocol':2}
206-
_in_mem = False
207-
class TestAsciiMemShelve(TestShelveBase):
208-
_args={'protocol':0}
209-
_in_mem = True
210-
class TestBinaryMemShelve(TestShelveBase):
211-
_args={'protocol':1}
212-
_in_mem = True
213-
class TestProto2MemShelve(TestShelveBase):
214-
_args={'protocol':2}
215-
_in_mem = True
216-
217-
def test_main():
218-
for module in dbm_iterator():
219-
support.run_unittest(
220-
TestAsciiFileShelve,
221-
TestBinaryFileShelve,
222-
TestProto2FileShelve,
223-
TestAsciiMemShelve,
224-
TestBinaryMemShelve,
225-
TestProto2MemShelve,
226-
TestCase
227-
)
191+
192+
def setUp(self):
193+
dirname = os_helper.TESTFN
194+
os.mkdir(dirname)
195+
self.addCleanup(os_helper.rmtree, dirname)
196+
self.base_path = os.path.join(dirname, "shelftemp.db")
197+
self.addCleanup(setattr, dbm, '_defaultmod', dbm._defaultmod)
198+
dbm._defaultmod = self.dbm_mod
199+
200+
201+
from test import mapping_tests
202+
203+
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
204+
bases = (TestShelveInMemBase, mapping_tests.BasicTestMappingProtocol)
205+
name = f'TestProto{proto}MemShelve'
206+
globals()[name] = type(name, bases,
207+
{'_args': {'protocol': proto}})
208+
bases = (TestShelveFileBase, mapping_tests.BasicTestMappingProtocol)
209+
for dbm_mod in dbm_iterator():
210+
assert dbm_mod.__name__.startswith('dbm.')
211+
suffix = dbm_mod.__name__[4:]
212+
name = f'TestProto{proto}File_{suffix}Shelve'
213+
globals()[name] = type(name, bases,
214+
{'dbm_mod': dbm_mod, '_args': {'protocol': proto}})
215+
228216

229217
if __name__ == "__main__":
230-
test_main()
218+
unittest.main()

0 commit comments

Comments
 (0)