diff --git a/src/sage/doctest/control.py b/src/sage/doctest/control.py index faae91590b6..a9d11d0863e 100644 --- a/src/sage/doctest/control.py +++ b/src/sage/doctest/control.py @@ -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 diff --git a/src/sage/doctest/forker.py b/src/sage/doctest/forker.py index 94347db180c..39967cfbbe6 100644 --- a/src/sage/doctest/forker.py +++ b/src/sage/doctest/forker.py @@ -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: @@ -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 diff --git a/src/sage/misc/persist.pyx b/src/sage/misc/persist.pyx index bdc84445e7f..b71d7a32fc5 100644 --- a/src/sage/misc/persist.pyx +++ b/src/sage/misc/persist.pyx @@ -237,9 +237,11 @@ 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] @@ -247,26 +249,28 @@ def save(obj, filename, compress=True, **kwargs): 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) """ diff --git a/src/sage/misc/sage_ostools.pyx b/src/sage/misc/sage_ostools.pyx index 88227c8cfbe..678b6731b82 100644 --- a/src/sage/misc/sage_ostools.pyx +++ b/src/sage/misc/sage_ostools.pyx @@ -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() diff --git a/src/sage/repl/attach.py b/src/sage/repl/attach.py index 8cbe64c1f94..39da6ee4acd 100644 --- a/src/sage/repl/attach.py +++ b/src/sage/repl/attach.py @@ -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 @@ -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 diff --git a/src/sage/repl/ipython_extension.py b/src/sage/repl/ipython_extension.py index 65d9522939c..eb508b067f5 100644 --- a/src/sage/repl/ipython_extension.py +++ b/src/sage/repl/ipython_extension.py @@ -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: @@ -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))