Skip to content

Commit

Permalink
fix: remove formally generated tempfiles during error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
CNSeniorious000 committed Jun 11, 2024
1 parent 74745bf commit 10cb385
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion python/promplate/prompt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ def add_linecache(filename: str, source_getter: Callable[[], str]):


def save_tempfile(filename: str, source: str, auto_deletion: bool):
from tempfile import mkdtemp
from tempfile import gettempdir, mkdtemp

for i in Path(gettempdir()).glob(f"promplate-*/{filename}"):
i.unlink()

file = Path(mkdtemp(prefix="promplate-")) / filename
file.write_text(source)
Expand Down
9 changes: 9 additions & 0 deletions python/tests/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,3 +416,12 @@ def test_error_handling():
t.render()
except NameError:
assert "__append__(a)" in format_exc()


def test_error_handling_namesake():
with raises(ValueError, match="123"):
Template("{# raise ValueError(123) #}").render()
try:
Template("{# raise TypeError(123) #}").render()
except TypeError:
assert "ValueError" not in format_exc()

0 comments on commit 10cb385

Please sign in to comment.