<s> import os <EOL> import unittest <EOL> import shelve <EOL> import glob <EOL> from test import test_support <EOL> test_support . import_module ( '<STR_LIT>' , deprecated = True ) <EOL> class TestCase ( unittest . TestCase ) : <EOL> fn = "<STR_LIT>" + os . extsep + "<STR_LIT>" <EOL> def test_close ( self ) : <EOL> d1 = { } <EOL> s = shelve . Shelf ( d1 , protocol = <NUM_LIT:2> , writeback = False ) <EOL> s [ '<STR_LIT>' ] = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] <EOL> self . assertEqual ( s [ '<STR_LIT>' ] , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> self . assertEqual ( len ( s ) , <NUM_LIT:1> ) <EOL> s . close ( ) <EOL> self . assertRaises ( ValueError , len , s ) <EOL> try : <EOL> s [ '<STR_LIT>' ] <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> def test_ascii_file_shelf ( self ) : <EOL> try : <EOL> s = shelve . open ( self . fn , protocol = <NUM_LIT:0> ) <EOL> s [ '<STR_LIT>' ] = ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> self . assertEqual ( s [ '<STR_LIT>' ] , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) <EOL> s . close ( ) <EOL> finally : <EOL> for f in glob . glob ( self . fn + "<STR_LIT:*>" ) : <EOL> os . unlink ( f ) <EOL> def test_binary_file_shelf ( self ) : <EOL> try : <EOL> s = shelve . open ( self . fn , protocol = <NUM_LIT:1> ) <EOL> s [ '<STR_LIT>' ] = ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> self . assertEqual ( s [ '<STR_LIT>' ] , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) <EOL> s . close ( ) <EOL> finally : <EOL> for f in glob . glob ( self . fn + "<STR_LIT:*>" ) : <EOL> os . unlink ( f ) <EOL> def test_proto2_file_shelf ( self ) : <EOL> try : <EOL> s = shelve . open ( self . fn , protocol = <NUM_LIT:2> ) <EOL> s [ '<STR_LIT>' ] = ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> self . assertEqual ( s [ '<STR_LIT>' ] , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) <EOL> s . close ( ) <EOL> finally : <EOL> for f in glob . glob ( self . fn + "<STR_LIT:*>" ) : <EOL> os . unlink ( f ) <EOL> def test_in_memory_shelf ( self ) : <EOL> d1 = { } <EOL> s = shelve . Shelf ( d1 , protocol = <NUM_LIT:0> ) <EOL> s [ '<STR_LIT>' ] = ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> self . assertEqual ( s [ '<STR_LIT>' ] , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) <EOL> s . close ( ) <EOL> d2 = { } <EOL> s = shelve . Shelf ( d2 , protocol = <NUM_LIT:1> ) <EOL> s [ '<STR_LIT>' ] = ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> self . assertEqual ( s [ '<STR_LIT>' ] , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) <EOL> s . close ( ) <EOL> self . assertEqual ( len ( d1 ) , <NUM_LIT:1> ) <EOL> self . assertNotEqual ( d1 , d2 ) <EOL> def test_mutable_entry ( self ) : <EOL> d1 = { } <EOL> s = shelve . Shelf ( d1 , protocol = <NUM_LIT:2> , writeback = False ) <EOL> s [ '<STR_LIT>' ] = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] <EOL> self . assertEqual ( s [ '<STR_LIT>' ] , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> s [ '<STR_LIT>' ] . append ( <NUM_LIT:5> ) <EOL> self . assertEqual ( s [ '<STR_LIT>' ] , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> s . close ( ) <EOL> d2 = { } <EOL> s = shelve . Shelf ( d2 , protocol = <NUM_LIT:2> , writeback = True ) <EOL> s [ '<STR_LIT>' ] = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] <EOL> self . assertEqual ( s [ '<STR_LIT>' ] , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> s [ '<STR_LIT>' ] . append ( <NUM_LIT:5> ) <EOL> self . assertEqual ( s [ '<STR_LIT>' ] , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] ) <EOL> s . close ( ) <EOL> self . assertEqual ( len ( d1 ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( d2 ) , <NUM_LIT:1> ) <EOL> def test_writeback_also_writes_immediately ( self ) : <EOL> d = { } <EOL> s = shelve . Shelf ( d , writeback = True ) <EOL> s [ '<STR_LIT:key>' ] = [ <NUM_LIT:1> ] <EOL> p1 = d [ '<STR_LIT:key>' ] <EOL> s [ '<STR_LIT:key>' ] . append ( <NUM_LIT:2> ) <EOL> s . close ( ) <EOL> p2 = d [ '<STR_LIT:key>' ] <EOL> self . assertNotEqual ( p1 , p2 ) <EOL> from test import mapping_tests <EOL> class TestShelveBase ( mapping_tests . BasicTestMappingProtocol ) : <EOL> fn = "<STR_LIT>" <EOL> counter = <NUM_LIT:0> <EOL> def __init__ ( self , * args , ** kw ) : <EOL> self . _db = [ ] <EOL> mapping_tests . BasicTestMappingProtocol . __init__ ( self , * args , ** kw ) <EOL> type2test = shelve . Shelf <EOL> def _reference ( self ) : <EOL> return { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : <NUM_LIT:2> , "<STR_LIT>" : ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) } <EOL> def _empty_mapping ( self ) : <EOL> if self . _in_mem : <EOL> x = shelve . Shelf ( { } , ** self . _args ) <EOL> else : <EOL> self . counter += <NUM_LIT:1> <EOL> x = shelve . open ( self . fn + str ( self . counter ) , ** self . _args ) <EOL> self . _db . append ( x ) <EOL> return x <EOL> def tearDown ( self ) : <EOL> for db in self . _db : <EOL> db . close ( ) <EOL> self . _db = [ ] <EOL> if not self . _in_mem : <EOL> for f in glob . glob ( self . fn + "<STR_LIT:*>" ) : <EOL> test_support . unlink ( f ) <EOL> class TestAsciiFileShelve ( TestShelveBase ) : <EOL> _args = { '<STR_LIT>' : <NUM_LIT:0> } <EOL> _in_mem = False <EOL> class TestBinaryFileShelve ( TestShelveBase ) : <EOL> _args = { '<STR_LIT>' : <NUM_LIT:1> } <EOL> _in_mem = False <EOL> class TestProto2FileShelve ( TestShelveBase ) : <EOL> _args = { '<STR_LIT>' : <NUM_LIT:2> } <EOL> _in_mem = False <EOL> class TestAsciiMemShelve ( TestShelveBase ) : <EOL> _args = { '<STR_LIT>' : <NUM_LIT:0> } <EOL> _in_mem = True <EOL> class TestBinaryMemShelve ( TestShelveBase ) : <EOL> _args = { '<STR_LIT>' : <NUM_LIT:1> } <EOL> _in_mem = True <EOL> class TestProto2MemShelve ( TestShelveBase ) : <EOL> _args = { '<STR_LIT>' : <NUM_LIT:2> } <EOL> _in_mem = True <EOL> def test_main ( ) : <EOL> test_support . run_unittest ( <EOL> TestAsciiFileShelve , <EOL> TestBinaryFileShelve , <EOL> TestProto2FileShelve , <EOL> TestAsciiMemShelve , <EOL> TestBinaryMemShelve , <EOL> TestProto2MemShelve , <EOL> TestCase <EOL> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_main ( ) </s>
