diff --git a/src/jinja2/bccache.py b/src/jinja2/bccache.py index f8bdf3032..e9dca6d61 100644 --- a/src/jinja2/bccache.py +++ b/src/jinja2/bccache.py @@ -279,31 +279,28 @@ def dump_bytecode(self, bucket: Bucket) -> None: # https://github.com/pallets/jinja/issues/1654. fn = self._get_cache_filename(bucket) - # Name of the temporary file - tempfn: t.Optional[str] = None + f = tempfile.NamedTemporaryFile( + mode="wb", + dir=os.path.dirname(fn), + prefix=os.path.basename(fn), + suffix=".tmp", + delete=False, + ) def clean() -> None: - if tempfn: - try: - os.remove(tempfn) - except OSError: - # This can occur if another process called - # clear(). Also, on Windows, this might occur for - # reasons beyond our control. For example, - # another process such as the Windows Search - # indexer or an antivirus product may be holding - # the file open, preventing deletion. - pass + try: + os.remove(f.name) + except OSError: + # This can occur if another process called + # clear(). Also, on Windows, this might occur for + # reasons beyond our control. For example, + # another process such as the Windows Search + # indexer or an antivirus product may be holding + # the file open, preventing deletion. + pass try: - with tempfile.NamedTemporaryFile( - mode="wb", - dir=os.path.dirname(fn), - prefix=os.path.basename(fn), - suffix=".tmp", - delete=False, - ) as f: - tempfn = f.name + with f: bucket.write_bytecode(f) except BaseException: clean() @@ -319,7 +316,7 @@ def clean() -> None: # For example, an antivirus product or search indexer # process. # 3) Another process might clear() just before this rename. - os.rename(tempfn, fn) + os.rename(f.name, fn) except OSError: clean() except BaseException: