1 | n/a | import unittest |
---|
2 | n/a | import shelve |
---|
3 | n/a | import glob |
---|
4 | n/a | from test import support |
---|
5 | n/a | from collections.abc import MutableMapping |
---|
6 | n/a | from test.test_dbm import dbm_iterator |
---|
7 | n/a | |
---|
8 | n/a | def L1(s): |
---|
9 | n/a | return s.decode("latin-1") |
---|
10 | n/a | |
---|
11 | n/a | class byteskeydict(MutableMapping): |
---|
12 | n/a | "Mapping that supports bytes keys" |
---|
13 | n/a | |
---|
14 | n/a | def __init__(self): |
---|
15 | n/a | self.d = {} |
---|
16 | n/a | |
---|
17 | n/a | def __getitem__(self, key): |
---|
18 | n/a | return self.d[L1(key)] |
---|
19 | n/a | |
---|
20 | n/a | def __setitem__(self, key, value): |
---|
21 | n/a | self.d[L1(key)] = value |
---|
22 | n/a | |
---|
23 | n/a | def __delitem__(self, key): |
---|
24 | n/a | del self.d[L1(key)] |
---|
25 | n/a | |
---|
26 | n/a | def __len__(self): |
---|
27 | n/a | return len(self.d) |
---|
28 | n/a | |
---|
29 | n/a | def iterkeys(self): |
---|
30 | n/a | for k in self.d.keys(): |
---|
31 | n/a | yield k.encode("latin-1") |
---|
32 | n/a | |
---|
33 | n/a | __iter__ = iterkeys |
---|
34 | n/a | |
---|
35 | n/a | def keys(self): |
---|
36 | n/a | return list(self.iterkeys()) |
---|
37 | n/a | |
---|
38 | n/a | def copy(self): |
---|
39 | n/a | return byteskeydict(self.d) |
---|
40 | n/a | |
---|
41 | n/a | |
---|
42 | n/a | class TestCase(unittest.TestCase): |
---|
43 | n/a | |
---|
44 | n/a | fn = "shelftemp.db" |
---|
45 | n/a | |
---|
46 | n/a | def tearDown(self): |
---|
47 | n/a | for f in glob.glob(self.fn+"*"): |
---|
48 | n/a | support.unlink(f) |
---|
49 | n/a | |
---|
50 | n/a | def test_close(self): |
---|
51 | n/a | d1 = {} |
---|
52 | n/a | s = shelve.Shelf(d1, protocol=2, writeback=False) |
---|
53 | n/a | s['key1'] = [1,2,3,4] |
---|
54 | n/a | self.assertEqual(s['key1'], [1,2,3,4]) |
---|
55 | n/a | self.assertEqual(len(s), 1) |
---|
56 | n/a | s.close() |
---|
57 | n/a | self.assertRaises(ValueError, len, s) |
---|
58 | n/a | try: |
---|
59 | n/a | s['key1'] |
---|
60 | n/a | except ValueError: |
---|
61 | n/a | pass |
---|
62 | n/a | else: |
---|
63 | n/a | self.fail('Closed shelf should not find a key') |
---|
64 | n/a | |
---|
65 | n/a | def test_ascii_file_shelf(self): |
---|
66 | n/a | s = shelve.open(self.fn, protocol=0) |
---|
67 | n/a | try: |
---|
68 | n/a | s['key1'] = (1,2,3,4) |
---|
69 | n/a | self.assertEqual(s['key1'], (1,2,3,4)) |
---|
70 | n/a | finally: |
---|
71 | n/a | s.close() |
---|
72 | n/a | |
---|
73 | n/a | def test_binary_file_shelf(self): |
---|
74 | n/a | s = shelve.open(self.fn, protocol=1) |
---|
75 | n/a | try: |
---|
76 | n/a | s['key1'] = (1,2,3,4) |
---|
77 | n/a | self.assertEqual(s['key1'], (1,2,3,4)) |
---|
78 | n/a | finally: |
---|
79 | n/a | s.close() |
---|
80 | n/a | |
---|
81 | n/a | def test_proto2_file_shelf(self): |
---|
82 | n/a | s = shelve.open(self.fn, protocol=2) |
---|
83 | n/a | try: |
---|
84 | n/a | s['key1'] = (1,2,3,4) |
---|
85 | n/a | self.assertEqual(s['key1'], (1,2,3,4)) |
---|
86 | n/a | finally: |
---|
87 | n/a | s.close() |
---|
88 | n/a | |
---|
89 | n/a | def test_in_memory_shelf(self): |
---|
90 | n/a | d1 = byteskeydict() |
---|
91 | n/a | s = shelve.Shelf(d1, protocol=0) |
---|
92 | n/a | s['key1'] = (1,2,3,4) |
---|
93 | n/a | self.assertEqual(s['key1'], (1,2,3,4)) |
---|
94 | n/a | s.close() |
---|
95 | n/a | d2 = byteskeydict() |
---|
96 | n/a | s = shelve.Shelf(d2, protocol=1) |
---|
97 | n/a | s['key1'] = (1,2,3,4) |
---|
98 | n/a | self.assertEqual(s['key1'], (1,2,3,4)) |
---|
99 | n/a | s.close() |
---|
100 | n/a | |
---|
101 | n/a | self.assertEqual(len(d1), 1) |
---|
102 | n/a | self.assertEqual(len(d2), 1) |
---|
103 | n/a | self.assertNotEqual(d1.items(), d2.items()) |
---|
104 | n/a | |
---|
105 | n/a | def test_mutable_entry(self): |
---|
106 | n/a | d1 = byteskeydict() |
---|
107 | n/a | s = shelve.Shelf(d1, protocol=2, writeback=False) |
---|
108 | n/a | s['key1'] = [1,2,3,4] |
---|
109 | n/a | self.assertEqual(s['key1'], [1,2,3,4]) |
---|
110 | n/a | s['key1'].append(5) |
---|
111 | n/a | self.assertEqual(s['key1'], [1,2,3,4]) |
---|
112 | n/a | s.close() |
---|
113 | n/a | |
---|
114 | n/a | d2 = byteskeydict() |
---|
115 | n/a | s = shelve.Shelf(d2, protocol=2, writeback=True) |
---|
116 | n/a | s['key1'] = [1,2,3,4] |
---|
117 | n/a | self.assertEqual(s['key1'], [1,2,3,4]) |
---|
118 | n/a | s['key1'].append(5) |
---|
119 | n/a | self.assertEqual(s['key1'], [1,2,3,4,5]) |
---|
120 | n/a | s.close() |
---|
121 | n/a | |
---|
122 | n/a | self.assertEqual(len(d1), 1) |
---|
123 | n/a | self.assertEqual(len(d2), 1) |
---|
124 | n/a | |
---|
125 | n/a | def test_keyencoding(self): |
---|
126 | n/a | d = {} |
---|
127 | n/a | key = 'Pöp' |
---|
128 | n/a | # the default keyencoding is utf-8 |
---|
129 | n/a | shelve.Shelf(d)[key] = [1] |
---|
130 | n/a | self.assertIn(key.encode('utf-8'), d) |
---|
131 | n/a | # but a different one can be given |
---|
132 | n/a | shelve.Shelf(d, keyencoding='latin-1')[key] = [1] |
---|
133 | n/a | self.assertIn(key.encode('latin-1'), d) |
---|
134 | n/a | # with all consequences |
---|
135 | n/a | s = shelve.Shelf(d, keyencoding='ascii') |
---|
136 | n/a | self.assertRaises(UnicodeEncodeError, s.__setitem__, key, [1]) |
---|
137 | n/a | |
---|
138 | n/a | def test_writeback_also_writes_immediately(self): |
---|
139 | n/a | # Issue 5754 |
---|
140 | n/a | d = {} |
---|
141 | n/a | key = 'key' |
---|
142 | n/a | encodedkey = key.encode('utf-8') |
---|
143 | n/a | s = shelve.Shelf(d, writeback=True) |
---|
144 | n/a | s[key] = [1] |
---|
145 | n/a | p1 = d[encodedkey] # Will give a KeyError if backing store not updated |
---|
146 | n/a | s['key'].append(2) |
---|
147 | n/a | s.close() |
---|
148 | n/a | p2 = d[encodedkey] |
---|
149 | n/a | self.assertNotEqual(p1, p2) # Write creates new object in store |
---|
150 | n/a | |
---|
151 | n/a | def test_with(self): |
---|
152 | n/a | d1 = {} |
---|
153 | n/a | with shelve.Shelf(d1, protocol=2, writeback=False) as s: |
---|
154 | n/a | s['key1'] = [1,2,3,4] |
---|
155 | n/a | self.assertEqual(s['key1'], [1,2,3,4]) |
---|
156 | n/a | self.assertEqual(len(s), 1) |
---|
157 | n/a | self.assertRaises(ValueError, len, s) |
---|
158 | n/a | try: |
---|
159 | n/a | s['key1'] |
---|
160 | n/a | except ValueError: |
---|
161 | n/a | pass |
---|
162 | n/a | else: |
---|
163 | n/a | self.fail('Closed shelf should not find a key') |
---|
164 | n/a | |
---|
165 | n/a | def test_default_protocol(self): |
---|
166 | n/a | with shelve.Shelf({}) as s: |
---|
167 | n/a | self.assertEqual(s._protocol, 3) |
---|
168 | n/a | |
---|
169 | n/a | from test import mapping_tests |
---|
170 | n/a | |
---|
171 | n/a | class TestShelveBase(mapping_tests.BasicTestMappingProtocol): |
---|
172 | n/a | fn = "shelftemp.db" |
---|
173 | n/a | counter = 0 |
---|
174 | n/a | def __init__(self, *args, **kw): |
---|
175 | n/a | self._db = [] |
---|
176 | n/a | mapping_tests.BasicTestMappingProtocol.__init__(self, *args, **kw) |
---|
177 | n/a | type2test = shelve.Shelf |
---|
178 | n/a | def _reference(self): |
---|
179 | n/a | return {"key1":"value1", "key2":2, "key3":(1,2,3)} |
---|
180 | n/a | def _empty_mapping(self): |
---|
181 | n/a | if self._in_mem: |
---|
182 | n/a | x= shelve.Shelf(byteskeydict(), **self._args) |
---|
183 | n/a | else: |
---|
184 | n/a | self.counter+=1 |
---|
185 | n/a | x= shelve.open(self.fn+str(self.counter), **self._args) |
---|
186 | n/a | self._db.append(x) |
---|
187 | n/a | return x |
---|
188 | n/a | def tearDown(self): |
---|
189 | n/a | for db in self._db: |
---|
190 | n/a | db.close() |
---|
191 | n/a | self._db = [] |
---|
192 | n/a | if not self._in_mem: |
---|
193 | n/a | for f in glob.glob(self.fn+"*"): |
---|
194 | n/a | support.unlink(f) |
---|
195 | n/a | |
---|
196 | n/a | class TestAsciiFileShelve(TestShelveBase): |
---|
197 | n/a | _args={'protocol':0} |
---|
198 | n/a | _in_mem = False |
---|
199 | n/a | class TestBinaryFileShelve(TestShelveBase): |
---|
200 | n/a | _args={'protocol':1} |
---|
201 | n/a | _in_mem = False |
---|
202 | n/a | class TestProto2FileShelve(TestShelveBase): |
---|
203 | n/a | _args={'protocol':2} |
---|
204 | n/a | _in_mem = False |
---|
205 | n/a | class TestAsciiMemShelve(TestShelveBase): |
---|
206 | n/a | _args={'protocol':0} |
---|
207 | n/a | _in_mem = True |
---|
208 | n/a | class TestBinaryMemShelve(TestShelveBase): |
---|
209 | n/a | _args={'protocol':1} |
---|
210 | n/a | _in_mem = True |
---|
211 | n/a | class TestProto2MemShelve(TestShelveBase): |
---|
212 | n/a | _args={'protocol':2} |
---|
213 | n/a | _in_mem = True |
---|
214 | n/a | |
---|
215 | n/a | def test_main(): |
---|
216 | n/a | for module in dbm_iterator(): |
---|
217 | n/a | support.run_unittest( |
---|
218 | n/a | TestAsciiFileShelve, |
---|
219 | n/a | TestBinaryFileShelve, |
---|
220 | n/a | TestProto2FileShelve, |
---|
221 | n/a | TestAsciiMemShelve, |
---|
222 | n/a | TestBinaryMemShelve, |
---|
223 | n/a | TestProto2MemShelve, |
---|
224 | n/a | TestCase |
---|
225 | n/a | ) |
---|
226 | n/a | |
---|
227 | n/a | if __name__ == "__main__": |
---|
228 | n/a | test_main() |
---|