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

Commit

Permalink
Trac #33793: replace SAGE_TMP in SPYX_TMP.
Browse files Browse the repository at this point in the history
With the move to a real temporary directory, we now also install an
atexit hook to clean up the tempdir that is created. Moreover
SPYX_TMP() is renamed to a new a @cached_function spyx_tmp() so that
only one directory will be created, and only if it's used.

The old SPYX_TMP path is marked with a deprecation warning.
  • Loading branch information
orlitzky committed May 4, 2022
1 parent eb1a786 commit 202485f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/sage/misc/cython.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

from sage.env import (SAGE_LOCAL, cython_aliases,
sage_include_directories)
from sage.misc.misc import SPYX_TMP
from sage.misc.misc import spyx_tmp
from .temporary_file import tmp_filename
from sage.repl.user_globals import get_globals
from sage.misc.sage_ostools import restore_cwd, redirection
Expand Down Expand Up @@ -229,9 +229,9 @@ def cython(filename, verbose=0, compile_message=False,
base = sanitize(base)

# This is the *temporary* directory where we store the pyx file.
# This is deleted when Sage exits, which means pyx files must be
# rebuilt every time Sage is restarted at present.
target_dir = os.path.join(SPYX_TMP, base)
# spyx_tmp changes when we start Sage, so old (but not stale) pyx
# files must be rebuilt at the moment.
target_dir = os.path.join(spyx_tmp(), base)

# Build directory for Cython/distutils
build_dir = os.path.join(target_dir, "build")
Expand Down
30 changes: 27 additions & 3 deletions src/sage/misc/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

from .lazy_string import lazy_string
from sage.env import DOT_SAGE, HOSTNAME
from sage.misc.cachefunc import cached_function
from sage.misc.lazy_import import lazy_import

lazy_import("sage.misc.call", ["AttrCallObject", "attrcall", "call_method"],
Expand Down Expand Up @@ -247,15 +248,38 @@ def ECL_TMP():

@lazy_string
def SPYX_TMP():
"""
r"""
EXAMPLES::
sage: from sage.misc.misc import SPYX_TMP
sage: SPYX_TMP
l'.../temp/.../spyx'
doctest:warning...
DeprecationWarning: SPYX_TMP is deprecated;
use sage.misc.misc.spyx_tmp instead
See https://trac.sagemath.org/33213 for details.
...
"""
return os.path.join(str(SAGE_TMP), 'spyx')
from sage.misc.superseded import deprecation
deprecation(33213, "SPYX_TMP is deprecated; use sage.misc.misc.spyx_tmp instead")
return spyx_tmp()


@cached_function
def spyx_tmp():
r"""
The temporary directory used to store pyx files.
We use a cached function for this so that the same temporary
directory will always be returned (unless the user shoots himself
in the foot). Each temporary directory created is removed when
sage terminates using an atexit hook.
"""
import atexit, tempfile
d = tempfile.TemporaryDirectory()
result = os.path.join(d.name, 'spyx')
atexit.register(lambda: d.cleanup())
return result

@lazy_string
def SAGE_TMP_INTERFACE():
Expand Down
15 changes: 8 additions & 7 deletions src/sage/misc/sageinspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,10 @@ def _extract_embedded_position(docstring):
....: print(f.read())
cpdef test_funct(x,y): return
Ensure that the embedded filename of the compiled function is correct. In
particular it should be relative to ``SPYX_TMP`` in order for certain
documentation functions to work properly. See :trac:`24097`::
Ensure that the embedded filename of the compiled function is
correct. In particular it should be relative to ``spyx_tmp()`` in
order for certain documentation functions to work properly. See
:trac:`24097`::
sage: from sage.env import DOT_SAGE
sage: from sage.misc.sage_ostools import restore_cwd
Expand Down Expand Up @@ -293,10 +294,10 @@ def _extract_embedded_position(docstring):
# Try some common path prefixes for Cython modules built by/for Sage
# 1) Module in the sage src tree
# 2) Module compiled by Sage's inline cython() compiler
from sage.misc.misc import SPYX_TMP
from sage.misc.misc import spyx_tmp
try_filenames = [
os.path.join(SAGE_LIB, raw_filename),
os.path.join(SPYX_TMP, '_'.join(raw_filename.split('_')[:-1]),
os.path.join(spyx_tmp(), '_'.join(raw_filename.split('_')[:-1]),
raw_filename)
]
for try_filename in try_filenames:
Expand Down Expand Up @@ -2430,9 +2431,9 @@ class Element(object):
source_lines = f.readlines()
except IOError:
try:
from sage.misc.misc import SPYX_TMP
from sage.misc.misc import spyx_tmp
raw_name = filename.split('/')[-1]
newname = os.path.join(SPYX_TMP, '_'.join(raw_name.split('_')[:-1]), raw_name)
newname = os.path.join(spyx_tmp(), '_'.join(raw_name.split('_')[:-1]), raw_name)
with open(newname) as f:
source_lines = f.readlines()
except IOError:
Expand Down

0 comments on commit 202485f

Please sign in to comment.