bpo-33927: Add support for same infile and outfile to json.tool by remilapeyre · Pull Request #7865 · python/cpython (original) (raw)
Hi @rhettinger, thanks for the comments.
fix the other minor fd leaks mentioned in the bug report.
I'm not sure to follow, if I understand Pablo's remark in https://bugs.python.org/issue33927#msg320326 the problem is about the leaked fd outfile
when an exception is raised before the second with
statement. This happens when both fd points to the same file, outfile
being opened in 'w' mode, the content of the file gets destroyed before it could be read.
When this happens, the second fd has been open by argparse.FileType()
but is never closed hence the leak. As I understand it, this never happens with the first fd since an exception can't be raised before the first with
statement and it can therefore stay as argparse.FileType()
.
Both leaks shown by Pablo seems to be fixed by this patch:
➜ debug git:(bugfix/json.tool-same-infile-outfile) ✗ touch invalid_file.dat
➜ debug git:(bugfix/json.tool-same-infile-outfile) ✗ ./python.exe -m json.tool invalid_file.dat nofile.dat
Expecting value: line 1 column 1 (char 0)
➜ debug git:(bugfix/json.tool-same-infile-outfile) ✗ echo '{"foo":"bar"}'>valid.dat
➜ debug git:(bugfix/json.tool-same-infile-outfile) ✗ ./python.exe -m json.tool valid.dat valid.dat
➜ debug git:(bugfix/json.tool-same-infile-outfile) ✗ cat valid.dat
{
"foo": "bar"
}
Am I missing something?