mirror of
https://github.com/python/cpython.git
synced 2025-12-10 11:00:14 +00:00
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:
parent
a8629816c6
commit
b3d2427f22
7 changed files with 56 additions and 41 deletions
|
|
@ -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__":
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue