Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit bd76501

Browse files
orlitzkyMatthias Koeppe
authored andcommitted
Trac #33213: clean up after tmp_filename() and tmp_dir() for now.
Until these two functions 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.
1 parent 4182565 commit bd76501

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/sage/misc/temporary_file.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@
2828
import tempfile
2929
import atexit
3030

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

3239
def delete_tmpfiles():
3340
"""
@@ -96,7 +103,9 @@ def tmp_dir(name="dir_", ext=""):
96103
0
97104
sage: f.close()
98105
"""
99-
tmp = tempfile.mkdtemp(prefix=name, suffix=ext)
106+
tmp = tempfile.mkdtemp(prefix=name,
107+
suffix=ext,
108+
dir=TMP_DIR_FILENAME_BASE.name)
100109
name = os.path.abspath(tmp)
101110
return name + os.sep
102111

@@ -145,7 +154,9 @@ def tmp_filename(name="tmp_", ext=""):
145154
0
146155
sage: f.close()
147156
"""
148-
handle, tmp = tempfile.mkstemp(prefix=name, suffix=ext)
157+
handle, tmp = tempfile.mkstemp(prefix=name,
158+
suffix=ext,
159+
dir=TMP_DIR_FILENAME_BASE.name)
149160
os.close(handle)
150161
name = os.path.abspath(tmp)
151162
return name

0 commit comments

Comments
 (0)