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