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

@ -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__":