diff options
Diffstat (limited to 'generat3d')
| -rw-r--r-- | generat3d/dbz.py | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/generat3d/dbz.py b/generat3d/dbz.py index a71fbd5..2a56230 100644 --- a/generat3d/dbz.py +++ b/generat3d/dbz.py @@ -15,6 +15,7 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import contextlib import gzip import json import math @@ -22,7 +23,7 @@ import os import tempfile from pathlib import Path -from . import __version__ +from . import Error, __version__ from .evaluate import evaluate @@ -70,11 +71,16 @@ def build(scene, roots): def write(doc, out): """Write doc as gzipped JSON. A temp file plus rename: no partial file on failure.""" out = Path(out) - fd, tmp = tempfile.mkstemp(dir=out.parent, prefix=out.name, suffix=".tmp") + tmp = None try: + fd, tmp = tempfile.mkstemp(dir=out.parent, prefix=out.name, suffix=".tmp") with os.fdopen(fd, "wb") as f: f.write(gzip.compress(json.dumps(doc).encode())) os.replace(tmp, out) - except BaseException: - os.unlink(tmp) + except BaseException as e: + if tmp is not None: + with contextlib.suppress(OSError): + os.unlink(tmp) + if isinstance(e, OSError): + raise Error(f"cannot write {out}: {e.strerror or e}") from None raise |
