Ensure files get closed when they go out of scope

This is automatic in CPython but not guaranteed by the language. Be friendly
to other Python implementations.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine 2022-03-04 20:02:00 +01:00
parent 1177f37648
commit aeb8d66525
4 changed files with 56 additions and 48 deletions

View file

@ -249,7 +249,8 @@ class AbiChecker:
at_paragraph_start = True
description = None
full_path = os.path.join(directory, filename)
for line_number, line in enumerate(open(full_path), 1):
with open(full_path) as fd:
for line_number, line in enumerate(fd, 1):
line = line.strip()
if not line:
at_paragraph_start = True

View file

@ -407,12 +407,15 @@ def check_output(generated_output_file, main_input_file, merged_files):
is also present in an output file. This is not perfect but good enough
for now.
"""
generated_output = set(open(generated_output_file, 'r', encoding='utf-8'))
for line in open(main_input_file, 'r', encoding='utf-8'):
with open(generated_output_file, 'r', encoding='utf-8') as out_fd:
generated_output = set(out_fd)
with open(main_input_file, 'r', encoding='utf-8') as in_fd:
for line in in_fd:
if line not in generated_output:
raise LostContent('original file', line)
for merged_file in merged_files:
for line in open(merged_file, 'r', encoding='utf-8'):
with open(merged_file, 'r', encoding='utf-8') as in_fd:
for line in in_fd:
if line not in generated_output:
raise LostContent(merged_file, line)

View file

@ -18,7 +18,7 @@
import itertools
import re
from typing import Dict, Iterable, Iterator, List, Optional, Pattern, Set, Tuple, Union
from typing import Dict, IO, Iterable, Iterator, List, Optional, Pattern, Set, Tuple, Union
class ReadFileLineException(Exception):
@ -50,12 +50,13 @@ class read_file_lines:
"""
def __init__(self, filename: str, binary: bool = False) -> None:
self.filename = filename
self.file = None #type: Optional[IO[str]]
self.line_number = 'entry' #type: Union[int, str]
self.generator = None #type: Optional[Iterable[Tuple[int, str]]]
self.binary = binary
def __enter__(self) -> 'read_file_lines':
self.generator = enumerate(open(self.filename,
'rb' if self.binary else 'r'))
self.file = open(self.filename, 'rb' if self.binary else 'r')
self.generator = enumerate(self.file)
return self
def __iter__(self) -> Iterator[str]:
assert self.generator is not None
@ -64,6 +65,8 @@ class read_file_lines:
yield content
self.line_number = 'exit'
def __exit__(self, exc_type, exc_value, exc_traceback) -> None:
if self.file is not None:
self.file.close()
if exc_type is not None:
raise ReadFileLineException(self.filename, self.line_number) \
from exc_value

View file

@ -56,7 +56,8 @@ class Requirements:
* Comments (``#`` at the beginning of the line or after whitespace).
* ``-r FILENAME`` to include another file.
"""
for line in open(filename):
with open(filename) as fd:
for line in fd:
line = line.strip()
line = re.sub(r'(\A|\s+)#.*', r'', line)
if not line: