File tree Expand file tree Collapse file tree 5 files changed +34
-30
lines changed Expand file tree Collapse file tree 5 files changed +34
-30
lines changed Original file line number Diff line number Diff line change @@ -670,8 +670,10 @@ def do_GET(self):
670
670
"""Serve a GET request."""
671
671
f = self .send_head ()
672
672
if f :
673
- self .copyfile (f , self .wfile )
674
- f .close ()
673
+ try :
674
+ self .copyfile (f , self .wfile )
675
+ finally :
676
+ f .close ()
675
677
676
678
def do_HEAD (self ):
677
679
"""Serve a HEAD request."""
@@ -712,13 +714,17 @@ def send_head(self):
712
714
except IOError :
713
715
self .send_error (404 , "File not found" )
714
716
return None
715
- self .send_response (200 )
716
- self .send_header ("Content-type" , ctype )
717
- fs = os .fstat (f .fileno ())
718
- self .send_header ("Content-Length" , str (fs [6 ]))
719
- self .send_header ("Last-Modified" , self .date_time_string (fs .st_mtime ))
720
- self .end_headers ()
721
- return f
717
+ try :
718
+ self .send_response (200 )
719
+ self .send_header ("Content-type" , ctype )
720
+ fs = os .fstat (f .fileno ())
721
+ self .send_header ("Content-Length" , str (fs [6 ]))
722
+ self .send_header ("Last-Modified" , self .date_time_string (fs .st_mtime ))
723
+ self .end_headers ()
724
+ return f
725
+ except :
726
+ f .close ()
727
+ raise
722
728
723
729
def list_directory (self , path ):
724
730
"""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 IOError :
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 IOError :
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