mirror of
https://github.com/python/cpython.git
synced 2025-12-04 00:30:19 +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
13
Lib/dis.py
13
Lib/dis.py
|
|
@ -1032,11 +1032,16 @@ def main():
|
|||
help='show inline caches')
|
||||
parser.add_argument('-O', '--show-offsets', action='store_true',
|
||||
help='show instruction offsets')
|
||||
parser.add_argument('infile', type=argparse.FileType('rb'), nargs='?', default='-')
|
||||
parser.add_argument('infile', nargs='?', default='-')
|
||||
args = parser.parse_args()
|
||||
with args.infile as infile:
|
||||
source = infile.read()
|
||||
code = compile(source, args.infile.name, "exec")
|
||||
if args.infile == '-':
|
||||
name = '<stdin>'
|
||||
source = sys.stdin.buffer.read()
|
||||
else:
|
||||
name = args.infile
|
||||
with open(args.infile, 'rb') as infile:
|
||||
source = infile.read()
|
||||
code = compile(source, name, "exec")
|
||||
dis(code, show_caches=args.show_caches, show_offsets=args.show_offsets)
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue