22 Original by Roger E. Masse
33"""
44
5+ import contextlib
56import io
67import operator
78import os
@@ -32,12 +33,11 @@ class DumbDBMTestCase(unittest.TestCase):
3233 }
3334
3435 def test_dumbdbm_creation (self ):
35- f = dumbdbm .open (_fname , 'c' )
36- self .assertEqual (list (f .keys ()), [])
37- for key in self ._dict :
38- f [key ] = self ._dict [key ]
39- self .read_helper (f )
40- f .close ()
36+ with contextlib .closing (dumbdbm .open (_fname , 'c' )) as f :
37+ self .assertEqual (list (f .keys ()), [])
38+ for key in self ._dict :
39+ f [key ] = self ._dict [key ]
40+ self .read_helper (f )
4141
4242 @unittest .skipUnless (hasattr (os , 'umask' ), 'test needs os.umask()' )
4343 def test_dumbdbm_creation_mode (self ):
@@ -69,78 +69,70 @@ def test_close_twice(self):
6969
7070 def test_dumbdbm_modification (self ):
7171 self .init_db ()
72- f = dumbdbm .open (_fname , 'w' )
73- self ._dict [b'g' ] = f [b'g' ] = b"indented"
74- self .read_helper (f )
75- # setdefault() works as in the dict interface
76- self .assertEqual (f .setdefault (b'xxx' , b'foo' ), b'foo' )
77- self .assertEqual (f [b'xxx' ], b'foo' )
78- f .close ()
72+ with contextlib .closing (dumbdbm .open (_fname , 'w' )) as f :
73+ self ._dict [b'g' ] = f [b'g' ] = b"indented"
74+ self .read_helper (f )
75+ # setdefault() works as in the dict interface
76+ self .assertEqual (f .setdefault (b'xxx' , b'foo' ), b'foo' )
77+ self .assertEqual (f [b'xxx' ], b'foo' )
7978
8079 def test_dumbdbm_read (self ):
8180 self .init_db ()
82- f = dumbdbm .open (_fname , 'r' )
83- self .read_helper (f )
84- with self .assertRaisesRegex (dumbdbm .error ,
85- 'The database is opened for reading only' ):
86- f [b'g' ] = b'x'
87- with self .assertRaisesRegex (dumbdbm .error ,
88- 'The database is opened for reading only' ):
89- del f [b'a' ]
90- # get() works as in the dict interface
91- self .assertEqual (f .get (b'a' ), self ._dict [b'a' ])
92- self .assertEqual (f .get (b'xxx' , b'foo' ), b'foo' )
93- self .assertIsNone (f .get (b'xxx' ))
94- with self .assertRaises (KeyError ):
95- f [b'xxx' ]
96- f .close ()
81+ with contextlib .closing (dumbdbm .open (_fname , 'r' )) as f :
82+ self .read_helper (f )
83+ with self .assertRaisesRegex (dumbdbm .error ,
84+ 'The database is opened for reading only' ):
85+ f [b'g' ] = b'x'
86+ with self .assertRaisesRegex (dumbdbm .error ,
87+ 'The database is opened for reading only' ):
88+ del f [b'a' ]
89+ # get() works as in the dict interface
90+ self .assertEqual (f .get (b'a' ), self ._dict [b'a' ])
91+ self .assertEqual (f .get (b'xxx' , b'foo' ), b'foo' )
92+ self .assertIsNone (f .get (b'xxx' ))
93+ with self .assertRaises (KeyError ):
94+ f [b'xxx' ]
9795
9896 def test_dumbdbm_keys (self ):
9997 self .init_db ()
100- f = dumbdbm .open (_fname )
101- keys = self .keys_helper (f )
102- f .close ()
98+ with contextlib .closing (dumbdbm .open (_fname )) as f :
99+ keys = self .keys_helper (f )
103100
104101 def test_write_contains (self ):
105- f = dumbdbm .open (_fname )
106- f [b'1' ] = b'hello'
107- self .assertIn (b'1' , f )
108- f .close ()
102+ with contextlib .closing (dumbdbm .open (_fname )) as f :
103+ f [b'1' ] = b'hello'
104+ self .assertIn (b'1' , f )
109105
110106 def test_write_write_read (self ):
111107 # test for bug #482460
112- f = dumbdbm .open (_fname )
113- f [b'1' ] = b'hello'
114- f [b'1' ] = b'hello2'
115- f .close ()
116- f = dumbdbm .open (_fname )
117- self .assertEqual (f [b'1' ], b'hello2' )
118- f .close ()
108+ with contextlib .closing (dumbdbm .open (_fname )) as f :
109+ f [b'1' ] = b'hello'
110+ f [b'1' ] = b'hello2'
111+ with contextlib .closing (dumbdbm .open (_fname )) as f :
112+ self .assertEqual (f [b'1' ], b'hello2' )
119113
120114 def test_str_read (self ):
121115 self .init_db ()
122- f = dumbdbm .open (_fname , 'r' )
123- self .assertEqual (f ['\u00fc ' ], self ._dict ['\u00fc ' .encode ('utf-8' )])
116+ with contextlib . closing ( dumbdbm .open (_fname , 'r' )) as f :
117+ self .assertEqual (f ['\u00fc ' ], self ._dict ['\u00fc ' .encode ('utf-8' )])
124118
125119 def test_str_write_contains (self ):
126120 self .init_db ()
127- f = dumbdbm .open (_fname )
128- f ['\u00fc ' ] = b'!'
129- f ['1' ] = 'a'
130- f .close ()
131- f = dumbdbm .open (_fname , 'r' )
132- self .assertIn ('\u00fc ' , f )
133- self .assertEqual (f ['\u00fc ' .encode ('utf-8' )],
134- self ._dict ['\u00fc ' .encode ('utf-8' )])
135- self .assertEqual (f [b'1' ], b'a' )
121+ with contextlib .closing (dumbdbm .open (_fname )) as f :
122+ f ['\u00fc ' ] = b'!'
123+ f ['1' ] = 'a'
124+ with contextlib .closing (dumbdbm .open (_fname , 'r' )) as f :
125+ self .assertIn ('\u00fc ' , f )
126+ self .assertEqual (f ['\u00fc ' .encode ('utf-8' )],
127+ self ._dict ['\u00fc ' .encode ('utf-8' )])
128+ self .assertEqual (f [b'1' ], b'a' )
136129
137130 def test_line_endings (self ):
138131 # test for bug #1172763: dumbdbm would die if the line endings
139132 # weren't what was expected.
140- f = dumbdbm .open (_fname )
141- f [b'1' ] = b'hello'
142- f [b'2' ] = b'hello2'
143- f .close ()
133+ with contextlib .closing (dumbdbm .open (_fname )) as f :
134+ f [b'1' ] = b'hello'
135+ f [b'2' ] = b'hello2'
144136
145137 # Mangle the file by changing the line separator to Windows or Unix
146138 with io .open (_fname + '.dir' , 'rb' ) as file :
@@ -163,10 +155,9 @@ def read_helper(self, f):
163155 self .assertEqual (self ._dict [key ], f [key ])
164156
165157 def init_db (self ):
166- f = dumbdbm .open (_fname , 'n' )
167- for k in self ._dict :
168- f [k ] = self ._dict [k ]
169- f .close ()
158+ with contextlib .closing (dumbdbm .open (_fname , 'n' )) as f :
159+ for k in self ._dict :
160+ f [k ] = self ._dict [k ]
170161
171162 def keys_helper (self , f ):
172163 keys = sorted (f .keys ())
@@ -180,25 +171,23 @@ def test_random(self):
180171 import random
181172 d = {} # mirror the database
182173 for dummy in range (5 ):
183- f = dumbdbm .open (_fname )
184- for dummy in range (100 ):
185- k = random .choice ('abcdefghijklm' )
186- if random .random () < 0.2 :
187- if k in d :
188- del d [k ]
189- del f [k ]
190- else :
191- v = random .choice ((b'a' , b'b' , b'c' )) * random .randrange (10000 )
192- d [k ] = v
193- f [k ] = v
194- self .assertEqual (f [k ], v )
195- f .close ()
196-
197- f = dumbdbm .open (_fname )
198- expected = sorted ((k .encode ("latin-1" ), v ) for k , v in d .items ())
199- got = sorted (f .items ())
200- self .assertEqual (expected , got )
201- f .close ()
174+ with contextlib .closing (dumbdbm .open (_fname )) as f :
175+ for dummy in range (100 ):
176+ k = random .choice ('abcdefghijklm' )
177+ if random .random () < 0.2 :
178+ if k in d :
179+ del d [k ]
180+ del f [k ]
181+ else :
182+ v = random .choice ((b'a' , b'b' , b'c' )) * random .randrange (10000 )
183+ d [k ] = v
184+ f [k ] = v
185+ self .assertEqual (f [k ], v )
186+
187+ with contextlib .closing (dumbdbm .open (_fname )) as f :
188+ expected = sorted ((k .encode ("latin-1" ), v ) for k , v in d .items ())
189+ got = sorted (f .items ())
190+ self .assertEqual (expected , got )
202191
203192 def test_context_manager (self ):
204193 with dumbdbm .open (_fname , 'c' ) as db :
0 commit comments