Skip to content

Commit

Permalink
Trac #13681: Use os.path.join() to construct temporary filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
jdemeyer committed Nov 3, 2012
1 parent 6418941 commit 5b42d86
Show file tree
Hide file tree
Showing 29 changed files with 56 additions and 73 deletions.
2 changes: 1 addition & 1 deletion src/sage/calculus/calculus.py
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,7 @@ def laplace(ex, t, s):
629/8*e^(-4*t) + 91/8*e^(4*t)
sage: p1 = plot(xt,0,1/2,rgbcolor=(1,0,0))
sage: p2 = plot(yt,0,1/2,rgbcolor=(0,1,0))
sage: (p1+p2).save(SAGE_TMP + "de_plot.png")
sage: (p1+p2).save(os.path.join(SAGE_TMP, "de_plot.png"))
Another example::
Expand Down
2 changes: 1 addition & 1 deletion src/sage/combinat/words/morphism.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ def __call__(self, w, order=1, datatype='iter'):
<class 'sage.combinat.words.word.FiniteWord_iter_with_caching'>
sage: w == loads(dumps(w))
True
sage: save(w, filename=SAGE_TMP + 'test.sobj')
sage: save(w, filename=os.path.join(SAGE_TMP, 'test.sobj'))
One may impose the datatype of the resulting word::
Expand Down
2 changes: 1 addition & 1 deletion src/sage/geometry/polytope.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
if os.path.exists(path):
os.environ['PATH'] = '%s:'%path + os.environ['PATH']

tmp_file = '%s/tmp.poly'%SAGE_TMP
tmp_file = os.path.join(SAGE_TMP, 'tmp.poly')

class Polytope(SageObject):
"""
Expand Down
8 changes: 4 additions & 4 deletions src/sage/graphs/bipartite_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class BipartiteGraph(Graph):
5. From an alist file::
sage: file_name = SAGE_TMP + 'deleteme.alist.txt'
sage: file_name = os.path.join(SAGE_TMP, 'deleteme.alist.txt')
sage: fi = open(file_name, 'w')
sage: fi.write("7 4 \n 3 4 \n 3 3 1 3 1 1 1 \n 3 3 3 4 \n\
1 2 4 \n 1 3 4 \n 1 0 0 \n 2 3 4 \n\
Expand Down Expand Up @@ -903,7 +903,7 @@ def load_afile(self, fname):
EXAMPLE::
sage: file_name = SAGE_TMP + 'deleteme.alist.txt'
sage: file_name = os.path.join(SAGE_TMP, 'deleteme.alist.txt')
sage: fi = open(file_name, 'w')
sage: fi.write("7 4 \n 3 4 \n 3 3 1 3 1 1 1 \n 3 3 3 4 \n\
1 2 4 \n 1 3 4 \n 1 0 0 \n 2 3 4 \n\
Expand Down Expand Up @@ -999,15 +999,15 @@ def save_afile(self, fname):
[0 1 0 1 0 1 0]
[1 1 0 1 0 0 1]
sage: b = BipartiteGraph(M)
sage: file_name = SAGE_TMP + 'deleteme.alist.txt'
sage: file_name = os.path.join(SAGE_TMP, 'deleteme.alist.txt')
sage: b.save_afile(file_name)
sage: b2 = BipartiteGraph(file_name)
sage: b == b2
True
TESTS::
sage: file_name = SAGE_TMP + 'deleteme.alist.txt'
sage: file_name = os.path.join(SAGE_TMP, 'deleteme.alist.txt')
sage: for order in range(3, 13, 3):
... num_chks = int(order / 3)
... num_vars = order - num_chks
Expand Down
4 changes: 2 additions & 2 deletions src/sage/gsl/ode.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class ode_solver(object):
sage: T.function=f_1
sage: T.jacobian=j_1
sage: T.ode_solve(y_0=[1,0],t_span=[0,100],params=[10.0],num_points=1000)
sage: outfile = SAGE_TMP + 'sage.png'
sage: outfile = os.path.join(SAGE_TMP, 'sage.png')
sage: T.plot_solution(filename=outfile)
The solver line is equivalent to::
Expand Down Expand Up @@ -316,7 +316,7 @@ class ode_solver(object):
sage: vander = van_der_pol() # not tested
sage: T.function=vander # not tested
sage: T.ode_solve(y_0 = [1,0], t_span=[0,2000], num_points=1000) # not tested
sage: T.plot_solution(i=0, filename=SAGE_TMP + '/test.png') # not tested
sage: T.plot_solution(i=0, filename=os.path.join(SAGE_TMP, 'test.png')) # not tested
"""
Expand Down
4 changes: 2 additions & 2 deletions src/sage/interfaces/cleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
import os

import sage.misc.misc as misc
F = '%s/spawned_processes'%misc.SAGE_TMP

def cleaner(pid, cmd=''):
if cmd != '':
cmd = cmd.strip().split()[0]
# This is safe, since only this process writes to this file.
F = os.path.join(misc.SAGE_TMP, 'spawned_processes')
if os.path.exists(F):
o = open(F,'a')
else:
if not os.path.exists(misc.SAGE_TMP):
if not os.path.exists(str(misc.SAGE_TMP)):
return
o = open(F,'w')
o.write('%s %s\n'%(pid, cmd))
Expand Down
18 changes: 1 addition & 17 deletions src/sage/interfaces/expect.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,6 @@

failed_to_start = []

#tmp_expect_interface_local='%s/tmp'%SAGE_TMP_INTERFACE

#def tmp_expect_interface_local():
# return '%s/tmp'%SAGE_TMP_INTERFACE + str(os.getpid())

## On some platforms, e.g., windows, this can easily take 10 seconds!?! Terrible. And
## it should not be necessary or used anyways.
## def _absolute(cmd):
## c = cmd.split()
## s = c[0]
## t = os.popen('which %s'%s).read().strip()
## if len(t) == 0:
## raise RuntimeError
## return ' '.join([t] + c[1:])

# The subprocess is a shared resource. In a multi-threaded
# environment, there would have to be a lock to control access to the
# subprocess. Fortunately, Sage does not use Python threads.
Expand Down Expand Up @@ -613,11 +598,10 @@ def _local_tmpfile(self):
- Simon King (2010-09): Making the tmp-file unique for the interface instance
"""
#return '%s/tmp'%SAGE_TMP_INTERFACE + str(self.pid())
try:
return self.__local_tmpfile
except AttributeError:
self.__local_tmpfile = '%s/tmp'%SAGE_TMP_INTERFACE + str(self.pid())
self.__local_tmpfile = os.path.join(SAGE_TMP_INTERFACE, 'tmp' + str(self.pid()))
return self.__local_tmpfile

def _remote_tmpdir(self):
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/gap.py
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ def save_workspace(self):
workspace file contains more than 82 characters) is fixed::
sage: ORIGINAL_WORKSPACE = sage.interfaces.gap.WORKSPACE
sage: sage.interfaces.gap.WORKSPACE = SAGE_TMP + "gap" + "0"*(80-len(SAGE_TMP))
sage: sage.interfaces.gap.WORKSPACE = os.path.join(SAGE_TMP, "gap" + "0"*(80-len(SAGE_TMP)))
sage: gap = Gap()
sage: gap('3+2')
5
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/gnuplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def plot3d_parametric(self, f='cos(u)*(3 + v*cos(u/2)), sin(u)*(3 + v*cos(u/2)),

def interact(self, cmd):
from sage.misc.all import SAGE_TMP
file= '%s/gnuplot'%SAGE_TMP
file = os.path.join(SAGE_TMP, 'gnuplot')
open(file, 'w').write(cmd + '\n pause -1 "Press return to continue (no further rotation possible)"')
os.system('sage-native-execute gnuplot -persist %s'%file)

Expand Down
7 changes: 4 additions & 3 deletions src/sage/interfaces/magma.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,9 +1001,10 @@ def load(self, filename):
EXAMPLES::
sage: open(SAGE_TMP + 'a.m','w').write('function f(n) return n^2; end function;\nprint "hi";')
sage: print magma.load(SAGE_TMP + 'a.m') # optional - magma
Loading ".../.sage//temp/.../a.m"
sage: filename = os.path.join(SAGE_TMP, 'a.m')
sage: open(filename, 'w').write('function f(n) return n^2; end function;\nprint "hi";')
sage: print magma.load(filename) # optional - magma
Loading ".../tmp/.../a.m"
hi
sage: magma('f(12)') # optional - magma
144
Expand Down
5 changes: 2 additions & 3 deletions src/sage/libs/mwrank/mwrank.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ def get_precision():
sage: from sage.libs.mwrank.mwrank import get_precision
sage: get_precision()
50
50
"""
return mwrank_get_precision()

Expand Down Expand Up @@ -154,7 +153,7 @@ def initprimes(filename, verb=False):
EXAMPLES::
sage: file= SAGE_TMP + '/PRIMES'
sage: file = os.path.join(SAGE_TMP, 'PRIMES')
sage: open(file,'w').write(' '.join([str(p) for p in prime_range(10^7,10^7+20)]))
sage: mwrank_initprimes(file, verb=True)
Expand Down
2 changes: 1 addition & 1 deletion src/sage/matrix/matrix2.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -7333,7 +7333,7 @@ cdef class Matrix(matrix1.Matrix):
EXAMPLE::

sage: M = random_matrix(CC, 4)
sage: M.visualize_structure(SAGE_TMP + "matrix.png")
sage: M.visualize_structure(os.path.join(SAGE_TMP, "matrix.png"))
"""
import gd
import os
Expand Down
2 changes: 1 addition & 1 deletion src/sage/misc/cython.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ def cython(filename, verbose=False, compile_message=False,
# This is the *temporary* directory where we build the pyx file.
# This is deleted when sage exits, which means pyx files must be
# rebuilt every time Sage is restarted at present.
build_dir = '%s/%s'%(SPYX_TMP, base)
build_dir = os.path.join(SPYX_TMP, base)

if os.path.exists(build_dir):
# There is already a module here. Maybe we do not have to rebuild?
Expand Down
2 changes: 1 addition & 1 deletion src/sage/misc/dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def install_scripts(directory=None, ignore_existing=False):
EXAMPLES::
sage: install_scripts(SAGE_TMP, ignore_existing=True)
sage: install_scripts(str(SAGE_TMP), ignore_existing=True)
Checking that Sage has the command 'gap' installed
...
"""
Expand Down
2 changes: 1 addition & 1 deletion src/sage/misc/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -2112,7 +2112,7 @@ def png(x, filename, density=150, debug=False,
EXAMPLES::
sage: from sage.misc.latex import png
sage: png(ZZ[x], SAGE_TMP + "zz.png") # random - error if no latex
sage: png(ZZ[x], os.path.join(SAGE_TMP, "zz.png")) # random - error if no latex
"""
if not pdflatex:
engine = "latex"
Expand Down
7 changes: 3 additions & 4 deletions src/sage/misc/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,14 @@ def sage_makedirs(dir):

#################################################
# Next we create the Sage temporary directory.
# It is called temp instead of tmp mainly for
# "historical reasons"...
#################################################

SAGE_TMP = os.path.join(DOT_SAGE, 'tmp', HOSTNAME, str(os.getpid()))
sage_makedirs(SAGE_TMP)

SPYX_TMP = os.path.join(SAGE_TMP, 'spyx/')
SPYX_TMP = os.path.join(SAGE_TMP, 'spyx')

SAGE_TMP_INTERFACE = os.path.join(SAGE_TMP, 'interface/')
SAGE_TMP_INTERFACE = os.path.join(SAGE_TMP, 'interface')
sage_makedirs(SAGE_TMP_INTERFACE)

SAGE_DB = os.path.join(DOT_SAGE, 'db')
Expand Down
4 changes: 2 additions & 2 deletions src/sage/misc/sageinspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1430,9 +1430,9 @@ def sage_getsourcelines(obj, is_binary=False):
source_lines = open(filename).readlines()
except IOError:
try:
from sage.all import SAGE_TMP
from sage.misc.misc import SPYX_TMP
raw_name = filename.split('/')[-1]
newname = SAGE_TMP+'/spyx/'+'_'.join(raw_name.split('_')[:-1])+'/'+raw_name
newname = os.path.join(SPYX_TMP, '_'.join(raw_name.split('_')[:-1]), raw_name)
source_lines = open(newname).readlines()
except IOError:
return None
Expand Down
6 changes: 3 additions & 3 deletions src/sage/misc/session.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ We next save this session. We are using a file in SAGE_TMP. We do this
save your session permanently, since SAGE_TMP will be removed when
leaving Sage!::
sage: save_session(SAGE_TMP+'session')
sage: save_session(os.path.join(SAGE_TMP, 'session'))
This saves a dictionary with $w$ as one of the keys::
sage: z = load(SAGE_TMP+'session')
sage: z = load(os.path.join(SAGE_TMP, 'session'))
sage: z.keys()
['w']
sage: z['w']
Expand All @@ -32,7 +32,7 @@ Next we reset the session, verify this, and load the session back.::
sage: reset()
sage: show_identifiers()
[]
sage: load_session(SAGE_TMP+'session')
sage: load_session(os.path.join(SAGE_TMP, 'session'))
Indeed $w$ is now defined again.::
Expand Down
4 changes: 2 additions & 2 deletions src/sage/numerical/backends/coin_backend.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ cdef class CoinBackend(GenericBackend):
1
sage: p.add_linear_constraint([(0, 1), (1, 2)], None, 3) # optional - Coin
sage: p.set_objective([2, 5]) # optional - Coin
sage: p.write_mps(SAGE_TMP+"/lp_problem.mps", 0) # optional - Coin
sage: p.write_mps(os.path.join(SAGE_TMP, "lp_problem.mps"), 0) # optional - Coin
"""

cdef char * mps = "mps"
Expand All @@ -1057,7 +1057,7 @@ cdef class CoinBackend(GenericBackend):
1
sage: p.add_linear_constraint([(0, 1), (1, 2)], None, 3) # optional - Coin
sage: p.set_objective([2, 5]) # optional - Coin
sage: p.write_lp(SAGE_TMP+"/lp_problem.lp") # optional - Coin
sage: p.write_lp(os.path.join(SAGE_TMP, "lp_problem.lp")) # optional - Coin
"""

cdef char * lp = "lp"
Expand Down
4 changes: 2 additions & 2 deletions src/sage/numerical/backends/cplex_backend.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1274,7 +1274,7 @@ cdef class CPLEXBackend(GenericBackend):
1
sage: p.add_linear_constraint([(0, 1), (1, 2)], None, 3) # optional - CPLEX
sage: p.set_objective([2, 5]) # optional - CPLEX
sage: p.write_lp(SAGE_TMP+"/lp_problem.lp") # optional - CPLEX
sage: p.write_lp(os.path.join(SAGE_TMP, "lp_problem.lp")) # optional - CPLEX
"""

cdef int status
Expand All @@ -1298,7 +1298,7 @@ cdef class CPLEXBackend(GenericBackend):
1
sage: p.add_linear_constraint([(0, 1), (1, 2)], None, 3) # optional - CPLEX
sage: p.set_objective([2, 5]) # optional - CPLEX
sage: p.write_lp(SAGE_TMP+"/lp_problem.lp") # optional - CPLEX
sage: p.write_lp(os.path.join(SAGE_TMP, "lp_problem.lp")) # optional - CPLEX
"""

cdef int status
Expand Down
4 changes: 2 additions & 2 deletions src/sage/numerical/backends/generic_backend.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ cdef class GenericBackend:
2
sage: p.add_linear_constraint([(0, 1], (1, 2)], None, 3) # optional - Nonexistent_LP_solver
sage: p.set_objective([2, 5]) # optional - Nonexistent_LP_solver
sage: p.write_lp(SAGE_TMP+"/lp_problem.lp") # optional - Nonexistent_LP_solver
sage: p.write_lp(os.path.join(SAGE_TMP, "lp_problem.lp")) # optional - Nonexistent_LP_solver
"""
raise NotImplementedError()

Expand All @@ -574,7 +574,7 @@ cdef class GenericBackend:
2
sage: p.add_linear_constraint([(0, 1), (1, 2)], None, 3) # optional - Nonexistent_LP_solver
sage: p.set_objective([2, 5]) # optional - Nonexistent_LP_solver
sage: p.write_lp(SAGE_TMP+"/lp_problem.lp") # optional - Nonexistent_LP_solver
sage: p.write_lp(os.path.join(SAGE_TMP, "lp_problem.lp")) # optional - Nonexistent_LP_solver
"""
raise NotImplementedError()

Expand Down
4 changes: 2 additions & 2 deletions src/sage/numerical/backends/glpk_backend.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ cdef class GLPKBackend(GenericBackend):
1
sage: p.add_linear_constraint([[0, 1], [1, 2]], None, 3)
sage: p.set_objective([2, 5])
sage: p.write_lp(SAGE_TMP+"/lp_problem.lp")
sage: p.write_lp(os.path.join(SAGE_TMP, "lp_problem.lp"))
"""
glp_write_lp(self.lp, NULL, filename)

Expand All @@ -1231,7 +1231,7 @@ cdef class GLPKBackend(GenericBackend):
1
sage: p.add_linear_constraint([[0, 1], [1, 2]], None, 3)
sage: p.set_objective([2, 5])
sage: p.write_lp(SAGE_TMP+"/lp_problem.lp")
sage: p.write_lp(os.path.join(SAGE_TMP, "lp_problem.lp"))
"""
glp_write_mps(self.lp, modern, NULL, filename)

Expand Down
4 changes: 2 additions & 2 deletions src/sage/numerical/backends/gurobi_backend.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ cdef class GurobiBackend(GenericBackend):
1
sage: p.add_linear_constraint([[0, 1], [1, 2]], None, 3) # optional - Gurobi
sage: p.set_objective([2, 5]) # optional - Gurobi
sage: p.write_lp(SAGE_TMP+"/lp_problem.lp") # optional - Gurobi
sage: p.write_lp(os.path.join(SAGE_TMP, "lp_problem.lp")) # optional - Gurobi
"""
check(self.env, GRBwrite(self.model[0], filename))

Expand All @@ -1121,7 +1121,7 @@ cdef class GurobiBackend(GenericBackend):
1
sage: p.add_linear_constraint([[0, 1], [1, 2]], None, 3) # optional - Gurobi
sage: p.set_objective([2, 5]) # optional - Gurobi
sage: p.write_lp(SAGE_TMP+"/lp_problem.lp") # optional - Gurobi
sage: p.write_lp(os.path.join(SAGE_TMP, "lp_problem.lp")) # optional - Gurobi
"""
check(self.env, GRBwrite(self.model[0], filename))

Expand Down
4 changes: 2 additions & 2 deletions src/sage/numerical/mip.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ cdef class MixedIntegerLinearProgram:
sage: x = p.new_variable()
sage: p.set_objective(x[1] + x[2])
sage: p.add_constraint(-3*x[1] + 2*x[2], max=2,name="OneConstraint")
sage: p.write_mps(SAGE_TMP+"/lp_problem.mps")
sage: p.write_mps(os.path.join(SAGE_TMP, "lp_problem.mps"))
For information about the MPS file format :
http://en.wikipedia.org/wiki/MPS_%28format%29
Expand All @@ -749,7 +749,7 @@ cdef class MixedIntegerLinearProgram:
sage: x = p.new_variable()
sage: p.set_objective(x[1] + x[2])
sage: p.add_constraint(-3*x[1] + 2*x[2], max=2)
sage: p.write_lp(SAGE_TMP+"/lp_problem.lp")
sage: p.write_lp(os.path.join(SAGE_TMP, "lp_problem.lp"))
For more information about the LP file format :
http://lpsolve.sourceforge.net/5.5/lp-format.htm
Expand Down
2 changes: 1 addition & 1 deletion src/sage/plot/plot3d/base.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ end_scene""" % (render_params.antialiasing,
# Tachyon resolution options
if DOCTEST_MODE:
opts = '-res 10 10'
filename = sage.misc.misc.SAGE_TMP + "/tmp"
filename = os.path.join(sage.misc.misc.SAGE_TMP, "tmp")
elif EMBEDDED_MODE:
opts = '-res %s %s'%(figsize[0]*100, figsize[1]*100)
filename = sage.misc.temporary_file.graphics_filename()[:-4]
Expand Down
Loading

0 comments on commit 5b42d86

Please sign in to comment.