Skip to content

Commit

Permalink
Trac #33797: sage.misc.temporary_file: Remove use of SAGE_TMP
Browse files Browse the repository at this point in the history
(split out from #33213)

Until `tmp_filename` and `tmp_dir` are eliminated from the library, we
don't want them cluttering up /tmp. In typical usage, this is easy
enough to avoid by using one parent temporary directory to contain all
the other temporary files and directories, and then removing that one
parent as sage exits.

URL: https://trac.sagemath.org/33797
Reported by: mkoeppe
Ticket author(s): Michael Orlitzky
Reviewer(s): Matthias Koeppe
  • Loading branch information
Release Manager committed May 22, 2022
2 parents 2ca0530 + bd76501 commit a3fd718
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/sage/misc/temporary_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@

import atexit

# Until tmp_dir() and tmp_filename() are removed, we use this directory
# as the parent for all temporary files & directories created by them.
# This lets us clean up after those two functions when sage exits normally
# using an atexit hook
TMP_DIR_FILENAME_BASE=tempfile.TemporaryDirectory()
atexit.register(lambda: TMP_DIR_FILENAME_BASE.cleanup())


def delete_tmpfiles():
"""
Expand Down Expand Up @@ -97,8 +104,9 @@ def tmp_dir(name="dir_", ext=""):
0
sage: f.close()
"""
from sage.misc.misc import SAGE_TMP
tmp = tempfile.mkdtemp(prefix=name, suffix=ext, dir=str(SAGE_TMP))
tmp = tempfile.mkdtemp(prefix=name,
suffix=ext,
dir=TMP_DIR_FILENAME_BASE.name)
name = os.path.abspath(tmp)
return name + os.sep

Expand Down Expand Up @@ -147,8 +155,9 @@ def tmp_filename(name="tmp_", ext=""):
0
sage: f.close()
"""
from sage.misc.misc import SAGE_TMP
handle, tmp = tempfile.mkstemp(prefix=name, suffix=ext, dir=str(SAGE_TMP))
handle, tmp = tempfile.mkstemp(prefix=name,
suffix=ext,
dir=TMP_DIR_FILENAME_BASE.name)
os.close(handle)
name = os.path.abspath(tmp)
return name
Expand Down

0 comments on commit a3fd718

Please sign in to comment.