Skip to content

Commit 3960180

Browse files
authored
Merge pull request #241 from ShahadatTest/fix/cleanup-failed-output-setup
Clean temporary output after fdopen setup failure
2 parents 083bd38 + d1d81db commit 3960180

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

‎jsonpatch_cli.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def patch_files():
7878
args.ORIGINAL.close()
7979
dirname = os.path.abspath(os.path.dirname(args.ORIGINAL.name))
8080

81+
fd = None
8182
try:
8283
# Attempt to replace the file atomically. We do this by
8384
# creating a temporary file in the same directory as the
@@ -90,6 +91,17 @@ def patch_files():
9091
atomic = True
9192

9293
except OSError:
94+
# mkstemp may have succeeded before fdopen failed. Neither the
95+
# descriptor nor the temporary file belongs to the fallback.
96+
if fd is not None:
97+
try:
98+
os.close(fd)
99+
except OSError:
100+
pass
101+
try:
102+
os.unlink(pathname)
103+
except OSError:
104+
pass
93105
# We failed to create the temporary file for an atomic
94106
# replace, so fall back to non-atomic mode by backing up
95107
# the original (if desired) and writing a new file.

‎tests.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2159,6 +2159,45 @@ def dump(value, stream, **kwargs):
21592159
self.assertEqual(self.read(doc), {'a': 1})
21602160
self.assertEqual(sorted(os.listdir(self.dir)), ['doc.json', 'patch.json'])
21612161

2162+
def test_jsonpatch_in_place_cleans_temp_on_fdopen_error(self):
2163+
for backup in (False, True):
2164+
with self.subTest(backup=backup):
2165+
tmp = tempfile.TemporaryDirectory()
2166+
self.addCleanup(tmp.cleanup)
2167+
self.dir = tmp.name
2168+
doc = self.write('doc.json', {'a': 1})
2169+
patch = self.write('patch.json', [{'op': 'replace', 'path': '/a', 'value': 2}])
2170+
parser = jsonpatch_cli.jsonpatch_parser
2171+
args = parser.parse_args(['-i', doc, patch] + (['-b'] if backup else []))
2172+
self.addCleanup(args.ORIGINAL.close)
2173+
self.addCleanup(args.PATCH.close)
2174+
descriptors = []
2175+
2176+
def close_if_open(fd):
2177+
try:
2178+
os.close(fd)
2179+
except OSError:
2180+
pass
2181+
2182+
def fail_fdopen(fd, *args, **kwargs):
2183+
descriptors.append(fd)
2184+
self.addCleanup(close_if_open, fd)
2185+
raise OSError('cannot wrap temporary descriptor')
2186+
2187+
with mock.patch.object(parser, 'parse_args', return_value=args), \
2188+
mock.patch('os.fdopen', side_effect=fail_fdopen):
2189+
jsonpatch_cli.patch_files()
2190+
2191+
self.assertEqual(self.read(doc), {'a': 2})
2192+
if backup:
2193+
self.assertEqual(self.read(doc + '.orig'), {'a': 1})
2194+
with self.assertRaises(OSError):
2195+
os.fstat(descriptors[0])
2196+
expected = ['doc.json', 'patch.json']
2197+
if backup:
2198+
expected.append('doc.json.orig')
2199+
self.assertEqual(sorted(os.listdir(self.dir)), sorted(expected))
2200+
21622201
def test_jsonpatch_in_place_preserves_original_on_replace_error(self):
21632202
doc = self.write('doc.json', {'a': 1})
21642203
patch = self.write('patch.json', [{'op': 'replace', 'path': '/a', 'value': 2}])

0 commit comments

Comments
 (0)