Skip to content

Commit

Permalink
Trac #33799: Replace SAGE_TMP in doctests of sage.misc.{persist,ostoo…
Browse files Browse the repository at this point in the history
…ls}, sage.doctest, sage.repl

(split out from #33213)

URL: https://trac.sagemath.org/33799
Reported by: mkoeppe
Ticket author(s): Michael Orlitzky
Reviewer(s): Matthias Koeppe
  • Loading branch information
Release Manager committed May 22, 2022
2 parents a3fd718 + 7947b77 commit 3e6b41f
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 58 deletions.
13 changes: 7 additions & 6 deletions src/sage/doctest/control.py
Expand Up @@ -758,13 +758,14 @@ def add_files(self):
EXAMPLES::
sage: from sage.doctest.control import DocTestDefaults, DocTestController
sage: from sage.doctest.control import (DocTestDefaults,
....: DocTestController)
sage: from sage.env import SAGE_SRC
sage: import os
sage: log_location = os.path.join(SAGE_TMP, 'control_dt_log.log')
sage: DD = DocTestDefaults(all=True, logfile=log_location)
sage: DC = DocTestController(DD, [])
sage: DC.add_files()
sage: import tempfile
sage: with tempfile.NamedTemporaryFile() as f:
....: DD = DocTestDefaults(all=True, logfile=f.name)
....: DC = DocTestController(DD, [])
....: DC.add_files()
Doctesting ...
sage: os.path.join(SAGE_SRC, 'sage') in DC.files
True
Expand Down
42 changes: 22 additions & 20 deletions src/sage/doctest/forker.py
Expand Up @@ -1704,25 +1704,26 @@ def parallel_dispatch(self):
module will be immediately printed and any other ongoing tests
canceled::
sage: test1 = os.path.join(SAGE_TMP, 'test1.py')
sage: test2 = os.path.join(SAGE_TMP, 'test2.py')
sage: with open(test1, 'w') as f:
....: _ = f.write("'''\nsage: import time; time.sleep(60)\n'''")
sage: with open(test2, 'w') as f:
....: _ = f.write("'''\nsage: True\nFalse\n'''")
sage: DC = DocTestController(DocTestDefaults(exitfirst=True,
....: nthreads=2),
....: [test1, test2])
sage: DC.expand_files_into_sources()
sage: DD = DocTestDispatcher(DC)
sage: DR = DocTestReporter(DC)
sage: DC.reporter = DR
sage: DC.dispatcher = DD
sage: DC.timer = Timer().start()
sage: DD.parallel_dispatch()
sage -t .../test2.py
sage: from tempfile import NamedTemporaryFile as NTF
sage: with ( NTF(suffix=".py", mode="w+t") as f1,
....: NTF(suffix=".py", mode="w+t") as f2 ):
....: _ = f1.write("'''\nsage: import time; time.sleep(60)\n'''")
....: f1.flush()
....: _ = f2.write("'''\nsage: True\nFalse\n'''")
....: f2.flush()
....: DC = DocTestController(DocTestDefaults(exitfirst=True,
....: nthreads=2),
....: [f1.name, f2.name])
....: DC.expand_files_into_sources()
....: DD = DocTestDispatcher(DC)
....: DR = DocTestReporter(DC)
....: DC.reporter = DR
....: DC.dispatcher = DD
....: DC.timer = Timer().start()
....: DD.parallel_dispatch()
sage -t ...
**********************************************************************
File ".../test2.py", line 2, in test2
File "...", line 2, in ...
Failed example:
True
Expected:
Expand All @@ -1731,9 +1732,10 @@ def parallel_dispatch(self):
True
**********************************************************************
1 item had failures:
1 of 1 in test2
1 of 1 in ...
[1 test, 1 failure, ... s]
Killing test .../test1.py
Killing test ...
"""
opt = self.controller.options

Expand Down
20 changes: 12 additions & 8 deletions src/sage/misc/persist.pyx
Expand Up @@ -237,36 +237,40 @@ def save(obj, filename, compress=True, **kwargs):
EXAMPLES::
sage: import tempfile
sage: d = tempfile.TemporaryDirectory()
sage: a = matrix(2, [1,2,3,-5/2])
sage: objfile = os.path.join(SAGE_TMP, 'test.sobj')
sage: objfile_short = os.path.join(SAGE_TMP, 'test')
sage: objfile = os.path.join(d.name, 'test.sobj')
sage: objfile_short = os.path.join(d.name, 'test')
sage: save(a, objfile)
sage: load(objfile_short)
[ 1 2]
[ 3 -5/2]
sage: E = EllipticCurve([-1,0])
sage: P = plot(E)
sage: save(P, objfile_short) # saves the plot to "test.sobj"
sage: save(P, filename=os.path.join(SAGE_TMP, "sage.png"), xmin=-2)
sage: save(P, os.path.join(SAGE_TMP, "filename.with.some.wrong.ext"))
sage: save(P, filename=os.path.join(d.name, "sage.png"), xmin=-2)
sage: save(P, os.path.join(d.name, "filename.with.some.wrong.ext"))
Traceback (most recent call last):
...
ValueError: allowed file extensions for images are '.eps', '.pdf', '.pgf', '.png', '.ps', '.sobj', '.svg'!
sage: print(load(objfile))
Graphics object consisting of 2 graphics primitives
sage: save("A python string", os.path.join(SAGE_TMP, 'test'))
sage: save("A python string", os.path.join(d.name, 'test'))
sage: load(objfile)
'A python string'
sage: load(objfile_short)
'A python string'
sage: d.cleanup()
TESTS:
Check that :trac:`11577` is fixed::
sage: filename = os.path.join(SAGE_TMP, "foo.bar") # filename containing a dot
sage: save((1,1),filename) # saves tuple to "foo.bar.sobj"
sage: load(filename)
sage: import tempfile
sage: with tempfile.NamedTemporaryFile(suffix=".bar") as f:
....: save((1,1), f.name)
....: load(f.name)
(1, 1)
"""

Expand Down
14 changes: 7 additions & 7 deletions src/sage/misc/sage_ostools.pyx
Expand Up @@ -62,14 +62,14 @@ def restore_cwd(chdir=None):
EXAMPLES::
sage: import os
sage: from sage.misc.sage_ostools import restore_cwd
sage: from sage.misc.misc import SAGE_TMP
sage: cwd = os.getcwd()
sage: with restore_cwd(str(SAGE_TMP)):
....: print(os.getcwd() == os.path.realpath(SAGE_TMP))
True
sage: cwd == os.getcwd()
sage: import tempfile
sage: orig_cwd = os.getcwd()
sage: with tempfile.TemporaryDirectory() as d:
....: with restore_cwd(d):
....: print(os.getcwd() == orig_cwd)
False
sage: os.getcwd() == orig_cwd
True
"""
orig_cwd = os.getcwd()
Expand Down
19 changes: 11 additions & 8 deletions src/sage/repl/attach.py
Expand Up @@ -170,12 +170,14 @@ def load_attach_path(path=None, replace=False):
sage: with open(fullpath, 'w') as f:
....: _ = f.write("print(37 * 3)")
We put ``SAGE_TMP`` on the attach path for testing (otherwise this will
load ``test.py`` from the current working directory if that happens
to exist)::
sage: load_attach_path(SAGE_TMP, replace=True)
sage: attach('test.py')
We put a new, empty directory on the attach path for testing
(otherwise this will load ``test.py`` from the current working
directory if that happens to exist)::
sage: import tempfile
sage: with tempfile.TemporaryDirectory() as d:
....: load_attach_path(d, replace=True)
....: attach('test.py')
Traceback (most recent call last):
...
OSError: did not find file 'test.py' to load or attach
Expand All @@ -187,8 +189,9 @@ def load_attach_path(path=None, replace=False):
sage: sage.repl.attach.reset(); reset_load_attach_path()
sage: load_attach_path() == ['.']
True
sage: load_attach_path(SAGE_TMP, replace=True)
sage: load('test.py')
sage: with tempfile.TemporaryDirectory() as d:
....: load_attach_path(d, replace=True)
....: load('test.py')
Traceback (most recent call last):
...
OSError: did not find file 'test.py' to load or attach
Expand Down
20 changes: 11 additions & 9 deletions src/sage/repl/ipython_extension.py
Expand Up @@ -38,12 +38,14 @@
sage: from sage.misc.temporary_file import tmp_dir
sage: shell = get_test_shell()
sage: TMP = tmp_dir()
sage: TMP = os.path.join(TMP, "12345", "temp")
sage: os.makedirs(TMP)
The temporary directory should have a name of the form
``.../12345/...``, to demonstrate that file names are not
preparsed when calling ``%runfile`` ::
sage: bool(re.search('/[0-9]+/', TMP))
sage: bool(re.search('/12345/', TMP))
True
sage: tmp = os.path.join(TMP, 'run_cell.py')
sage: with open(tmp, 'w') as f:
Expand Down Expand Up @@ -130,30 +132,30 @@ def attach(self, s):
EXAMPLES::
sage: import os
sage: from sage.repl.interpreter import get_test_shell
sage: shell = get_test_shell()
sage: tmp = os.path.normpath(os.path.join(SAGE_TMP, 'run_cell.py'))
sage: with open(tmp, 'w') as f: _ = f.write('a = 2\n')
sage: shell.run_cell('%attach ' + tmp)
sage: from tempfile import NamedTemporaryFile as NTF
sage: with NTF(mode="w+t", suffix=".py", delete=False) as f:
....: _ = f.write('a = 2\n')
sage: shell.run_cell('%attach ' + f.name)
sage: shell.run_cell('a')
2
sage: sleep(1) # filesystem timestamp granularity
sage: with open(tmp, 'w') as f: _ = f.write('a = 3\n')
sage: with open(f.name, 'w') as f: _ = f.write('a = 3\n')
Note that the doctests are never really at the command prompt, so
we call the input hook manually::
sage: shell.run_cell('from sage.repl.attach import reload_attached_files_if_modified')
sage: shell.run_cell('reload_attached_files_if_modified()')
### reloading attached file run_cell.py modified at ... ###
### reloading attached file ... modified at ... ###
sage: shell.run_cell('a')
3
sage: shell.run_cell('detach(%r)'%tmp)
sage: shell.run_cell('detach(%r)' % f.name)
sage: shell.run_cell('attached_files()')
[]
sage: os.remove(tmp)
sage: os.remove(f.name)
sage: shell.quit()
"""
return self.shell.ex(load_wrap(s, attach=True))
Expand Down

0 comments on commit 3e6b41f

Please sign in to comment.