Skip to content

Commit

Permalink
eliminate tempfn variable from dump_bytecode
Browse files Browse the repository at this point in the history
  • Loading branch information
David Vitek committed Apr 11, 2022
1 parent 8859c75 commit 77111f3
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions src/jinja2/bccache.py
Expand Up @@ -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()
Expand Down

0 comments on commit 77111f3

Please sign in to comment.