diff --git a/src/jinja2/bccache.py b/src/jinja2/bccache.py index f8bdf3032..f9c065e86 100644 --- a/src/jinja2/bccache.py +++ b/src/jinja2/bccache.py @@ -279,31 +279,29 @@ 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, + ) + tempfn = f.name 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(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: - 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()