Skip to content

Commit

Permalink
Trac #33793: sage.misc.cython: Replace use of SPYX_TMP by a new cache…
Browse files Browse the repository at this point in the history
…d function in sage.misc.temporary_file

(split out from #33213)

URL: https://trac.sagemath.org/33793
Reported by: mkoeppe
Ticket author(s): Michael Orlitzky
Reviewer(s): Matthias Koeppe
  • Loading branch information
Release Manager committed May 22, 2022
2 parents d115270 + fc25b79 commit 7037fba
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
9 changes: 4 additions & 5 deletions src/sage/misc/cython.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@

from sage.env import (SAGE_LOCAL, cython_aliases,
sage_include_directories)
from sage.misc.misc import SPYX_TMP
from .temporary_file import tmp_filename
from .temporary_file import spyx_tmp, tmp_filename
from sage.repl.user_globals import get_globals
from sage.misc.sage_ostools import restore_cwd, redirection
from sage.cpython.string import str_to_bytes
Expand Down Expand Up @@ -229,9 +228,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
15 changes: 12 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,14 +248,22 @@ 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.temporary_file.spyx_tmp instead
See https://trac.sagemath.org/33213 for details.
...
"""
return os.path.join(str(SAGE_TMP), 'spyx')
from sage.misc.temporary_file import spyx_tmp
from sage.misc.superseded import deprecation
deprecation(33213, "SPYX_TMP is deprecated; use sage.misc.temporary_file.spyx_tmp instead")
return spyx_tmp()


@lazy_string
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.temporary_file 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.temporary_file 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
22 changes: 22 additions & 0 deletions src/sage/misc/temporary_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io
import os
import tempfile

import atexit


Expand Down Expand Up @@ -543,3 +544,24 @@ def __exit__(self, exc_type, exc_val, exc_tb):
else:
# Failure: delete temporary file
shutil.rmtree(self.tempname)


_spyx_tmp = None
def spyx_tmp():
r"""
The temporary directory used to store pyx files.
We cache the result of this function "by hand" so that the same
temporary directory will always be returned. A function is used to
delay creating a directory until (if) it is needed. The temporary
directory is removed when sage terminates by way of an atexit
hook.
"""
global _spyx_tmp
if _spyx_tmp:
return _spyx_tmp

d = tempfile.TemporaryDirectory()
_spyx_tmp = os.path.join(d.name, 'spyx')
atexit.register(lambda: d.cleanup())
return _spyx_tmp

0 comments on commit 7037fba

Please sign in to comment.