Repository navigation
pickle and pickletools cli interface doesn't close input and output file. #85567
Description
Activity
pickle and pickletools use argparse with FileType which is not automatically closed. Other cli interfaces like json [0], ast [1] use context manager to close filetype objects.
pickle :
Line 1799 in af08db7
'pickle_file', type=argparse.FileType('br'), mypickle
>> import pickle
>> with open("mypickle", "wb") as f: pickle.dump({"a": 1}, f)./python -Wall -m pickle mypickle
{'a': 1}
sys:1: ResourceWarning: unclosed file <_io.BufferedReader name='mypickle'>pickletools :
Lines 2850 to 2855 in af08db7
parser.add_argument( 'pickle_file', type=argparse.FileType('br'), nargs='*', help='the pickle file') parser.add_argument( '-o', '--output', default=sys.stdout, type=argparse.FileType('w'), help='the file where the output should be written') ./python -Wall -m pickletools mypickle -o mypickle.py
sys:1: ResourceWarning: unclosed file <_io.BufferedReader name='mypickle'>
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name='mypickle.py' mode='w' encoding='UTF-8'>[0]
Line 61 in af08db7
with options.infile as infile, options.outfile as outfile:
[1]Line 1510 in af08db7
with args.infile as infile: - added3.8 (EOL)end of lifeend of life3.9 (EOL)end of lifeend of life3.10 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Jul 25, 2020 Hi,
First-time contributor here, I've made a patch in follow-up to the discussions that happened in Amir's patch in regards to this. I'd appreciate it if someone would be able to take a look and review it!
9 remaining items
- added a commit that references this issue
on Jul 5, 2022 @serhiy-storchaka The issue seems to be resolved in this PR.
The
pickletoolsCLI is especially treacherous. It leaks the files if you specify the-targument. It leaks the output file if you only specify the output file argument. And of course it leaks the files in case of argument errors, for example specifying non-integer indent level or non-writeable output file.The only reliable way to handle this is to get rid of FileType and open and close files explicitly. PR #113618 does this.
Reacted by Furkan Onder
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs