Skip to content

Commit fafc679

Browse files
authored
remove usage of mktemp (deprecated) in docstrings (ipython#15028)
2 parents 79f313a + 6d99f49 commit fafc679

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

IPython/core/magics/history.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,20 @@ class HistoryMagics(Magics):
7373
the command 'history -f FILENAME' from the IPython Notebook
7474
interface will replace FILENAME even if it already exists *without*
7575
confirmation.
76-
""")
76+
""",
77+
)
78+
@argument(
79+
"-y",
80+
dest="overwrite",
81+
help="yes, overwrite filename even if exists",
82+
action="store_true",
83+
default=None,
84+
)
7785
@argument(
78-
'-g', dest='pattern', nargs='*', default=None,
86+
"-g",
87+
dest="pattern",
88+
nargs="*",
89+
default=None,
7990
help="""
8091
treat the arg as a glob pattern to search for in (full) history.
8192
This includes the saved history (almost all commands ever written).
@@ -152,13 +163,19 @@ def _format_lineno(session, line):
152163
close_at_end = False
153164
else:
154165
outfname = os.path.expanduser(outfname)
155-
if os.path.exists(outfname):
156-
try:
166+
if args.overwrite is True:
167+
ans = True
168+
elif os.path.exists(outfname):
169+
ans = True
170+
if sys.stdin.isatty():
157171
ans = io.ask_yes_no("File %r exists. Overwrite?" % outfname)
158-
except StdinNotImplementedError:
159-
ans = True
172+
else:
173+
try:
174+
ans = io.ask_yes_no("File %r exists. Overwrite?" % outfname)
175+
except StdinNotImplementedError:
176+
ans = True
160177
if not ans:
161-
print('Aborting.')
178+
print("Aborting.")
162179
return
163180
print("Overwriting file.")
164181
outfile = io_open(outfname, 'w', encoding='utf-8')

tests/test_magic.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,14 @@ def test_magic_parse_long_options():
198198
def doctest_hist_f():
199199
"""Test %hist -f with temporary filename.
200200
201-
In [9]: import tempfile
201+
In [9]: import tempfile, os
202202
203-
In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
203+
In [10]: fd, tfile = tempfile.mkstemp('.py','tmp-ipython-')
204+
In [11]: os.close(fd)
204205
205-
In [11]: %hist -nl -f $tfile 3
206+
In [12]: %history -nl -y -f $tfile 3
206207
207-
In [13]: import os; os.unlink(tfile)
208+
In [14]: import os; os.unlink(tfile)
208209
"""
209210

210211

0 commit comments

Comments
 (0)