From 22bea7a4f6c830d1ac46de749e985d9f5a4b6dd3 Mon Sep 17 00:00:00 2001 From: Nick Sweeting Date: Tue, 16 Apr 2019 23:21:49 -0400 Subject: [PATCH] use atomic writes inside to_json helper func --- archivebox/legacy/index.py | 1 - archivebox/legacy/util.py | 6 ++++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/archivebox/legacy/index.py b/archivebox/legacy/index.py index 9574c1bf..03cd52a9 100644 --- a/archivebox/legacy/index.py +++ b/archivebox/legacy/index.py @@ -20,7 +20,6 @@ from .config import ( ) from .util import ( scheme, - fuzzy_url, ts_to_date, urlencode, htmlencode, diff --git a/archivebox/legacy/util.py b/archivebox/legacy/util.py index 92410d2f..a1c823ff 100644 --- a/archivebox/legacy/util.py +++ b/archivebox/legacy/util.py @@ -618,8 +618,10 @@ class ExtendedEncoder(JSONEncoder): def to_json(obj: Any, file: IO=None, indent: Optional[int]=4, sort_keys: bool=True, cls=ExtendedEncoder) -> Optional[str]: if file: - json.dump(obj, file, indent=indent, sort_keys=sort_keys, cls=ExtendedEncoder) - return None + path = os.path.realpath(file.name) + contents = json.dumps(obj, indent=indent, sort_keys=sort_keys, cls=ExtendedEncoder) + atomic_write(contents, path) + return contents else: return json.dumps(obj, indent=indent, sort_keys=sort_keys, cls=ExtendedEncoder)