Skip to content

Commit

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

URL: https://trac.sagemath.org/33779
Reported by: mkoeppe
Ticket author(s): Michael Orlitzky
Reviewer(s): Matthias Koeppe
  • Loading branch information
Release Manager committed May 22, 2022
2 parents b376a8d + 21214c3 commit 833f53d
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/sage/interfaces/latte.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from sage.cpython.string import str_to_bytes, bytes_to_str

from subprocess import Popen, PIPE
from sage.misc.misc import SAGE_TMP
from sage.rings.integer import Integer
from sage.features.latte import Latte_count, Latte_integrate

Expand Down Expand Up @@ -156,10 +155,13 @@ def count(arg, ehrhart_polynomial=False, multivariate_generating_function=False,

# The cwd argument is needed because latte
# always produces diagnostic output files.
import tempfile
tempd = tempfile.TemporaryDirectory()

latte_proc = Popen(args,
stdin=PIPE, stdout=PIPE,
stderr=(None if verbose else PIPE),
cwd=str(SAGE_TMP))
cwd=tempd.name)

ans, err = latte_proc.communicate(arg)
if err:
Expand All @@ -174,19 +176,26 @@ def count(arg, ehrhart_polynomial=False, multivariate_generating_function=False,

ans = bytes_to_str(ans)

# There's an error handler below that uses the numOfLatticePoints
# file created by latte, so we can't cleanup() the temporary
# directory here. Instead we have to clean it up before the
# (several) return statements.
if ehrhart_polynomial:
ans = ans.splitlines()[-2]
if raw_output:
tempd.cleanup()
return ans
else:
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
from sage.rings.rational_field import QQ
R = PolynomialRing(QQ, 't')
tempd.cleanup()
return R(ans)
elif multivariate_generating_function:
with open(filename + '.rat') as f:
ans = f.read()
if raw_output:
tempd.cleanup()
return ans
else:
raise NotImplementedError("there is no Sage object to handle multivariate series from LattE, use raw_output=True")
Expand All @@ -196,12 +205,14 @@ def count(arg, ehrhart_polynomial=False, multivariate_generating_function=False,
if not ans:
# opening a file is slow (30e-6s), so we read the file
# numOfLatticePoints only in case of a IndexError above
with open(SAGE_TMP+'/numOfLatticePoints', 'r') as f:
with open(tempd.name+'/numOfLatticePoints', 'r') as f:
ans = f.read()

if raw_output:
tempd.cleanup()
return ans
else:
tempd.cleanup()
return Integer(ans)


Expand Down Expand Up @@ -376,10 +387,13 @@ def integrate(arg, polynomial=None, algorithm='triangulate', raw_output=False, v

# The cwd argument is needed because latte
# always produces diagnostic output files.
import tempfile
tempd = tempfile.TemporaryDirectory()

latte_proc = Popen(args,
stdin=PIPE, stdout=PIPE,
stderr=(None if verbose else PIPE),
cwd=str(SAGE_TMP))
cwd=tempd.name)

ans, err = latte_proc.communicate(arg)
if err:
Expand All @@ -398,6 +412,7 @@ def integrate(arg, polynomial=None, algorithm='triangulate', raw_output=False, v
assert(ans[0]=='Answer:')
ans = ans[1]

tempd.cleanup()
if raw_output:
return ans
else:
Expand Down

0 comments on commit 833f53d

Please sign in to comment.