Skip to content

Commit a12432d

Browse files
author
Guido van Rossum
committed
Catch and ignore errors writing JSON files.
This is an alternative attempt at fixing issue #3215, given the complexity of PR #3239.
1 parent 8293f40 commit a12432d

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

mypy/build.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -897,12 +897,17 @@ def write_cache(id: str, path: str, tree: MypyFile,
897897
data_mtime = os.path.getmtime(data_json)
898898
manager.trace("Interface for {} is unchanged".format(id))
899899
else:
900-
with open(data_json_tmp, 'w') as f:
901-
f.write(data_str)
902-
f.write('\n')
903-
data_mtime = os.path.getmtime(data_json_tmp)
904-
os.replace(data_json_tmp, data_json)
905900
manager.trace("Interface for {} has changed".format(id))
901+
try:
902+
with open(data_json_tmp, 'w') as f:
903+
f.write(data_str)
904+
f.write('\n')
905+
os.replace(data_json_tmp, data_json)
906+
data_mtime = os.path.getmtime(data_json)
907+
except os.error as err:
908+
# Most likely the error is the replace() call
909+
# (see https://github.com/python/mypy/issues/3215).
910+
manager.log("Error writing data JSON file {}".format(data_json_tmp))
906911

907912
mtime = st.st_mtime
908913
size = st.st_size
@@ -922,12 +927,17 @@ def write_cache(id: str, path: str, tree: MypyFile,
922927
}
923928

924929
# Write meta cache file
925-
with open(meta_json_tmp, 'w') as f:
926-
if manager.options.debug_cache:
927-
json.dump(meta, f, indent=2, sort_keys=True)
928-
else:
929-
json.dump(meta, f)
930-
os.replace(meta_json_tmp, meta_json)
930+
try:
931+
with open(meta_json_tmp, 'w') as f:
932+
if manager.options.debug_cache:
933+
json.dump(meta, f, indent=2, sort_keys=True)
934+
else:
935+
json.dump(meta, f)
936+
os.replace(meta_json_tmp, meta_json)
937+
except os.error as err:
938+
# Most likely the error is the replace() call
939+
# (see https://github.com/python/mypy/issues/3215).
940+
manager.log("Error writing meta JSON file {}".format(meta_json_tmp))
931941

932942
return interface_hash
933943

0 commit comments

Comments
 (0)