@@ -1761,11 +1761,8 @@ def load(self, filename=None, ignore_discard=False, ignore_expires=False):
17611761 if self .filename is not None : filename = self .filename
17621762 else : raise ValueError (MISSING_FILENAME_TEXT )
17631763
1764- f = open (filename )
1765- try :
1764+ with open (filename ) as f :
17661765 self ._really_load (f , filename , ignore_discard , ignore_expires )
1767- finally :
1768- f .close ()
17691766
17701767 def revert (self , filename = None ,
17711768 ignore_discard = False , ignore_expires = False ):
@@ -1856,15 +1853,12 @@ def save(self, filename=None, ignore_discard=False, ignore_expires=False):
18561853 if self .filename is not None : filename = self .filename
18571854 else : raise ValueError (MISSING_FILENAME_TEXT )
18581855
1859- f = open (filename , "w" )
1860- try :
1856+ with open (filename , "w" ) as f :
18611857 # There really isn't an LWP Cookies 2.0 format, but this indicates
18621858 # that there is extra information in here (domain_dot and
18631859 # port_spec) while still being compatible with libwww-perl, I hope.
18641860 f .write ("#LWP-Cookies-2.0\n " )
18651861 f .write (self .as_lwp_str (ignore_discard , ignore_expires ))
1866- finally :
1867- f .close ()
18681862
18691863 def _really_load (self , f , filename , ignore_discard , ignore_expires ):
18701864 magic = f .readline ()
@@ -2055,8 +2049,7 @@ def save(self, filename=None, ignore_discard=False, ignore_expires=False):
20552049 if self .filename is not None : filename = self .filename
20562050 else : raise ValueError (MISSING_FILENAME_TEXT )
20572051
2058- f = open (filename , "w" )
2059- try :
2052+ with open (filename , "w" ) as f :
20602053 f .write (self .header )
20612054 now = time .time ()
20622055 for cookie in self :
@@ -2085,5 +2078,3 @@ def save(self, filename=None, ignore_discard=False, ignore_expires=False):
20852078 "\t " .join ([cookie .domain , initial_dot , cookie .path ,
20862079 secure , expires , name , value ])+
20872080 "\n " )
2088- finally :
2089- f .close ()
0 commit comments