diff --git a/IPython/core/displayhook.py b/IPython/core/displayhook.py index 262fe7e..b0f83e3 100644 --- a/IPython/core/displayhook.py +++ b/IPython/core/displayhook.py @@ -250,7 +250,7 @@ class DisplayHook(Configurable): def log_output(self, result): """Log the output.""" if self.shell.logger.log_output: - self.shell.logger.log_write(repr(result),'output') + self.shell.logger.log_write(repr(result), 'output') def finish_displayhook(self): """Finish up all displayhook activities.""" diff --git a/IPython/core/history.py b/IPython/core/history.py index 9ee3dca..e013a6b 100644 --- a/IPython/core/history.py +++ b/IPython/core/history.py @@ -36,6 +36,7 @@ class HistoryManager(object): def __init__(self, shell): """Create a new history manager associated with a shell instance. """ + # We need a pointer back to the shell for various tasks. self.shell = shell # List of input with multi-line handling. @@ -64,6 +65,13 @@ class HistoryManager(object): # Objects related to shadow history management self._init_shadow_hist() + # Variables used to store the three last inputs from the user. On each + # new history update, we populate the user's namespace with these, + # shifted as necessary. + self._i00, self._i, self._ii, self._iii = '','','','' + + # Object is fully initialized, we can now call methods on it. + # Fill the history zero entry, user counter starts at 1 self.store_inputs('\n', '\n') @@ -158,8 +166,9 @@ class HistoryManager(object): return hist def store_inputs(self, source, source_raw=None): - """Store source and raw input in history. - + """Store source and raw input in history and create input cache + variables _i*. + Parameters ---------- source : str @@ -175,6 +184,20 @@ class HistoryManager(object): self.input_hist_raw.append(source_raw) self.shadow_hist.add(source) + # update the auto _i variables + self._iii = self._ii + self._ii = self._i + self._i = self._i00 + self._i00 = source_raw + + # hackish access to user namespace to create _i1,_i2... dynamically + new_i = '_i%s' % self.shell.execution_count + to_main = {'_i': self._i, + '_ii': self._ii, + '_iii': self._iii, + new_i : self._i00 } + self.shell.user_ns.update(to_main) + def sync_inputs(self): """Ensure raw and translated histories have same length.""" if len(self.input_hist) != len (self.input_hist_raw): @@ -403,7 +426,7 @@ def rep_f(self, arg): try: lines = self.extract_input_slices(args, True) print("lines", lines) - self.runlines(lines) + self.run_cell(lines) except ValueError: print("Not found in recent history:", args) diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 649c049..51c1112 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -430,11 +430,12 @@ class InteractiveShell(Configurable, Magic): self.dir_stack = [] def init_logger(self): - self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate') - # local shortcut, this is used a LOT - self.log = self.logger.log + self.logger = Logger(self.home_dir, logfname='ipython_log.py', + logmode='rotate') def init_logstart(self): + """Initialize logging in case it was requested at the command line. + """ if self.logappend: self.magic_logstart(self.logappend + ' append') elif self.logfile: @@ -1839,7 +1840,6 @@ class InteractiveShell(Configurable, Magic): # code out there that may rely on this). self.prefilter = self.prefilter_manager.prefilter_lines - def auto_rewrite_input(self, cmd): """Print to the screen the rewritten form of the user's command. @@ -2026,12 +2026,11 @@ class InteractiveShell(Configurable, Magic): with prepended_to_syspath(dname): try: with open(fname) as thefile: - script = thefile.read() - # self.runlines currently captures all exceptions - # raise in user code. It would be nice if there were + # self.run_cell currently captures all exceptions + # raised in user code. It would be nice if there were # versions of runlines, execfile that did raise, so # we could catch the errors. - self.runlines(script, clean=True) + self.run_cell(thefile.read()) except: self.showtraceback() warn('Unknown failure executing file: <%s>' % fname) @@ -2099,6 +2098,7 @@ class InteractiveShell(Configurable, Magic): # Store raw and processed history self.history_manager.store_inputs(ipy_cell, cell) + self.logger.log(ipy_cell, cell) # dbg code!!! if 0: def myapp(self, val): # dbg diff --git a/IPython/core/macro.py b/IPython/core/macro.py index 676e083..4a05180 100644 --- a/IPython/core/macro.py +++ b/IPython/core/macro.py @@ -20,9 +20,7 @@ class Macro(IPyAutocall): """ def __init__(self,data): - - # store the macro value, as a single string which can be evaluated by - # runlines() + # store the macro value, as a single string which can be executed self.value = ''.join(data).rstrip()+'\n' def __str__(self): @@ -34,7 +32,7 @@ class Macro(IPyAutocall): def __call__(self,*args): IPython.utils.io.Term.cout.flush() self._ip.user_ns['_margv'] = args - self._ip.runlines(self.value) + self._ip.run_cell(self.value) def __getstate__(self): """ needed for safe pickling via %store """ diff --git a/IPython/core/magic.py b/IPython/core/magic.py index 31ba558..c63b054 100644 --- a/IPython/core/magic.py +++ b/IPython/core/magic.py @@ -41,11 +41,6 @@ except ImportError: except ImportError: profile = pstats = None -# print_function was added to __future__ in Python2.6, remove this when we drop -# 2.5 compatibility -if not hasattr(__future__,'CO_FUTURE_PRINT_FUNCTION'): - __future__.CO_FUTURE_PRINT_FUNCTION = 65536 - import IPython from IPython.core import debugger, oinspect from IPython.core.error import TryNext @@ -2331,7 +2326,7 @@ Currently the magic system has the following functions:\n""" else: print 'done. Executing edited code...' if opts_r: - self.shell.runlines(file_read(filename)) + self.shell.run_cell(file_read(filename)) else: self.shell.safe_execfile(filename,self.shell.user_ns, self.shell.user_ns) @@ -2992,7 +2987,7 @@ Defaulting color scheme to 'NoColor'""" (input.startswith(start) or input.startswith(start_magic)): #print 'match',`input` # dbg print 'Executing:',input, - self.shell.runlines(input) + self.shell.run_cell(input) return print 'No previous input matching `%s` found.' % start diff --git a/IPython/core/prefilter.py b/IPython/core/prefilter.py index e427091..5f36f94 100755 --- a/IPython/core/prefilter.py +++ b/IPython/core/prefilter.py @@ -373,10 +373,6 @@ class PrefilterManager(Configurable): # print "prefilter_line: ", line, continue_prompt # All handlers *must* return a value, even if it's blank (''). - # Lines are NOT logged here. Handlers should process the line as - # needed, update the cache AND log it (so that the input cache array - # stays synced). - # save the line away in case we crash, so the post-mortem handler can # record it self.shell._last_input_line = line @@ -792,7 +788,6 @@ class PrefilterHandler(Configurable): ): line = '' - self.shell.log(line, line, continue_prompt) return line def __str__(self): @@ -811,7 +806,6 @@ class AliasHandler(PrefilterHandler): line_out = '%sget_ipython().system(%s)' % (line_info.pre_whitespace, make_quoted_expr(transformed)) - self.shell.log(line_info.line, line_out, line_info.continue_prompt) return line_out @@ -840,8 +834,6 @@ class ShellEscapeHandler(PrefilterHandler): cmd = line.lstrip().lstrip(ESC_SHELL) line_out = '%sget_ipython().system(%s)' % (line_info.pre_whitespace, make_quoted_expr(cmd)) - # update cache/log and return - self.shell.log(line, line_out, line_info.continue_prompt) return line_out @@ -856,7 +848,6 @@ class MagicHandler(PrefilterHandler): the_rest = line_info.the_rest cmd = '%sget_ipython().magic(%s)' % (line_info.pre_whitespace, make_quoted_expr(ifun + " " + the_rest)) - self.shell.log(line_info.line, cmd, line_info.continue_prompt) return cmd @@ -877,7 +868,6 @@ class AutoHandler(PrefilterHandler): # This should only be active for single-line input! if continue_prompt: - self.shell.log(line,line,continue_prompt) return line force_auto = isinstance(obj, IPyAutocall) @@ -918,9 +908,6 @@ class AutoHandler(PrefilterHandler): if auto_rewrite: self.shell.auto_rewrite_input(newcmd) - # log what is now valid Python, not the actual user input (without the - # final newline) - self.shell.log(line,newcmd,continue_prompt) return newcmd @@ -947,7 +934,6 @@ class HelpHandler(PrefilterHandler): line = line[1:] elif line[-1]==ESC_HELP: line = line[:-1] - self.shell.log(line, '#?'+line, line_info.continue_prompt) if line: #print 'line:<%r>' % line # dbg self.shell.magic_pinfo(line) diff --git a/IPython/frontend/terminal/ipapp.py b/IPython/frontend/terminal/ipapp.py index d52052a..442e98a 100755 --- a/IPython/frontend/terminal/ipapp.py +++ b/IPython/frontend/terminal/ipapp.py @@ -571,7 +571,7 @@ class IPythonApp(Application): try: self.log.info("Running code in user namespace: %s" % line) - self.shell.runlines(line) + self.shell.run_cell(line) except: self.log.warn("Error in executing line in user " "namespace: %s" % line) @@ -616,7 +616,7 @@ class IPythonApp(Application): try: self.log.info("Running code given at command line (-c): %s" % line) - self.shell.runlines(line) + self.shell.run_cell(line) except: self.log.warn("Error in executing line in user namespace: %s" % line) diff --git a/IPython/lib/demo.py b/IPython/lib/demo.py index 580588f..a2e82ea 100644 --- a/IPython/lib/demo.py +++ b/IPython/lib/demo.py @@ -248,7 +248,7 @@ class Demo(object): self.ip_ns = ip.user_ns self.ip_colorize = ip.pycolorize self.ip_showtb = ip.showtraceback - self.ip_runlines = ip.runlines + self.ip_run_cell = ip.run_cell self.shell = ip # load user data and initialize data structures @@ -411,7 +411,7 @@ class Demo(object): print >>IPython.utils.io.Term.cout, block, sys.stdout.flush() - def runlines(self,source): + def run_cell(self,source): """Execute a string with one or more lines of code""" exec source in self.user_ns @@ -449,7 +449,7 @@ class Demo(object): try: save_argv = sys.argv sys.argv = self.sys_argv - self.runlines(next_block) + self.run_cell(next_block) self.post_cmd() finally: sys.argv = save_argv @@ -496,10 +496,10 @@ class IPythonDemo(Demo): class requires the input to be valid, pure Python code. """ - def runlines(self,source): + def run_cell(self,source): """Execute a string with one or more lines of code""" - self.shell.runlines(source) + self.shell.run_cell(source) class LineDemo(Demo): """Demo where each line is executed as a separate block. diff --git a/IPython/zmq/ipkernel.py b/IPython/zmq/ipkernel.py index b07776f..0282979 100755 --- a/IPython/zmq/ipkernel.py +++ b/IPython/zmq/ipkernel.py @@ -214,14 +214,8 @@ class Kernel(Configurable): # statements in that code will obviously still execute. shell.runcode(code) else: - # FIXME: runlines calls the exception handler itself. + # FIXME: the shell calls the exception handler itself. shell._reply_content = None - - # For now leave this here until we're sure we can stop using it - #shell.runlines(code) - - # Experimental: cell mode! Test more before turning into - # default and removing the hacks around runlines. shell.run_cell(code) except: status = u'error'