From 7233d8bd47ee241dc83f60177eea1c991af9905e 2012-05-26 02:31:42 From: Fernando Perez Date: 2012-05-26 02:31:42 Subject: [PATCH] Fix more failures due to direct shell access in magics. --- diff --git a/IPython/core/history.py b/IPython/core/history.py index e3761e5..8434872 100644 --- a/IPython/core/history.py +++ b/IPython/core/history.py @@ -909,7 +909,7 @@ def magic_rep(self, arg): self.shell.set_next_input(str(self.shell.user_ns["_"])) return # Get history range - histlines = self.history_manager.get_range_by_str(arg) + histlines = self.shell.history_manager.get_range_by_str(arg) cmd = "\n".join(x[2] for x in histlines) if cmd: self.shell.set_next_input(cmd.rstrip()) @@ -918,7 +918,7 @@ def magic_rep(self, arg): try: # Variable in user namespace cmd = str(eval(arg, self.shell.user_ns)) except Exception: # Search for term in history - histlines = self.history_manager.search("*"+arg+"*") + histlines = self.shell.history_manager.search("*"+arg+"*") for h in reversed([x[2] for x in histlines]): if 'rep' in h: continue @@ -945,10 +945,10 @@ def magic_rerun(self, parameter_s=''): opts, args = self.parse_options(parameter_s, 'l:g:', mode='string') if "l" in opts: # Last n lines n = int(opts['l']) - hist = self.history_manager.get_tail(n) + hist = self.shell.history_manager.get_tail(n) elif "g" in opts: # Search p = "*"+opts['g']+"*" - hist = list(self.history_manager.search(p)) + hist = list(self.shell.history_manager.search(p)) for l in reversed(hist): if "rerun" not in l[2]: hist = [l] # The last match which isn't a %rerun @@ -956,9 +956,9 @@ def magic_rerun(self, parameter_s=''): else: hist = [] # No matches except %rerun elif args: # Specify history ranges - hist = self.history_manager.get_range_by_str(args) + hist = self.shell.history_manager.get_range_by_str(args) else: # Last line - hist = self.history_manager.get_tail(1) + hist = self.shell.history_manager.get_tail(1) hist = [x[2] for x in hist] if not hist: print("No lines in history match specification") @@ -967,7 +967,7 @@ def magic_rerun(self, parameter_s=''): print("=== Executing: ===") print(histlines) print("=== Output: ===") - self.run_cell("\n".join(hist), store_history=False) + self.shell.run_cell("\n".join(hist), store_history=False) def init_ipython(ip): diff --git a/IPython/core/magic.py b/IPython/core/magic.py index b9b658e..d54ab01 100644 --- a/IPython/core/magic.py +++ b/IPython/core/magic.py @@ -171,8 +171,8 @@ python-profiler package from non-free.""") inst_bound_magic = lambda fn: fn.startswith('magic_') and \ callable(self.__class__.__dict__[fn]) magics = filter(class_magic,Magic.__dict__.keys()) + \ - filter(inst_magic,self.__dict__.keys()) + \ - filter(inst_bound_magic,self.__class__.__dict__.keys()) + filter(inst_magic, self.__dict__.keys()) + \ + filter(inst_bound_magic, self.__class__.__dict__.keys()) out = [] for fn in set(magics): out.append(fn.replace('magic_','',1)) @@ -337,7 +337,7 @@ python-profiler package from non-free.""") magic_docs = [] for fname in self.lsmagic(): mname = 'magic_' + fname - for space in (Magic,self,self.__class__): + for space in (Magic, self, self.__class__): try: fn = space.__dict__[mname] except KeyError: @@ -1035,17 +1035,17 @@ Currently the magic system has the following functions:\n""" # reset in/out/dhist/array: previously extensinions/clearcmd.py ip = self.shell - user_ns = self.user_ns # local lookup, heavily used + user_ns = self.shell.user_ns # local lookup, heavily used for target in args: target = target.lower() # make matches case insensitive if target == 'out': print "Flushing output cache (%d entries)" % len(user_ns['_oh']) - self.displayhook.flush() + self.shell.displayhook.flush() elif target == 'in': print "Flushing input history" - pc = self.displayhook.prompt_count + 1 + pc = self.shell.displayhook.prompt_count + 1 for n in range(1, pc): key = '_i'+repr(n) user_ns.pop(key,None) @@ -2630,7 +2630,7 @@ Currently the magic system has the following functions:\n""" self.shell.run_cell(file_read(filename), store_history=False) else: - self.shell.safe_execfile(filename,self.shell.user_ns, + self.shell.safe_execfile(filename, self.shell.user_ns, self.shell.user_ns) if is_temp: @@ -3804,7 +3804,8 @@ Defaulting color scheme to 'NoColor'""" # some IPython objects are Configurable, but do not yet have # any configurable traits. Exclude them from the effects of # this magic, as their presence is just noise: - configurables = [ c for c in self.configurables if c.__class__.class_traits(config=True) ] + configurables = [ c for c in self.shell.configurables + if c.__class__.class_traits(config=True) ] classnames = [ c.__class__.__name__ for c in configurables ] line = s.strip() @@ -3832,7 +3833,7 @@ Defaulting color scheme to 'NoColor'""" # leave quotes on args when splitting, because we want # unquoted args to eval in user_ns cfg = Config() - exec "cfg."+line in locals(), self.user_ns + exec "cfg."+line in locals(), self.shell.user_ns for configurable in configurables: try: diff --git a/IPython/core/prefilter.py b/IPython/core/prefilter.py index 83f08ce..22bf1e1 100644 --- a/IPython/core/prefilter.py +++ b/IPython/core/prefilter.py @@ -807,7 +807,7 @@ class AutoHandler(PrefilterHandler): pre = line_info.pre esc = line_info.esc continue_prompt = line_info.continue_prompt - obj = line_info.ofind(self)['obj'] + obj = line_info.ofind(self.shell)['obj'] #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun,the_rest) # dbg # This should only be active for single-line input! diff --git a/IPython/core/splitinput.py b/IPython/core/splitinput.py index 9cf060c..692251e 100644 --- a/IPython/core/splitinput.py +++ b/IPython/core/splitinput.py @@ -49,6 +49,7 @@ line_split = re.compile(""" (.*?$|$) # rest of line """, re.VERBOSE) + def split_user_input(line, pattern=None): """Split user input into initial whitespace, escape character, function part and the rest. @@ -76,6 +77,7 @@ def split_user_input(line, pattern=None): #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun.strip(),the_rest) # dbg return pre, esc or '', ifun.strip(), the_rest.lstrip() + class LineInfo(object): """A single line of input and associated info. @@ -122,7 +124,7 @@ class LineInfo(object): """Do a full, attribute-walking lookup of the ifun in the various namespaces for the given IPython InteractiveShell instance. - Return a dict with keys: found,obj,ospace,ismagic + Return a dict with keys: {found, obj, ospace, ismagic} Note: can cause state changes because of calling getattr, but should only be run if autocall is on and if the line hasn't matched any