gh-58032: Do not use argparse.FileType in module CLIs and scripts (GH-113649)

Open and close files manually. It prevents from leaking files,
preliminary creation of output files, and accidental closing of stdin
and stdout.
This commit is contained in:
Serhiy Storchaka 2024-01-10 15:07:19 +02:00 • committed by GitHub
parent a8629816c6
commit b3d2427f22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 56 additions and 41 deletions

View file

@ -165,8 +165,8 @@ decimal_using_bytecode = _using_bytecode(decimal)
def main(import_, options):
if options.source_file:
with options.source_file:
prev_results = json.load(options.source_file)
with open(options.source_file, 'r', encoding='utf-8') as source_file:
prev_results = json.load(source_file)
else:
prev_results = {}
__builtins__.__import__ = import_
@ -218,8 +218,8 @@ def main(import_, options):
new_result/old_result)
print(benchmark_name, ':', result)
if options.dest_file:
with options.dest_file:
json.dump(new_results, options.dest_file, indent=2)
with open(options.dest_file, 'w', encoding='utf-8') as dest_file:
json.dump(new_results, dest_file, indent=2)
if __name__ == '__main__':
@ -229,11 +229,9 @@ if __name__ == '__main__':
parser.add_argument('-b', '--builtin', dest='builtin', action='store_true',
default=False, help="use the built-in __import__")
parser.add_argument('-r', '--read', dest='source_file',
type=argparse.FileType('r'),
help='file to read benchmark data from to compare '
'against')
parser.add_argument('-w', '--write', dest='dest_file',
type=argparse.FileType('w'),
help='file to write benchmark data to')
parser.add_argument('--benchmark', dest='benchmark',
help='specific benchmark to run')

View file

@ -41,25 +41,24 @@ def main() -> None:
description="Generate the Lib/keywords.py file from the grammar."
)
parser.add_argument(
"grammar", type=str, help="The file with the grammar definition in PEG format"
"grammar", help="The file with the grammar definition in PEG format"
)
parser.add_argument(
"tokens_file", type=argparse.FileType("r"), help="The file with the token definitions"
"tokens_file", help="The file with the token definitions"
)
parser.add_argument(
"keyword_file",
type=argparse.FileType("w"),
help="The path to write the keyword definitions",
)
args = parser.parse_args()
grammar, _, _ = build_parser(args.grammar)
with args.tokens_file as tok_file:
with open(args.tokens_file) as tok_file:
all_tokens, exact_tok, non_exact_tok = generate_token_definitions(tok_file)
gen = CParserGenerator(grammar, all_tokens, exact_tok, non_exact_tok, file=None)
gen.collect_rules()
with args.keyword_file as thefile:
with open(args.keyword_file, 'w') as thefile:
all_keywords = sorted(list(gen.keywords.keys()))
all_soft_keywords = sorted(gen.soft_keywords)

View file

@ -1154,12 +1154,13 @@ def output_markdown(
print("Stats gathered on:", date.today(), file=out)
def output_stats(inputs: list[Path], json_output=TextIO | None):
def output_stats(inputs: list[Path], json_output=str | None):
match len(inputs):
case 1:
data = load_raw_data(Path(inputs[0]))
if json_output is not None:
save_raw_data(data, json_output) # type: ignore
with open(json_output, 'w', encoding='utf-8') as f:
save_raw_data(data, f) # type: ignore
stats = Stats(data)
output_markdown(sys.stdout, LAYOUT, stats)
case 2:
@ -1195,7 +1196,6 @@ def main():
parser.add_argument(
"--json-output",
nargs="?",
type=argparse.FileType("w"),
help="Output complete raw results to the given JSON file.",
)

View file

@ -23,7 +23,7 @@ parser = argparse.ArgumentParser(
)
parser.add_argument("srcdir", help="OpenSSL source directory")
parser.add_argument(
"output", nargs="?", type=argparse.FileType("w"), default=sys.stdout
"output", nargs="?", default=None
)
@ -126,8 +126,13 @@ def main():
lines.append("")
lines.extend(gen_error_codes(args))
for line in lines:
args.output.write(line + "\n")
if args.output is None:
for line in lines:
print(line)
else:
with open(args.output, 'w') as output:
for line in lines:
print(line, file=output)
if __name__ == "__main__":