2002-12-08 14:36:24 -04:00
|
|
|
import unittest
|
|
|
|
import shelve
|
|
|
|
import glob
|
2008-05-20 18:35:26 -03:00
|
|
|
from test import support
|
2008-02-04 18:07:15 -04:00
|
|
|
from collections import MutableMapping
|
2008-05-26 13:32:26 -03:00
|
|
|
from test.test_dbm import dbm_iterator
|
2007-08-11 03:57:14 -03:00
|
|
|
|
|
|
|
def L1(s):
|
|
|
|
return s.decode("latin-1")
|
|
|
|
|
2008-02-04 18:07:15 -04:00
|
|
|
class byteskeydict(MutableMapping):
|
2007-08-11 03:57:14 -03:00
|
|
|
"Mapping that supports bytes keys"
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.d = {}
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
return self.d[L1(key)]
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
self.d[L1(key)] = value
|
|
|
|
|
|
|
|
def __delitem__(self, key):
|
|
|
|
del self.d[L1(key)]
|
|
|
|
|
2008-02-04 18:07:15 -04:00
|
|
|
def __len__(self):
|
|
|
|
return len(self.d)
|
|
|
|
|
2007-08-11 03:57:14 -03:00
|
|
|
def iterkeys(self):
|
|
|
|
for k in self.d.keys():
|
2007-08-28 23:57:31 -03:00
|
|
|
yield k.encode("latin-1")
|
2007-08-11 03:57:14 -03:00
|
|
|
|
2008-02-04 18:07:15 -04:00
|
|
|
__iter__ = iterkeys
|
|
|
|
|
2007-08-11 03:57:14 -03:00
|
|
|
def keys(self):
|
|
|
|
return list(self.iterkeys())
|
|
|
|
|
|
|
|
def copy(self):
|
|
|
|
return byteskeydict(self.d)
|
|
|
|
|
2002-12-08 14:36:24 -04:00
|
|
|
|
|
|
|
class TestCase(unittest.TestCase):
|
|
|
|
|
2007-05-18 19:12:08 -03:00
|
|
|
fn = "shelftemp.db"
|
2002-12-24 14:31:27 -04:00
|
|
|
|
2007-05-18 18:57:09 -03:00
|
|
|
def tearDown(self):
|
|
|
|
for f in glob.glob(self.fn+"*"):
|
2008-05-20 18:35:26 -03:00
|
|
|
support.unlink(f)
|
2007-05-18 18:57:09 -03:00
|
|
|
|
Merged revisions 65209-65216,65225-65226,65233,65239,65246-65247,65255-65256 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r65209 | raymond.hettinger | 2008-07-23 19:08:18 -0500 (Wed, 23 Jul 2008) | 1 line
Finish-up the partial conversion from int to Py_ssize_t for deque indices and length.
........
r65210 | raymond.hettinger | 2008-07-23 19:53:49 -0500 (Wed, 23 Jul 2008) | 1 line
Parse to the correct datatype.
........
r65211 | benjamin.peterson | 2008-07-23 21:27:46 -0500 (Wed, 23 Jul 2008) | 1 line
fix spacing
........
r65212 | benjamin.peterson | 2008-07-23 21:31:28 -0500 (Wed, 23 Jul 2008) | 1 line
fix markup
........
r65213 | benjamin.peterson | 2008-07-23 21:45:37 -0500 (Wed, 23 Jul 2008) | 1 line
add some documentation for 2to3
........
r65214 | raymond.hettinger | 2008-07-24 00:38:48 -0500 (Thu, 24 Jul 2008) | 1 line
Finish conversion from int to Py_ssize_t.
........
r65215 | raymond.hettinger | 2008-07-24 02:04:55 -0500 (Thu, 24 Jul 2008) | 1 line
Convert from long to Py_ssize_t.
........
r65216 | georg.brandl | 2008-07-24 02:09:21 -0500 (Thu, 24 Jul 2008) | 2 lines
Fix indentation.
........
r65225 | benjamin.peterson | 2008-07-25 11:55:37 -0500 (Fri, 25 Jul 2008) | 1 line
teach .bzrignore about doc tools
........
r65226 | benjamin.peterson | 2008-07-25 12:02:11 -0500 (Fri, 25 Jul 2008) | 1 line
document default value for fillvalue
........
r65233 | raymond.hettinger | 2008-07-25 13:43:33 -0500 (Fri, 25 Jul 2008) | 1 line
Issue 1592: Better error reporting for operations on closed shelves.
........
r65239 | benjamin.peterson | 2008-07-25 16:59:53 -0500 (Fri, 25 Jul 2008) | 1 line
fix indentation
........
r65246 | andrew.kuchling | 2008-07-26 08:08:19 -0500 (Sat, 26 Jul 2008) | 1 line
This sentence continues to bug me; rewrite it for the second time
........
r65247 | andrew.kuchling | 2008-07-26 08:09:06 -0500 (Sat, 26 Jul 2008) | 1 line
Remove extra words
........
r65255 | skip.montanaro | 2008-07-26 19:49:02 -0500 (Sat, 26 Jul 2008) | 3 lines
Close issue 3437 - missing state change when Allow lines are processed.
Adds test cases which use Allow: as well.
........
r65256 | skip.montanaro | 2008-07-26 19:50:41 -0500 (Sat, 26 Jul 2008) | 2 lines
note robotparser bug fix.
........
2008-07-31 13:23:04 -03:00
|
|
|
def test_close(self):
|
|
|
|
d1 = {}
|
|
|
|
s = shelve.Shelf(d1, protocol=2, writeback=False)
|
|
|
|
s['key1'] = [1,2,3,4]
|
|
|
|
self.assertEqual(s['key1'], [1,2,3,4])
|
|
|
|
self.assertEqual(len(s), 1)
|
|
|
|
s.close()
|
|
|
|
self.assertRaises(ValueError, len, s)
|
|
|
|
try:
|
|
|
|
s['key1']
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.fail('Closed shelf should not find a key')
|
|
|
|
|
2002-12-08 14:36:24 -04:00
|
|
|
def test_ascii_file_shelf(self):
|
2007-05-18 18:57:09 -03:00
|
|
|
s = shelve.open(self.fn, protocol=0)
|
2002-12-08 14:36:24 -04:00
|
|
|
try:
|
|
|
|
s['key1'] = (1,2,3,4)
|
|
|
|
self.assertEqual(s['key1'], (1,2,3,4))
|
|
|
|
finally:
|
2007-05-18 18:57:09 -03:00
|
|
|
s.close()
|
2002-12-08 14:36:24 -04:00
|
|
|
|
|
|
|
def test_binary_file_shelf(self):
|
2007-05-18 18:57:09 -03:00
|
|
|
s = shelve.open(self.fn, protocol=1)
|
2002-12-08 14:36:24 -04:00
|
|
|
try:
|
|
|
|
s['key1'] = (1,2,3,4)
|
|
|
|
self.assertEqual(s['key1'], (1,2,3,4))
|
|
|
|
finally:
|
2007-05-18 18:57:09 -03:00
|
|
|
s.close()
|
2002-12-08 14:36:24 -04:00
|
|
|
|
2003-04-19 17:59:03 -03:00
|
|
|
def test_proto2_file_shelf(self):
|
2007-05-18 18:57:09 -03:00
|
|
|
s = shelve.open(self.fn, protocol=2)
|
2003-04-19 17:59:03 -03:00
|
|
|
try:
|
|
|
|
s['key1'] = (1,2,3,4)
|
|
|
|
self.assertEqual(s['key1'], (1,2,3,4))
|
|
|
|
finally:
|
2007-05-18 18:57:09 -03:00
|
|
|
s.close()
|
2003-04-19 17:59:03 -03:00
|
|
|
|
2002-12-08 14:36:24 -04:00
|
|
|
def test_in_memory_shelf(self):
|
2007-08-11 03:57:14 -03:00
|
|
|
d1 = byteskeydict()
|
2004-12-04 23:58:17 -04:00
|
|
|
s = shelve.Shelf(d1, protocol=0)
|
2002-12-08 14:36:24 -04:00
|
|
|
s['key1'] = (1,2,3,4)
|
|
|
|
self.assertEqual(s['key1'], (1,2,3,4))
|
|
|
|
s.close()
|
2007-08-11 03:57:14 -03:00
|
|
|
d2 = byteskeydict()
|
2004-12-04 23:58:17 -04:00
|
|
|
s = shelve.Shelf(d2, protocol=1)
|
2002-12-08 14:36:24 -04:00
|
|
|
s['key1'] = (1,2,3,4)
|
|
|
|
self.assertEqual(s['key1'], (1,2,3,4))
|
|
|
|
s.close()
|
|
|
|
|
|
|
|
self.assertEqual(len(d1), 1)
|
2007-08-28 23:57:31 -03:00
|
|
|
self.assertEqual(len(d2), 1)
|
|
|
|
self.assertNotEqual(d1.items(), d2.items())
|
2002-12-08 14:36:24 -04:00
|
|
|
|
2003-04-19 17:59:03 -03:00
|
|
|
def test_mutable_entry(self):
|
2007-08-11 03:57:14 -03:00
|
|
|
d1 = byteskeydict()
|
2003-04-19 17:59:03 -03:00
|
|
|
s = shelve.Shelf(d1, protocol=2, writeback=False)
|
|
|
|
s['key1'] = [1,2,3,4]
|
|
|
|
self.assertEqual(s['key1'], [1,2,3,4])
|
|
|
|
s['key1'].append(5)
|
|
|
|
self.assertEqual(s['key1'], [1,2,3,4])
|
|
|
|
s.close()
|
|
|
|
|
2007-08-11 03:57:14 -03:00
|
|
|
d2 = byteskeydict()
|
2003-04-19 17:59:03 -03:00
|
|
|
s = shelve.Shelf(d2, protocol=2, writeback=True)
|
|
|
|
s['key1'] = [1,2,3,4]
|
|
|
|
self.assertEqual(s['key1'], [1,2,3,4])
|
|
|
|
s['key1'].append(5)
|
|
|
|
self.assertEqual(s['key1'], [1,2,3,4,5])
|
|
|
|
s.close()
|
|
|
|
|
|
|
|
self.assertEqual(len(d1), 1)
|
|
|
|
self.assertEqual(len(d2), 1)
|
|
|
|
|
|
|
|
|
2004-05-31 13:29:04 -03:00
|
|
|
from test import mapping_tests
|
2002-12-08 14:36:24 -04:00
|
|
|
|
2004-05-31 13:29:04 -03:00
|
|
|
class TestShelveBase(mapping_tests.BasicTestMappingProtocol):
|
2003-03-09 03:05:43 -04:00
|
|
|
fn = "shelftemp.db"
|
|
|
|
counter = 0
|
|
|
|
def __init__(self, *args, **kw):
|
|
|
|
self._db = []
|
2004-05-31 13:29:04 -03:00
|
|
|
mapping_tests.BasicTestMappingProtocol.__init__(self, *args, **kw)
|
2004-06-02 15:42:25 -03:00
|
|
|
type2test = shelve.Shelf
|
2003-03-09 03:05:43 -04:00
|
|
|
def _reference(self):
|
|
|
|
return {"key1":"value1", "key2":2, "key3":(1,2,3)}
|
|
|
|
def _empty_mapping(self):
|
|
|
|
if self._in_mem:
|
2007-08-11 03:57:14 -03:00
|
|
|
x= shelve.Shelf(byteskeydict(), **self._args)
|
2003-03-09 03:05:43 -04:00
|
|
|
else:
|
|
|
|
self.counter+=1
|
2003-04-19 17:59:03 -03:00
|
|
|
x= shelve.open(self.fn+str(self.counter), **self._args)
|
2003-03-09 03:05:43 -04:00
|
|
|
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+"*"):
|
2008-05-20 18:35:26 -03:00
|
|
|
support.unlink(f)
|
2003-03-09 03:05:43 -04:00
|
|
|
|
|
|
|
class TestAsciiFileShelve(TestShelveBase):
|
2004-12-04 23:58:17 -04:00
|
|
|
_args={'protocol':0}
|
2003-03-09 03:05:43 -04:00
|
|
|
_in_mem = False
|
|
|
|
class TestBinaryFileShelve(TestShelveBase):
|
2004-12-04 23:58:17 -04:00
|
|
|
_args={'protocol':1}
|
2003-04-19 17:59:03 -03:00
|
|
|
_in_mem = False
|
|
|
|
class TestProto2FileShelve(TestShelveBase):
|
|
|
|
_args={'protocol':2}
|
2003-03-09 03:05:43 -04:00
|
|
|
_in_mem = False
|
|
|
|
class TestAsciiMemShelve(TestShelveBase):
|
2004-12-04 23:58:17 -04:00
|
|
|
_args={'protocol':0}
|
2003-03-09 03:05:43 -04:00
|
|
|
_in_mem = True
|
|
|
|
class TestBinaryMemShelve(TestShelveBase):
|
2004-12-04 23:58:17 -04:00
|
|
|
_args={'protocol':1}
|
2003-04-19 17:59:03 -03:00
|
|
|
_in_mem = True
|
|
|
|
class TestProto2MemShelve(TestShelveBase):
|
|
|
|
_args={'protocol':2}
|
2003-03-09 03:05:43 -04:00
|
|
|
_in_mem = True
|
|
|
|
|
|
|
|
def test_main():
|
2007-08-24 18:59:45 -03:00
|
|
|
for module in dbm_iterator():
|
2008-05-20 18:35:26 -03:00
|
|
|
support.run_unittest(
|
2007-08-24 18:59:45 -03:00
|
|
|
TestAsciiFileShelve,
|
|
|
|
TestBinaryFileShelve,
|
|
|
|
TestProto2FileShelve,
|
|
|
|
TestAsciiMemShelve,
|
|
|
|
TestBinaryMemShelve,
|
|
|
|
TestProto2MemShelve,
|
|
|
|
TestCase
|
|
|
|
)
|
2002-12-08 14:36:24 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|