From 7947b77856219d2efca1079c8fbd092ca5395a15 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Mon, 14 Feb 2022 13:02:23 -0500 Subject: [PATCH] Trac #33213: replace SAGE_TMP in repl doctests. --- src/sage/repl/attach.py | 19 +++++++++++-------- src/sage/repl/ipython_extension.py | 20 +++++++++++--------- 2 files changed, 22 insertions(+), 17 deletions(-) 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))