Skip to content

Commit e736349

Browse files
committed
[LNT] Fix some ResourceWarnings
Differential Revision: https://reviews.llvm.org/D94767
1 parent 897087a commit e736349

File tree

7 files changed

+18
-9
lines changed

7 files changed

+18
-9
lines changed

lnt/server/db/rules/rule_update_profile_stats.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ def update_profile_stats(session, ts, run_id):
2020
return
2121

2222
try:
23-
history = json.loads(open(history_path).read())
23+
with open(history_path) as f:
24+
history = json.loads(f.read())
2425
except Exception:
2526
history = []
2627
age = []
@@ -38,8 +39,10 @@ def update_profile_stats(session, ts, run_id):
3839
sz = os.stat(f).st_size // 1000
3940
age.append([mtime, sz])
4041

41-
open(history_path, 'w').write(json.dumps(history))
42-
open(age_path, 'w').write(json.dumps(age))
42+
with open(history_path, 'w') as history_f:
43+
history_f.write(json.dumps(history))
44+
with open(age_path, 'w') as age_f:
45+
age_f.write(json.dumps(age))
4346

4447

4548
post_submission_hook = update_profile_stats

lnt/server/instance.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ def frompath(path):
5454
raise Exception("Invalid config: %r" % config_path)
5555

5656
config_data = {}
57-
exec(open(config_path).read(), config_data)
57+
with open(config_path) as f:
58+
exec(f.read(), config_data)
5859
config = lnt.server.config.Config.from_data(config_path, config_data)
5960

6061
return Instance(config_path, config, tmpdir)

lnt/testing/profile/perf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ def __init__(self):
1818

1919
@staticmethod
2020
def checkFile(fn):
21-
return open(fn, 'rb').read(8) == b'PERFILE2'
21+
with open(fn, 'rb') as f:
22+
return f.read(8) == b'PERFILE2'
2223

2324
@staticmethod
2425
def deserialize(f, nm='nm', objdump='objdump', propagateExceptions=False):

lnt/testing/profile/profilev1impl.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ def upgrade(old):
4545
@staticmethod
4646
def checkFile(fn):
4747
# "zlib compressed data" - 78 9C
48-
return open(fn, 'rb').read(2) == b'\x78\x9c'
48+
with open(fn, 'rb') as f:
49+
return f.read(2) == b'\x78\x9c'
4950

5051
@staticmethod
5152
def deserialize(fobj):

lnt/testing/profile/profilev2impl.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,8 @@ class ProfileV2(ProfileImpl):
560560
def checkFile(fn):
561561
# The first number is the version (2); ULEB encoded this is simply
562562
# 0x02.
563-
return open(fn, 'rb').read(1) == b'\x02'
563+
with open(fn, 'rb') as f:
564+
return f.read(1) == b'\x02'
564565

565566
@staticmethod
566567
def deserialize(fobj):

lnt/testing/util/compilers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ def get_cc_info(path, cc_flags=[]):
186186
(cc, cc_target_assembly))
187187

188188
cc_exec_hash = hashlib.sha1()
189-
cc_exec_hash.update(open(cc, 'rb').read())
189+
with open(cc, 'rb') as f:
190+
cc_exec_hash.update(f.read())
190191

191192
info = {
192193
'cc_build': cc_build,

lnt/tests/test_suite.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,8 @@ def _lit(self, path, test, profile):
639639
# failures!
640640
pass
641641
try:
642-
return json.loads(open(output_json_path.name).read())
642+
with open(output_json_path.name) as f:
643+
return json.loads(f.read())
643644
except ValueError as e:
644645
fatal("Running test-suite did not create valid json report "
645646
"in {}: {}".format(output_json_path.name, e))

0 commit comments

Comments
 (0)