You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow json.tool to have identical infile and outfile #78108
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:
assignee=Noneclosed_at=Nonecreated_at=<Date2018-06-21.10:45:40.446>labels= ['type-bug', 'library', '3.11']
title='Allow json.tool to have identical infile and outfile'updated_at=<Date2021-11-08.22:09:34.365>user='https://github.com/michael-kuhlmann'
The current status of json.tool also leaks a file descriptor if you use the same filename or an invalid one (needs debug build to receive this error message):
Maybe we should include a test that checks that if you provide an invalid file the file descriptors are not leaked (it can be in a different PR, maybe).
The original issue seems fixed but there was this comment: #89807 (comment) and the subsequent reply.
Nonetheless, I think this specific issue can be closed so I'll do it. I think a new issue should be created to handle the case --json-lines + same in/out file.
Activity
michael-kuhlmann commentedon Jun 21, 2018
It would be nice to have same infile and outfile for json.tool to replace json files with their pretty-printed version.
Currently, if I try this I get an error:
$ python3 -m json.tool example.json example.json
Expecting value: line 1 column 1 (char 0)
matrixise commentedon Jun 21, 2018
yep, or you could use sponge
cat example.json | python3 -m json.tool | sponge example.json
a small workaround ;-)
remilapeyre commentedon Jun 21, 2018
Hi Michael, looking at the current code of json.tool, there is no reason for it not to be able to do this, I will a patch to do this tonight.
remilapeyre commentedon Jun 23, 2018
Hi, I proposed a path in #7865, I'm not sure if I can apply the label
skip newsor if only a reviewer can.pablogsal commentedon Jun 23, 2018
The current status of json.tool also leaks a file descriptor if you use the same filename or an invalid one (needs debug build to receive this error message):
pablogsal commentedon Jun 23, 2018
@rémi can you include a NEWS entry?
Also, indicate that your patch prevents a file descriptor to be leaked in the cases indicated in my last message.
pablogsal commentedon Jun 23, 2018
Maybe we should include a test that checks that if you provide an invalid file the file descriptors are not leaked (it can be in a different PR, maybe).
remilapeyre commentedon Jun 24, 2018
Hi Pablo, while this patch should fix both problems, I'm not sure how to write a regression test for this,
assert_python_okin https://github.com/python/cpython/pull/7865/files#diff-7d4645839a05727ebdf39226fd56e29cR97 forks the interpreter so I'm not sure I can check for unclosed fd during the test.Should I call https://github.com/remilapeyre/cpython/blob/04b2ade751b318460c1f0f9566676ef519358328/Lib/json/tool.py#L18 directly and mock
io.openandos.close?remilapeyre commentedon Jun 29, 2018
Hi Pablo, I added two tests to confirm that file descriptors do not link anymore.
The tests are rather ugly but I'm not sure if it's possible to do better.
Is this patch ok for you?
matrixise commentedon Apr 12, 2021
Hello @pablogsal,
What do you think about the PR of Rémi?
Thank you,
furkanonder commentedon May 13, 2023
@pablogsal The issue seems to have been solved. Can we close the issue?
cat example.json{"name": "John", "age": 30, "car": null}After
python -m json.tool example.json example.json;cat example.json{ "name": "John", "age": 30, "car": null }picnixz commentedon Sep 7, 2024
The original issue seems fixed but there was this comment: #89807 (comment) and the subsequent reply.
Nonetheless, I think this specific issue can be closed so I'll do it. I think a new issue should be created to handle the case
--json-lines+ same in/out file.