##// END OF EJS Templates
Fix more failures due to direct shell access in magics.
Fernando Perez -
Show More
@@ -909,7 +909,7 b' def magic_rep(self, arg):'
909 self.shell.set_next_input(str(self.shell.user_ns["_"]))
909 self.shell.set_next_input(str(self.shell.user_ns["_"]))
910 return
910 return
911 # Get history range
911 # Get history range
912 histlines = self.history_manager.get_range_by_str(arg)
912 histlines = self.shell.history_manager.get_range_by_str(arg)
913 cmd = "\n".join(x[2] for x in histlines)
913 cmd = "\n".join(x[2] for x in histlines)
914 if cmd:
914 if cmd:
915 self.shell.set_next_input(cmd.rstrip())
915 self.shell.set_next_input(cmd.rstrip())
@@ -918,7 +918,7 b' def magic_rep(self, arg):'
918 try: # Variable in user namespace
918 try: # Variable in user namespace
919 cmd = str(eval(arg, self.shell.user_ns))
919 cmd = str(eval(arg, self.shell.user_ns))
920 except Exception: # Search for term in history
920 except Exception: # Search for term in history
921 histlines = self.history_manager.search("*"+arg+"*")
921 histlines = self.shell.history_manager.search("*"+arg+"*")
922 for h in reversed([x[2] for x in histlines]):
922 for h in reversed([x[2] for x in histlines]):
923 if 'rep' in h:
923 if 'rep' in h:
924 continue
924 continue
@@ -945,10 +945,10 b" def magic_rerun(self, parameter_s=''):"
945 opts, args = self.parse_options(parameter_s, 'l:g:', mode='string')
945 opts, args = self.parse_options(parameter_s, 'l:g:', mode='string')
946 if "l" in opts: # Last n lines
946 if "l" in opts: # Last n lines
947 n = int(opts['l'])
947 n = int(opts['l'])
948 hist = self.history_manager.get_tail(n)
948 hist = self.shell.history_manager.get_tail(n)
949 elif "g" in opts: # Search
949 elif "g" in opts: # Search
950 p = "*"+opts['g']+"*"
950 p = "*"+opts['g']+"*"
951 hist = list(self.history_manager.search(p))
951 hist = list(self.shell.history_manager.search(p))
952 for l in reversed(hist):
952 for l in reversed(hist):
953 if "rerun" not in l[2]:
953 if "rerun" not in l[2]:
954 hist = [l] # The last match which isn't a %rerun
954 hist = [l] # The last match which isn't a %rerun
@@ -956,9 +956,9 b" def magic_rerun(self, parameter_s=''):"
956 else:
956 else:
957 hist = [] # No matches except %rerun
957 hist = [] # No matches except %rerun
958 elif args: # Specify history ranges
958 elif args: # Specify history ranges
959 hist = self.history_manager.get_range_by_str(args)
959 hist = self.shell.history_manager.get_range_by_str(args)
960 else: # Last line
960 else: # Last line
961 hist = self.history_manager.get_tail(1)
961 hist = self.shell.history_manager.get_tail(1)
962 hist = [x[2] for x in hist]
962 hist = [x[2] for x in hist]
963 if not hist:
963 if not hist:
964 print("No lines in history match specification")
964 print("No lines in history match specification")
@@ -967,7 +967,7 b" def magic_rerun(self, parameter_s=''):"
967 print("=== Executing: ===")
967 print("=== Executing: ===")
968 print(histlines)
968 print(histlines)
969 print("=== Output: ===")
969 print("=== Output: ===")
970 self.run_cell("\n".join(hist), store_history=False)
970 self.shell.run_cell("\n".join(hist), store_history=False)
971
971
972
972
973 def init_ipython(ip):
973 def init_ipython(ip):
@@ -171,8 +171,8 b' python-profiler package from non-free.""")'
171 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
171 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
172 callable(self.__class__.__dict__[fn])
172 callable(self.__class__.__dict__[fn])
173 magics = filter(class_magic,Magic.__dict__.keys()) + \
173 magics = filter(class_magic,Magic.__dict__.keys()) + \
174 filter(inst_magic,self.__dict__.keys()) + \
174 filter(inst_magic, self.__dict__.keys()) + \
175 filter(inst_bound_magic,self.__class__.__dict__.keys())
175 filter(inst_bound_magic, self.__class__.__dict__.keys())
176 out = []
176 out = []
177 for fn in set(magics):
177 for fn in set(magics):
178 out.append(fn.replace('magic_','',1))
178 out.append(fn.replace('magic_','',1))
@@ -337,7 +337,7 b' python-profiler package from non-free.""")'
337 magic_docs = []
337 magic_docs = []
338 for fname in self.lsmagic():
338 for fname in self.lsmagic():
339 mname = 'magic_' + fname
339 mname = 'magic_' + fname
340 for space in (Magic,self,self.__class__):
340 for space in (Magic, self, self.__class__):
341 try:
341 try:
342 fn = space.__dict__[mname]
342 fn = space.__dict__[mname]
343 except KeyError:
343 except KeyError:
@@ -1035,17 +1035,17 b' Currently the magic system has the following functions:\\n"""'
1035
1035
1036 # reset in/out/dhist/array: previously extensinions/clearcmd.py
1036 # reset in/out/dhist/array: previously extensinions/clearcmd.py
1037 ip = self.shell
1037 ip = self.shell
1038 user_ns = self.user_ns # local lookup, heavily used
1038 user_ns = self.shell.user_ns # local lookup, heavily used
1039
1039
1040 for target in args:
1040 for target in args:
1041 target = target.lower() # make matches case insensitive
1041 target = target.lower() # make matches case insensitive
1042 if target == 'out':
1042 if target == 'out':
1043 print "Flushing output cache (%d entries)" % len(user_ns['_oh'])
1043 print "Flushing output cache (%d entries)" % len(user_ns['_oh'])
1044 self.displayhook.flush()
1044 self.shell.displayhook.flush()
1045
1045
1046 elif target == 'in':
1046 elif target == 'in':
1047 print "Flushing input history"
1047 print "Flushing input history"
1048 pc = self.displayhook.prompt_count + 1
1048 pc = self.shell.displayhook.prompt_count + 1
1049 for n in range(1, pc):
1049 for n in range(1, pc):
1050 key = '_i'+repr(n)
1050 key = '_i'+repr(n)
1051 user_ns.pop(key,None)
1051 user_ns.pop(key,None)
@@ -2630,7 +2630,7 b' Currently the magic system has the following functions:\\n"""'
2630 self.shell.run_cell(file_read(filename),
2630 self.shell.run_cell(file_read(filename),
2631 store_history=False)
2631 store_history=False)
2632 else:
2632 else:
2633 self.shell.safe_execfile(filename,self.shell.user_ns,
2633 self.shell.safe_execfile(filename, self.shell.user_ns,
2634 self.shell.user_ns)
2634 self.shell.user_ns)
2635
2635
2636 if is_temp:
2636 if is_temp:
@@ -3804,7 +3804,8 b' Defaulting color scheme to \'NoColor\'"""'
3804 # some IPython objects are Configurable, but do not yet have
3804 # some IPython objects are Configurable, but do not yet have
3805 # any configurable traits. Exclude them from the effects of
3805 # any configurable traits. Exclude them from the effects of
3806 # this magic, as their presence is just noise:
3806 # this magic, as their presence is just noise:
3807 configurables = [ c for c in self.configurables if c.__class__.class_traits(config=True) ]
3807 configurables = [ c for c in self.shell.configurables
3808 if c.__class__.class_traits(config=True) ]
3808 classnames = [ c.__class__.__name__ for c in configurables ]
3809 classnames = [ c.__class__.__name__ for c in configurables ]
3809
3810
3810 line = s.strip()
3811 line = s.strip()
@@ -3832,7 +3833,7 b' Defaulting color scheme to \'NoColor\'"""'
3832 # leave quotes on args when splitting, because we want
3833 # leave quotes on args when splitting, because we want
3833 # unquoted args to eval in user_ns
3834 # unquoted args to eval in user_ns
3834 cfg = Config()
3835 cfg = Config()
3835 exec "cfg."+line in locals(), self.user_ns
3836 exec "cfg."+line in locals(), self.shell.user_ns
3836
3837
3837 for configurable in configurables:
3838 for configurable in configurables:
3838 try:
3839 try:
@@ -807,7 +807,7 b' class AutoHandler(PrefilterHandler):'
807 pre = line_info.pre
807 pre = line_info.pre
808 esc = line_info.esc
808 esc = line_info.esc
809 continue_prompt = line_info.continue_prompt
809 continue_prompt = line_info.continue_prompt
810 obj = line_info.ofind(self)['obj']
810 obj = line_info.ofind(self.shell)['obj']
811 #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun,the_rest) # dbg
811 #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun,the_rest) # dbg
812
812
813 # This should only be active for single-line input!
813 # This should only be active for single-line input!
@@ -49,6 +49,7 b' line_split = re.compile("""'
49 (.*?$|$) # rest of line
49 (.*?$|$) # rest of line
50 """, re.VERBOSE)
50 """, re.VERBOSE)
51
51
52
52 def split_user_input(line, pattern=None):
53 def split_user_input(line, pattern=None):
53 """Split user input into initial whitespace, escape character, function part
54 """Split user input into initial whitespace, escape character, function part
54 and the rest.
55 and the rest.
@@ -76,6 +77,7 b' def split_user_input(line, pattern=None):'
76 #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun.strip(),the_rest) # dbg
77 #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun.strip(),the_rest) # dbg
77 return pre, esc or '', ifun.strip(), the_rest.lstrip()
78 return pre, esc or '', ifun.strip(), the_rest.lstrip()
78
79
80
79 class LineInfo(object):
81 class LineInfo(object):
80 """A single line of input and associated info.
82 """A single line of input and associated info.
81
83
@@ -122,7 +124,7 b' class LineInfo(object):'
122 """Do a full, attribute-walking lookup of the ifun in the various
124 """Do a full, attribute-walking lookup of the ifun in the various
123 namespaces for the given IPython InteractiveShell instance.
125 namespaces for the given IPython InteractiveShell instance.
124
126
125 Return a dict with keys: found,obj,ospace,ismagic
127 Return a dict with keys: {found, obj, ospace, ismagic}
126
128
127 Note: can cause state changes because of calling getattr, but should
129 Note: can cause state changes because of calling getattr, but should
128 only be run if autocall is on and if the line hasn't matched any
130 only be run if autocall is on and if the line hasn't matched any
General Comments 0
You need to be logged in to leave comments. Login now