Skip to content

bpo-45644: json.tool: Use staged write for outfile. #30659

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

Closed
wants to merge 3 commits into from

Conversation

methane
Copy link
Member

@methane methane commented Jan 18, 2022

To support python -m json.tool --json-lines infile infile.

https://bugs.python.org/issue45644

Support python -m json.tool --json-lines infile infile
@CharString
Copy link
Contributor

CharString commented Jan 18, 2022

I don't particularly like it, it assumes write access to more than just inode of the original file. But 🤷 I won't me bikeshedding over this. The implementation reads well, the user facing interface is the same and I expect in 99% of the cases, write access won't be a problem.

Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
@methane
Copy link
Member Author

methane commented Jan 18, 2022

Windows CI is failing:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "D:\a\cpython\cpython\Lib\json\tool.py", line 104, in <module>
    main()
    ^^^^^^
  File "D:\a\cpython\cpython\Lib\json\tool.py", line 94, in main
    with out as outfile:
    ^^^^^^^^^^^^^^^^^^^^
  File "D:\a\cpython\cpython\Lib\contextlib.py", line 144, in __exit__
    next(self.gen)
    ^^^^^^^^^^^^^^
  File "D:\a\cpython\cpython\Lib\json\tool.py", line 35, in _staged_outfile
    os.replace(tempname, path)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [WinError 5] Access is denied: 'D:\\a\\cpython\\cpython\\build\\test_python_1644\ufffd\\test_python_worker_5368\ufffd\\@test_5368_tmp\ufffd.1kimt_1s' -> '@test_5368_tmp\ufffd'

It is difficult to write the staged write idiom with few lines of code.

For example: https://github.com/mitsuhiko/python-atomicfile/blob/master/atomicfile.py

I close this PR and wait that Python has this feature in stdlib.

@methane methane closed this Jan 18, 2022
@methane methane deleted the json-infile-outfile branch January 18, 2022 11:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants