Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bin/jsonpatch
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def patch_files():
result = jsonpatch.apply_patch(doc, patch)

if args.in_place:
# 2026-10-11: Release the input handle before replacing it on Windows.
args.ORIGINAL.close()
dirname = os.path.abspath(os.path.dirname(args.ORIGINAL.name))

try:
Expand Down
22 changes: 22 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import jsonpatch
import jsonpointer
import random
import runpy # 2026-10-11: Exercise the CLI file-handle lifecycle.
import sys
import os
import stat
Expand Down Expand Up @@ -1898,6 +1899,27 @@ def test_jsonpatch_in_place(self):
# the temporary file has been moved over the original
self.assertEqual(sorted(os.listdir(self.dir)), ['doc.json', 'patch.json'])

# 2026-10-11: Windows cannot replace the original while its input is open.
def test_jsonpatch_in_place_closes_original_before_rename(self):
doc = self.write('doc.json', {'a': 1})
patch = self.write('patch.json', [{'op': 'replace', 'path': '/a', 'value': 2}])
here = os.path.dirname(os.path.abspath(__file__))
script = runpy.run_path(os.path.join(here, 'bin', 'jsonpatch'))
parser = script['parser']
args = parser.parse_args(['-i', doc, patch])
self.addCleanup(args.ORIGINAL.close)
self.addCleanup(args.PATCH.close)
rename = os.rename

def rename_closed_original(source, target):
self.assertTrue(args.ORIGINAL.closed)
return rename(source, target)

with mock.patch.object(parser, 'parse_args', return_value=args), \
mock.patch('os.rename', side_effect=rename_closed_original):
script['patch_files']()
self.assertEqual(self.read(doc), {'a': 2})

@unittest.skipUnless(os.name == 'posix', 'needs POSIX file permissions')
def test_jsonpatch_in_place_keeps_permissions(self):
doc = self.write('doc.json', {'a': 1})
Expand Down
Loading