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 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 165 | from test import mapping_tests |
Skip Montanaro | 3bf99e3 | 2002-12-08 18:36:24 +0000 | [diff] [blame] | 166 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 167 | class TestShelveBase(mapping_tests.BasicTestMappingProtocol): |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 168 | fn = "shelftemp.db" |
| 169 | counter = 0 |
| 170 | def __init__(self, *args, **kw): |
| 171 | self._db = [] |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 172 | mapping_tests.BasicTestMappingProtocol.__init__(self, *args, **kw) |
Walter Dörwald | 118f931 | 2004-06-02 18:42:25 +0000 | [diff] [blame] | 173 | type2test = shelve.Shelf |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 174 | def _reference(self): |
| 175 | return {"key1":"value1", "key2":2, "key3":(1,2,3)} |
| 176 | def _empty_mapping(self): |
| 177 | if self._in_mem: |
Martin v. Löwis | 79c3208 | 2007-08-11 06:57:14 +0000 | [diff] [blame] | 178 | x= shelve.Shelf(byteskeydict(), **self._args) |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 179 | else: |
| 180 | self.counter+=1 |
Martin v. Löwis | 153c9e4 | 2003-04-19 20:59:03 +0000 | [diff] [blame] | 181 | x= shelve.open(self.fn+str(self.counter), **self._args) |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 182 | self._db.append(x) |
| 183 | return x |
| 184 | def tearDown(self): |
| 185 | for db in self._db: |
| 186 | db.close() |
| 187 | self._db = [] |
| 188 | if not self._in_mem: |
| 189 | for f in glob.glob(self.fn+"*"): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 190 | support.unlink(f) |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 191 | |
| 192 | class TestAsciiFileShelve(TestShelveBase): |
Raymond Hettinger | 1bc82f8 | 2004-12-05 03:58:17 +0000 | [diff] [blame] | 193 | _args={'protocol':0} |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 194 | _in_mem = False |
| 195 | class TestBinaryFileShelve(TestShelveBase): |
Raymond Hettinger | 1bc82f8 | 2004-12-05 03:58:17 +0000 | [diff] [blame] | 196 | _args={'protocol':1} |
Martin v. Löwis | 153c9e4 | 2003-04-19 20:59:03 +0000 | [diff] [blame] | 197 | _in_mem = False |
| 198 | class TestProto2FileShelve(TestShelveBase): |
| 199 | _args={'protocol':2} |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 200 | _in_mem = False |
| 201 | class TestAsciiMemShelve(TestShelveBase): |
Raymond Hettinger | 1bc82f8 | 2004-12-05 03:58:17 +0000 | [diff] [blame] | 202 | _args={'protocol':0} |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 203 | _in_mem = True |
| 204 | class TestBinaryMemShelve(TestShelveBase): |
Raymond Hettinger | 1bc82f8 | 2004-12-05 03:58:17 +0000 | [diff] [blame] | 205 | _args={'protocol':1} |
Martin v. Löwis | 153c9e4 | 2003-04-19 20:59:03 +0000 | [diff] [blame] | 206 | _in_mem = True |
| 207 | class TestProto2MemShelve(TestShelveBase): |
| 208 | _args={'protocol':2} |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 209 | _in_mem = True |
| 210 | |
| 211 | def test_main(): |
Gregory P. Smith | 178fefb | 2007-08-24 21:59:45 +0000 | [diff] [blame] | 212 | for module in dbm_iterator(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 213 | support.run_unittest( |
Gregory P. Smith | 178fefb | 2007-08-24 21:59:45 +0000 | [diff] [blame] | 214 | TestAsciiFileShelve, |
| 215 | TestBinaryFileShelve, |
| 216 | TestProto2FileShelve, |
| 217 | TestAsciiMemShelve, |
| 218 | TestBinaryMemShelve, |
| 219 | TestProto2MemShelve, |
| 220 | TestCase |
| 221 | ) |
Skip Montanaro | 3bf99e3 | 2002-12-08 18:36:24 +0000 | [diff] [blame] | 222 | |
| 223 | if __name__ == "__main__": |
| 224 | test_main() |