File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -678,8 +678,10 @@ def do_GET(self):
678678 """Serve a GET request."""
679679 f = self .send_head ()
680680 if f :
681- self .copyfile (f , self .wfile )
682- f .close ()
681+ try :
682+ self .copyfile (f , self .wfile )
683+ finally :
684+ f .close ()
683685
684686 def do_HEAD (self ):
685687 """Serve a HEAD request."""
@@ -720,13 +722,17 @@ def send_head(self):
720722 except OSError :
721723 self .send_error (404 , "File not found" )
722724 return None
723- self .send_response (200 )
724- self .send_header ("Content-type" , ctype )
725- fs = os .fstat (f .fileno ())
726- self .send_header ("Content-Length" , str (fs [6 ]))
727- self .send_header ("Last-Modified" , self .date_time_string (fs .st_mtime ))
728- self .end_headers ()
729- return f
725+ try :
726+ self .send_response (200 )
727+ self .send_header ("Content-type" , ctype )
728+ fs = os .fstat (f .fileno ())
729+ self .send_header ("Content-Length" , str (fs [6 ]))
730+ self .send_header ("Last-Modified" , self .date_time_string (fs .st_mtime ))
731+ self .end_headers ()
732+ return f
733+ except :
734+ f .close ()
735+ raise
730736
731737 def list_directory (self , path ):
732738 """Helper to produce a directory listing (absent index.html).
Original file line number Diff line number Diff line change 77#-------------------------#
88
99def what (file , h = None ):
10- if h is None :
11- if isinstance (file , str ):
12- f = open (file , 'rb' )
13- h = f .read (32 )
14- else :
15- location = file .tell ()
16- h = file .read (32 )
17- file .seek (location )
18- f = None
19- else :
20- f = None
10+ f = None
2111 try :
12+ if h is None :
13+ if isinstance (file , str ):
14+ f = open (file , 'rb' )
15+ h = f .read (32 )
16+ else :
17+ location = file .tell ()
18+ h = file .read (32 )
19+ file .seek (location )
2220 for tf in tests :
2321 res = tf (h , f )
2422 if res :
Original file line number Diff line number Diff line change @@ -22,8 +22,8 @@ def getcaps():
2222 fp = open (mailcap , 'r' )
2323 except OSError :
2424 continue
25- morecaps = readmailcapfile ( fp )
26- fp . close ( )
25+ with fp :
26+ morecaps = readmailcapfile ( fp )
2727 for key , value in morecaps .items ():
2828 if not key in caps :
2929 caps [key ] = value
Original file line number Diff line number Diff line change @@ -363,9 +363,10 @@ def read_mime_types(file):
363363 f = open (file )
364364 except OSError :
365365 return None
366- db = MimeTypes ()
367- db .readfp (f , True )
368- return db .types_map [True ]
366+ with f :
367+ db = MimeTypes ()
368+ db .readfp (f , True )
369+ return db .types_map [True ]
369370
370371
371372def _default_mime_types ():
Original file line number Diff line number Diff line change @@ -76,14 +76,13 @@ class FatalIncludeError(SyntaxError):
7676
7777def default_loader (href , parse , encoding = None ):
7878 if parse == "xml" :
79- file = open (href , 'rb' )
80- data = ElementTree .parse (file ).getroot ()
79+ with open (href , 'rb' ) as file :
80+ data = ElementTree .parse (file ).getroot ()
8181 else :
8282 if not encoding :
8383 encoding = 'UTF-8'
84- file = open (href , 'r' , encoding = encoding )
85- data = file .read ()
86- file .close ()
84+ with open (href , 'r' , encoding = encoding ) as file :
85+ data = file .read ()
8786 return data
8887
8988##
You can’t perform that action at this time.
0 commit comments