New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow json.tool to have identical infile and outfile #78108
Comments
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: |
yep, or you could use sponge cat example.json | python3 -m json.tool | sponge example.json a small workaround ;-) |
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. |
Hi, I proposed a path in #7865, I'm not sure if I can apply the label |
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): $ ./python -m json.tool invalid_file.dat nofile.dat
Expecting value: line 1 column 1 (char 0)
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name='nofile.dat' mode='w' encoding='UTF-8'>
$ ./python -m json.tool valid.dat valid.dat
Expecting value: line 1 column 1 (char 0)
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name='valid.dat' mode='w' encoding='UTF-8'> |
@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. |
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). |
Hi Pablo, while this patch should fix both problems, I'm not sure how to write a regression test for this, Should I call https://github.com/remilapeyre/cpython/blob/04b2ade751b318460c1f0f9566676ef519358328/Lib/json/tool.py#L18 directly and mock |
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? |
Hello @pablogsal, What do you think about the PR of Rémi? Thank you, |
@pablogsal The issue seems to have been solved. Can we close the issue?
{"name": "John", "age": 30, "car": null} After
{
"name": "John",
"age": 30,
"car": null
} |
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:
The text was updated successfully, but these errors were encountered: