@@ -54,25 +54,23 @@ def mark_line(self, lineno, as_func=None):
54
54
line .add (as_func )
55
55
56
56
def write_text (self , fd ):
57
- source = open (self .path , "r" )
58
- for i , line in enumerate (source ):
59
- if i + 1 in self .lines :
60
- fd .write ("> " )
61
- else :
62
- fd .write ("! " )
63
- fd .write (line )
64
- source .close ()
57
+ with open (self .path , "r" ) as source :
58
+ for i , line in enumerate (source ):
59
+ if i + 1 in self .lines :
60
+ fd .write ("> " )
61
+ else :
62
+ fd .write ("! " )
63
+ fd .write (line )
65
64
66
65
def write_html (self , fd ):
67
- source = open (self .path , 'r' )
68
- code = source .read ()
69
- lexer = CLexer ()
70
- formatter = FunctionHtmlFormatter (
71
- self .lines ,
72
- full = True ,
73
- linenos = 'inline' )
74
- fd .write (highlight (code , lexer , formatter ))
75
- source .close ()
66
+ with open (self .path , 'r' ) as source :
67
+ code = source .read ()
68
+ lexer = CLexer ()
69
+ formatter = FunctionHtmlFormatter (
70
+ self .lines ,
71
+ full = True ,
72
+ linenos = 'inline' )
73
+ fd .write (highlight (code , lexer , formatter ))
76
74
77
75
78
76
class SourceFiles :
@@ -95,24 +93,24 @@ def clean_path(self, path):
95
93
96
94
def write_text (self , root ):
97
95
for path , source in self .files .items ():
98
- fd = open (os .path .join (root , self .clean_path (path )), "w" )
99
- source .write_text (fd )
100
- fd .close ()
96
+ with open (os .path .join (root , self .clean_path (path )), "w" ) as fd :
97
+ source .write_text (fd )
101
98
102
99
def write_html (self , root ):
103
100
for path , source in self .files .items ():
104
- fd = open (os .path .join (root , self .clean_path (path ) + ".html" ), "w" )
105
- source .write_html (fd )
106
- fd .close ()
101
+ with open (
102
+ os .path .join (root , self .clean_path (path ) + ".html" ), "w"
103
+ ) as fd :
104
+ source .write_html (fd )
107
105
108
- fd = open (os .path .join (root , 'index.html' ), 'w' )
109
- fd .write ("<html>" )
110
- paths = sorted (self .files .keys ())
111
- for path in paths :
112
- fd .write ('<p><a href="%s.html">%s</a></p>' %
113
- (self .clean_path (path ), escape ( path [ len ( self . prefix ):])))
114
- fd . write ( "</html>" )
115
- fd .close ( )
106
+ with open (os .path .join (root , 'index.html' ), 'w' ) as fd :
107
+ fd .write ("<html>" )
108
+ paths = sorted (self .files .keys ())
109
+ for path in paths :
110
+ fd .write ('<p><a href="%s.html">%s</a></p>' %
111
+ (self .clean_path (path ),
112
+ escape ( path [ len ( self . prefix ):])) )
113
+ fd .write ( "</html>" )
116
114
117
115
118
116
def collect_stats (files , fd , pattern ):
@@ -164,9 +162,8 @@ def collect_stats(files, fd, pattern):
164
162
165
163
files = SourceFiles ()
166
164
for log_file in args .callgrind_file :
167
- log_fd = open (log_file , 'r' )
168
- collect_stats (files , log_fd , args .pattern )
169
- log_fd .close ()
165
+ with open (log_file , 'r' ) as log_fd :
166
+ collect_stats (files , log_fd , args .pattern )
170
167
171
168
if not os .path .exists (args .directory ):
172
169
os .makedirs (args .directory )
0 commit comments