Skip to content

Commit 549e3d6

Browse files
MAINT: apply ruff/flake8-simplify rule SIM115
SIM115 Use context handler for opening files
1 parent 0bf9c46 commit 549e3d6

File tree

1 file changed

+31
-34
lines changed

1 file changed

+31
-34
lines changed

tools/c_coverage/c_coverage_report.py

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,23 @@ def mark_line(self, lineno, as_func=None):
5454
line.add(as_func)
5555

5656
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)
6564

6665
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))
7674

7775

7876
class SourceFiles:
@@ -95,24 +93,24 @@ def clean_path(self, path):
9593

9694
def write_text(self, root):
9795
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)
10198

10299
def write_html(self, root):
103100
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)
107105

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>")
116114

117115

118116
def collect_stats(files, fd, pattern):
@@ -164,9 +162,8 @@ def collect_stats(files, fd, pattern):
164162

165163
files = SourceFiles()
166164
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)
170167

171168
if not os.path.exists(args.directory):
172169
os.makedirs(args.directory)

0 commit comments

Comments
 (0)