@@ -1104,44 +1104,47 @@ def eval(self, x, globals, strip=False, filename=None, debug=None,
1104
1104
else :
1105
1105
filename = os .path .splitext (filename )[0 ] # get rid of extension
1106
1106
1107
- base = TemporaryDirectory ()
1108
- orig_base , filename = os .path .split (os .path .abspath (filename ))
1109
- if len (filename .split ()) > 1 :
1110
- raise ValueError ("filename must contain no spaces" )
1111
- if debug is None :
1112
- debug = self .__debug
1113
- x = self ._latex_preparse (x , locals )
1114
- O = open (os .path .join (base , filename + ".tex" ), 'w' )
1115
- if self .__slide :
1116
- O .write (SLIDE_HEADER )
1117
- O .write (MACROS )
1118
- O .write ('\\ begin{document}\n \n ' )
1119
- else :
1120
- O .write (LATEX_HEADER )
1121
- O .write (MACROS )
1122
- O .write ('\\ begin{document}\n ' )
1123
-
1124
- O .write (x )
1125
- if self .__slide :
1126
- O .write ('\n \n \\ end{document}' )
1127
- else :
1128
- O .write ('\n \n \\ end{document}\n ' )
1107
+ result = None
1108
+ with TemporaryDirectory () as base :
1109
+ orig_base , filename = os .path .split (os .path .abspath (filename ))
1110
+ if len (filename .split ()) > 1 :
1111
+ raise ValueError ("filename must contain no spaces" )
1112
+ if debug is None :
1113
+ debug = self .__debug
1114
+ x = self ._latex_preparse (x , locals )
1115
+ O = open (os .path .join (base , filename + ".tex" ), 'w' )
1116
+ if self .__slide :
1117
+ O .write (SLIDE_HEADER )
1118
+ O .write (MACROS )
1119
+ O .write ('\\ begin{document}\n \n ' )
1120
+ else :
1121
+ O .write (LATEX_HEADER )
1122
+ O .write (MACROS )
1123
+ O .write ('\\ begin{document}\n ' )
1129
1124
1130
- O .close ()
1131
- if engine is None :
1132
- if self .__engine is None :
1133
- engine = _Latex_prefs ._option ["engine" ]
1125
+ O .write (x )
1126
+ if self .__slide :
1127
+ O .write ('\n \n \\ end{document}' )
1134
1128
else :
1135
- engine = self .__engine
1136
- e = _run_latex_ (os .path .join (base , filename + ".tex" ), debug = debug ,
1137
- density = density , engine = engine , png = True )
1138
- result = None
1139
- if e .find ("Error" ) == - 1 :
1140
- shutil .copy (os .path .join (base , filename + ".png" ),
1141
- os .path .join (orig_base , filename + ".png" ))
1142
- result = ''
1129
+ O .write ('\n \n \\ end{document}\n ' )
1130
+
1131
+ O .close ()
1132
+ if engine is None :
1133
+ if self .__engine is None :
1134
+ engine = _Latex_prefs ._option ["engine" ]
1135
+ else :
1136
+ engine = self .__engine
1137
+ e = _run_latex_ (os .path .join (base , filename + ".tex" ),
1138
+ debug = debug ,
1139
+ density = density ,
1140
+ engine = engine ,
1141
+ png = True )
1142
+
1143
+ if e .find ("Error" ) == - 1 :
1144
+ shutil .copy (os .path .join (base , filename + ".png" ),
1145
+ os .path .join (orig_base , filename + ".png" ))
1146
+ result = ''
1143
1147
1144
- base .cleanup ()
1145
1148
return result
1146
1149
1147
1150
def blackboard_bold (self , t = None ):
@@ -1920,30 +1923,27 @@ def view(objects, title='Sage', debug=False, sep='', tiny=False,
1920
1923
if pdflatex or (viewer == "pdf" and engine == "latex" ):
1921
1924
engine = "pdflatex"
1922
1925
# command line or notebook with viewer
1923
- tmp = TemporaryDirectory ()
1924
- tex_file = os .path .join (tmp , "sage.tex" )
1925
- with open (tex_file , 'w' ) as file :
1926
- file .write (s )
1927
- suffix = _run_latex_ (tex_file , debug = debug , engine = engine , png = False )
1928
- if suffix == "pdf" :
1929
- from sage .misc .viewer import pdf_viewer
1930
- viewer = pdf_viewer ()
1931
- elif suffix == "dvi" :
1932
- from sage .misc .viewer import dvi_viewer
1933
- viewer = dvi_viewer ()
1934
- else :
1935
- print ("Latex error" )
1936
- tmp .cleanup ()
1937
- return
1938
- output_file = os .path .join (tmp , "sage." + suffix )
1939
- # this should get changed if we switch the stuff in misc.viewer to
1940
- # producing lists
1941
- if debug :
1942
- print ('viewer: "{}"' .format (viewer ))
1943
- call ('%s %s' % (viewer , output_file ), shell = True ,
1944
- stdout = PIPE , stderr = PIPE )
1945
-
1946
- tmp .cleanup ()
1926
+ with TemporaryDirectory () as tmp :
1927
+ tex_file = os .path .join (tmp , "sage.tex" )
1928
+ with open (tex_file , 'w' ) as file :
1929
+ file .write (s )
1930
+ suffix = _run_latex_ (tex_file , debug = debug , engine = engine , png = False )
1931
+ if suffix == "pdf" :
1932
+ from sage .misc .viewer import pdf_viewer
1933
+ viewer = pdf_viewer ()
1934
+ elif suffix == "dvi" :
1935
+ from sage .misc .viewer import dvi_viewer
1936
+ viewer = dvi_viewer ()
1937
+ else :
1938
+ print ("Latex error" )
1939
+ return
1940
+ output_file = os .path .join (tmp , "sage." + suffix )
1941
+ # this should get changed if we switch the stuff in misc.viewer to
1942
+ # producing lists
1943
+ if debug :
1944
+ print ('viewer: "{}"' .format (viewer ))
1945
+ call ('%s %s' % (viewer , output_file ), shell = True ,
1946
+ stdout = PIPE , stderr = PIPE )
1947
1947
return
1948
1948
1949
1949
@@ -1995,22 +1995,21 @@ def png(x, filename, density=150, debug=False,
1995
1995
# path name for permanent png output
1996
1996
abs_path_to_png = os .path .abspath (filename )
1997
1997
# temporary directory to store stuff
1998
- tmp = TemporaryDirectory ()
1999
- tex_file = os .path .join (tmp , "sage.tex" )
2000
- png_file = os .path .join (tmp , "sage.png" )
2001
- # write latex string to file
2002
- with open (tex_file , 'w' ) as file :
2003
- file .write (s )
2004
- # run latex on the file, producing png output to png_file
2005
- e = _run_latex_ (tex_file , density = density , debug = debug ,
2006
- png = True , engine = engine )
2007
- if e .find ("Error" ) == - 1 :
2008
- # if no errors, copy png_file to the appropriate place
2009
- shutil .copy (png_file , abs_path_to_png )
2010
- else :
2011
- print ("Latex error" )
1998
+ with TemporaryDirectory () as tmp :
1999
+ tex_file = os .path .join (tmp , "sage.tex" )
2000
+ png_file = os .path .join (tmp , "sage.png" )
2001
+ # write latex string to file
2002
+ with open (tex_file , 'w' ) as file :
2003
+ file .write (s )
2004
+ # run latex on the file, producing png output to png_file
2005
+ e = _run_latex_ (tex_file , density = density , debug = debug ,
2006
+ png = True , engine = engine )
2007
+ if e .find ("Error" ) == - 1 :
2008
+ # if no errors, copy png_file to the appropriate place
2009
+ shutil .copy (png_file , abs_path_to_png )
2010
+ else :
2011
+ print ("Latex error" )
2012
2012
2013
- tmp .cleanup ()
2014
2013
if debug :
2015
2014
return s
2016
2015
return
0 commit comments