Issue 33927: Allow json.tool to have identical infile and outfile (original) (raw)

Issue33927

Created on 2018-06-21 10:45 by kuhlmann, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 7865 closed remi.lapeyre,2018-06-22 22:01
PR 11992 merged 4383,2019-02-22 17:40
PR 29478 closed CharString,2021-11-08 22:09
Messages (10)
msg320151 - (view) Author: Michael Kuhlmann (kuhlmann) Date: 2018-06-21 10:45
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)
msg320152 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2018-06-21 10:48
yep, or you could use sponge cat example.json | python3 -m json.tool sponge example.json a small workaround ;-)
msg320173 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2018-06-21 12:52
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.
msg320302 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2018-06-23 09:25
Hi, I proposed a path in https://github.com/python/cpython/pull/7865, I'm not sure if I can apply the label `skip news` or if only a reviewer can.
msg320326 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2018-06-23 19:28
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'>
msg320328 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2018-06-23 19:30
@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.
msg320329 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2018-06-23 19:41
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).
msg320368 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2018-06-24 12:03
Hi Pablo, while this patch should fix both problems, I'm not sure how to write a regression test for this, `assert_python_ok` in 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.open` and `os.close`?
msg320729 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2018-06-29 18:45
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?
msg390853 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2021-04-12 14:18
Hello @pablogsal, What do you think about the PR of Rémi? Thank you,
History
Date User Action Args
2022-04-11 14:59:02 admin set github: 78108
2021-11-08 22:09:34 CharString set nosy: + CharStringpull_requests: + <pull%5Frequest27728>
2021-05-05 08:13:15 matrixise set versions: + Python 3.11, - Python 3.10
2021-04-12 14🔞33 matrixise set messages: + versions: - Python 3.7, Python 3.8, Python 3.9
2020-06-21 00:41:10 remi.lapeyre set components: + Library (Lib)versions: + Python 3.7, Python 3.8, Python 3.9, Python 3.10
2019-02-22 17:40:34 4383 set pull_requests: + <pull%5Frequest12015>
2018-06-29 18:45:48 remi.lapeyre set messages: +
2018-06-24 12:03:40 remi.lapeyre set messages: +
2018-06-23 19:41:11 pablogsal set messages: +
2018-06-23 19:30:12 pablogsal set messages: +
2018-06-23 19:28:36 pablogsal set nosy: + pablogsalmessages: +
2018-06-23 09:25:47 remi.lapeyre set messages: +
2018-06-22 22:01:06 remi.lapeyre set keywords: + patchstage: patch reviewpull_requests: + <pull%5Frequest7474>
2018-06-22 19:37:01 ned.deily set nosy: + rhettinger, ezio.melotti
2018-06-21 12:52:50 remi.lapeyre set nosy: + remi.lapeyremessages: +
2018-06-21 10:48:29 matrixise set nosy: + matrixisemessages: +
2018-06-21 10:45:40 kuhlmann create