30
30
import re
31
31
import shutil
32
32
from subprocess import call , PIPE
33
+ from tempfile import TemporaryDirectory
33
34
34
35
from sage .misc .cachefunc import cached_function , cached_method
35
- from sage .misc .temporary_file import tmp_dir
36
36
from sage .structure .sage_object import SageObject
37
37
38
38
from sage .misc .lazy_import import lazy_import
@@ -1103,7 +1103,8 @@ def eval(self, x, globals, strip=False, filename=None, debug=None,
1103
1103
filename = 'sage%s' % random .randint (1 , 100 ) # to defeat browser caches
1104
1104
else :
1105
1105
filename = os .path .splitext (filename )[0 ] # get rid of extension
1106
- base = tmp_dir ()
1106
+
1107
+ base = TemporaryDirectory ()
1107
1108
orig_base , filename = os .path .split (os .path .abspath (filename ))
1108
1109
if len (filename .split ()) > 1 :
1109
1110
raise ValueError ("filename must contain no spaces" )
@@ -1134,13 +1135,14 @@ def eval(self, x, globals, strip=False, filename=None, debug=None,
1134
1135
engine = self .__engine
1135
1136
e = _run_latex_ (os .path .join (base , filename + ".tex" ), debug = debug ,
1136
1137
density = density , engine = engine , png = True )
1138
+ result = None
1137
1139
if e .find ("Error" ) == - 1 :
1138
1140
shutil .copy (os .path .join (base , filename + ".png" ),
1139
1141
os .path .join (orig_base , filename + ".png" ))
1140
- shutil . rmtree ( base )
1141
- return ''
1142
- else :
1143
- return
1142
+ result = ''
1143
+
1144
+ base . cleanup ()
1145
+ return result
1144
1146
1145
1147
def blackboard_bold (self , t = None ):
1146
1148
r"""nodetex
@@ -1918,7 +1920,7 @@ def view(objects, title='Sage', debug=False, sep='', tiny=False,
1918
1920
if pdflatex or (viewer == "pdf" and engine == "latex" ):
1919
1921
engine = "pdflatex"
1920
1922
# command line or notebook with viewer
1921
- tmp = tmp_dir ( 'sage_viewer' )
1923
+ tmp = TemporaryDirectory ( )
1922
1924
tex_file = os .path .join (tmp , "sage.tex" )
1923
1925
with open (tex_file , 'w' ) as file :
1924
1926
file .write (s )
@@ -1931,6 +1933,7 @@ def view(objects, title='Sage', debug=False, sep='', tiny=False,
1931
1933
viewer = dvi_viewer ()
1932
1934
else :
1933
1935
print ("Latex error" )
1936
+ tmp .cleanup ()
1934
1937
return
1935
1938
output_file = os .path .join (tmp , "sage." + suffix )
1936
1939
# this should get changed if we switch the stuff in misc.viewer to
@@ -1939,6 +1942,8 @@ def view(objects, title='Sage', debug=False, sep='', tiny=False,
1939
1942
print ('viewer: "{}"' .format (viewer ))
1940
1943
call ('%s %s' % (viewer , output_file ), shell = True ,
1941
1944
stdout = PIPE , stderr = PIPE )
1945
+
1946
+ tmp .cleanup ()
1942
1947
return
1943
1948
1944
1949
@@ -1990,7 +1995,7 @@ def png(x, filename, density=150, debug=False,
1990
1995
# path name for permanent png output
1991
1996
abs_path_to_png = os .path .abspath (filename )
1992
1997
# temporary directory to store stuff
1993
- tmp = tmp_dir ( 'sage_viewer' )
1998
+ tmp = TemporaryDirectory ( )
1994
1999
tex_file = os .path .join (tmp , "sage.tex" )
1995
2000
png_file = os .path .join (tmp , "sage.png" )
1996
2001
# write latex string to file
@@ -2004,6 +2009,8 @@ def png(x, filename, density=150, debug=False,
2004
2009
shutil .copy (png_file , abs_path_to_png )
2005
2010
else :
2006
2011
print ("Latex error" )
2012
+
2013
+ tmp .cleanup ()
2007
2014
if debug :
2008
2015
return s
2009
2016
return
0 commit comments