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

Commit

Permalink
Trac #33213: replace SAGE_TMP in repl doctests.
Browse files Browse the repository at this point in the history
  • Loading branch information
orlitzky committed Feb 15, 2022
1 parent e44917e commit dabb1c7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
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 direcoty 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 dabb1c7

Please sign in to comment.