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

Commit 7947b77

Browse files
orlitzkyMatthias Koeppe
authored and
Matthias Koeppe
committed
Trac #33213: replace SAGE_TMP in repl doctests.
1 parent 6ecf3e3 commit 7947b77

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

src/sage/repl/attach.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,14 @@ def load_attach_path(path=None, replace=False):
170170
sage: with open(fullpath, 'w') as f:
171171
....: _ = f.write("print(37 * 3)")
172172
173-
We put ``SAGE_TMP`` on the attach path for testing (otherwise this will
174-
load ``test.py`` from the current working directory if that happens
175-
to exist)::
176-
177-
sage: load_attach_path(SAGE_TMP, replace=True)
178-
sage: attach('test.py')
173+
We put a new, empty directory on the attach path for testing
174+
(otherwise this will load ``test.py`` from the current working
175+
directory if that happens to exist)::
176+
177+
sage: import tempfile
178+
sage: with tempfile.TemporaryDirectory() as d:
179+
....: load_attach_path(d, replace=True)
180+
....: attach('test.py')
179181
Traceback (most recent call last):
180182
...
181183
OSError: did not find file 'test.py' to load or attach
@@ -187,8 +189,9 @@ def load_attach_path(path=None, replace=False):
187189
sage: sage.repl.attach.reset(); reset_load_attach_path()
188190
sage: load_attach_path() == ['.']
189191
True
190-
sage: load_attach_path(SAGE_TMP, replace=True)
191-
sage: load('test.py')
192+
sage: with tempfile.TemporaryDirectory() as d:
193+
....: load_attach_path(d, replace=True)
194+
....: load('test.py')
192195
Traceback (most recent call last):
193196
...
194197
OSError: did not find file 'test.py' to load or attach

src/sage/repl/ipython_extension.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@
3838
sage: from sage.misc.temporary_file import tmp_dir
3939
sage: shell = get_test_shell()
4040
sage: TMP = tmp_dir()
41+
sage: TMP = os.path.join(TMP, "12345", "temp")
42+
sage: os.makedirs(TMP)
4143
4244
The temporary directory should have a name of the form
4345
``.../12345/...``, to demonstrate that file names are not
4446
preparsed when calling ``%runfile`` ::
4547
46-
sage: bool(re.search('/[0-9]+/', TMP))
48+
sage: bool(re.search('/12345/', TMP))
4749
True
4850
sage: tmp = os.path.join(TMP, 'run_cell.py')
4951
sage: with open(tmp, 'w') as f:
@@ -130,30 +132,30 @@ def attach(self, s):
130132
131133
EXAMPLES::
132134
133-
sage: import os
134135
sage: from sage.repl.interpreter import get_test_shell
135136
sage: shell = get_test_shell()
136-
sage: tmp = os.path.normpath(os.path.join(SAGE_TMP, 'run_cell.py'))
137-
sage: with open(tmp, 'w') as f: _ = f.write('a = 2\n')
138-
sage: shell.run_cell('%attach ' + tmp)
137+
sage: from tempfile import NamedTemporaryFile as NTF
138+
sage: with NTF(mode="w+t", suffix=".py", delete=False) as f:
139+
....: _ = f.write('a = 2\n')
140+
sage: shell.run_cell('%attach ' + f.name)
139141
sage: shell.run_cell('a')
140142
2
141143
sage: sleep(1) # filesystem timestamp granularity
142-
sage: with open(tmp, 'w') as f: _ = f.write('a = 3\n')
144+
sage: with open(f.name, 'w') as f: _ = f.write('a = 3\n')
143145
144146
Note that the doctests are never really at the command prompt, so
145147
we call the input hook manually::
146148
147149
sage: shell.run_cell('from sage.repl.attach import reload_attached_files_if_modified')
148150
sage: shell.run_cell('reload_attached_files_if_modified()')
149-
### reloading attached file run_cell.py modified at ... ###
151+
### reloading attached file ... modified at ... ###
150152
151153
sage: shell.run_cell('a')
152154
3
153-
sage: shell.run_cell('detach(%r)'%tmp)
155+
sage: shell.run_cell('detach(%r)' % f.name)
154156
sage: shell.run_cell('attached_files()')
155157
[]
156-
sage: os.remove(tmp)
158+
sage: os.remove(f.name)
157159
sage: shell.quit()
158160
"""
159161
return self.shell.ex(load_wrap(s, attach=True))

0 commit comments

Comments
 (0)