File tree 5 files changed +34
-30
lines changed
5 files changed +34
-30
lines changed Original file line number Diff line number Diff line change @@ -678,8 +678,10 @@ def do_GET(self):
678
678
"""Serve a GET request."""
679
679
f = self .send_head ()
680
680
if f :
681
- self .copyfile (f , self .wfile )
682
- f .close ()
681
+ try :
682
+ self .copyfile (f , self .wfile )
683
+ finally :
684
+ f .close ()
683
685
684
686
def do_HEAD (self ):
685
687
"""Serve a HEAD request."""
@@ -720,13 +722,17 @@ def send_head(self):
720
722
except OSError :
721
723
self .send_error (404 , "File not found" )
722
724
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
730
736
731
737
def list_directory (self , path ):
732
738
"""Helper to produce a directory listing (absent index.html).
Original file line number Diff line number Diff line change 7
7
#-------------------------#
8
8
9
9
def 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
21
11
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 )
22
20
for tf in tests :
23
21
res = tf (h , f )
24
22
if res :
Original file line number Diff line number Diff line change @@ -22,8 +22,8 @@ def getcaps():
22
22
fp = open (mailcap , 'r' )
23
23
except OSError :
24
24
continue
25
- morecaps = readmailcapfile ( fp )
26
- fp . close ( )
25
+ with fp :
26
+ morecaps = readmailcapfile ( fp )
27
27
for key , value in morecaps .items ():
28
28
if not key in caps :
29
29
caps [key ] = value
Original file line number Diff line number Diff line change @@ -363,9 +363,10 @@ def read_mime_types(file):
363
363
f = open (file )
364
364
except OSError :
365
365
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 ]
369
370
370
371
371
372
def _default_mime_types ():
Original file line number Diff line number Diff line change @@ -76,14 +76,13 @@ class FatalIncludeError(SyntaxError):
76
76
77
77
def default_loader (href , parse , encoding = None ):
78
78
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 ()
81
81
else :
82
82
if not encoding :
83
83
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 ()
87
86
return data
88
87
89
88
##
You can’t perform that action at this time.
0 commit comments