From c8d17d8552b17d325f64fa7b7581f9d42111ff42 2012-01-20 00:29:03 From: Fernando Perez Date: 2012-01-20 00:29:03 Subject: [PATCH] Merge pull request #1288 from takluyver/reset-stdinless Don't ask for confirmation when stdin isn't available for actions like %reset or %history -f. Since the change is applied only on the Stdin error, once the notebook gets a stdin, it will naturally work. --- diff --git a/IPython/core/history.py b/IPython/core/history.py index 09dd003..9a4a249 100644 --- a/IPython/core/history.py +++ b/IPython/core/history.py @@ -24,6 +24,7 @@ except ImportError: import threading # Our own packages +from IPython.core.error import StdinNotImplementedError from IPython.config.configurable import Configurable from IPython.external.decorator import decorator from IPython.testing.skipdoctest import skip_doctest @@ -761,8 +762,10 @@ def magic_history(self, parameter_s = ''): the default is the last 10 lines. -f FILENAME: instead of printing the output to the screen, redirect it to - the given file. The file is always overwritten, though IPython asks for - confirmation first if it already exists. + the given file. The file is always overwritten, though *when it can*, + IPython asks for confirmation first. In particular, running the command + "history -f FILENAME" from the IPython Notebook interface will replace + FILENAME even if it already exists *without* confirmation. Examples -------- @@ -797,10 +800,14 @@ def magic_history(self, parameter_s = ''): close_at_end = False else: if os.path.exists(outfname): - if not io.ask_yes_no("File %r exists. Overwrite?" % outfname): + try: + ans = io.ask_yes_no("File %r exists. Overwrite?" % outfname) + except StdinNotImplementedError: + ans = True + if not ans: print('Aborting.') return - + print("Overwriting file.") outfile = open(outfname,'w') close_at_end = True diff --git a/IPython/core/magic.py b/IPython/core/magic.py index 3deba71..950a1e5 100644 --- a/IPython/core/magic.py +++ b/IPython/core/magic.py @@ -45,6 +45,7 @@ import IPython from IPython.core import debugger, oinspect from IPython.core.error import TryNext from IPython.core.error import UsageError +from IPython.core.error import StdinNotImplementedError from IPython.core.fakemodule import FakeModule from IPython.core.profiledir import ProfileDir from IPython.core.macro import Macro @@ -983,13 +984,22 @@ Currently the magic system has the following functions:\n""" In [1]: 'a' in _ip.user_ns Out[1]: False + + Notes + ----- + Calling this magic from clients that do not implement standard input, + such as the ipython notebook interface, will reset the namespace + without confirmation. """ opts, args = self.parse_options(parameter_s,'sf') if 'f' in opts: ans = True else: - ans = self.shell.ask_yes_no( + try: + ans = self.shell.ask_yes_no( "Once deleted, variables cannot be recovered. Proceed (y/[n])? ", default='n') + except StdinNotImplementedError: + ans = True if not ans: print 'Nothing done.' return @@ -1052,6 +1062,12 @@ Currently the magic system has the following functions:\n""" In [11]: who_ls Out[11]: ['a'] + + Notes + ----- + Calling this magic from clients that do not implement standard input, + such as the ipython notebook interface, will reset the namespace + without confirmation. """ opts, regex = self.parse_options(parameter_s,'f') @@ -1059,9 +1075,12 @@ Currently the magic system has the following functions:\n""" if opts.has_key('f'): ans = True else: - ans = self.shell.ask_yes_no( + try: + ans = self.shell.ask_yes_no( "Once deleted, variables cannot be recovered. Proceed (y/[n])? ", default='n') + except StdinNotImplementedError: + ans = True if not ans: print 'Nothing done.' return