##// END OF EJS Templates
Finish removing spurious calls to logger and runlines....
Fernando Perez -
Show More
@@ -1,293 +1,293 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Displayhook for IPython.
3 3
4 4 Authors:
5 5
6 6 * Fernando Perez
7 7 * Brian Granger
8 8 """
9 9
10 10 #-----------------------------------------------------------------------------
11 11 # Copyright (C) 2008-2010 The IPython Development Team
12 12 # Copyright (C) 2001-2007 Fernando Perez <fperez@colorado.edu>
13 13 #
14 14 # Distributed under the terms of the BSD License. The full license is in
15 15 # the file COPYING, distributed as part of this software.
16 16 #-----------------------------------------------------------------------------
17 17
18 18 #-----------------------------------------------------------------------------
19 19 # Imports
20 20 #-----------------------------------------------------------------------------
21 21
22 22 import __builtin__
23 23 from pprint import PrettyPrinter
24 24 pformat = PrettyPrinter().pformat
25 25
26 26 from IPython.config.configurable import Configurable
27 27 from IPython.core import prompts
28 28 import IPython.utils.generics
29 29 import IPython.utils.io
30 30 from IPython.utils.traitlets import Instance, Int
31 31 from IPython.utils.warn import warn
32 32
33 33 #-----------------------------------------------------------------------------
34 34 # Main displayhook class
35 35 #-----------------------------------------------------------------------------
36 36
37 37 # TODO: The DisplayHook class should be split into two classes, one that
38 38 # manages the prompts and their synchronization and another that just does the
39 39 # displayhook logic and calls into the prompt manager.
40 40
41 41 # TODO: Move the various attributes (cache_size, colors, input_sep,
42 42 # output_sep, output_sep2, ps1, ps2, ps_out, pad_left). Some of these are also
43 43 # attributes of InteractiveShell. They should be on ONE object only and the
44 44 # other objects should ask that one object for their values.
45 45
46 46 class DisplayHook(Configurable):
47 47 """The custom IPython displayhook to replace sys.displayhook.
48 48
49 49 This class does many things, but the basic idea is that it is a callable
50 50 that gets called anytime user code returns a value.
51 51
52 52 Currently this class does more than just the displayhook logic and that
53 53 extra logic should eventually be moved out of here.
54 54 """
55 55
56 56 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
57 57
58 58 # Each call to the In[] prompt raises it by 1, even the first.
59 59 #prompt_count = Int(0)
60 60
61 61 def __init__(self, shell=None, cache_size=1000,
62 62 colors='NoColor', input_sep='\n',
63 63 output_sep='\n', output_sep2='',
64 64 ps1 = None, ps2 = None, ps_out = None, pad_left=True,
65 65 config=None):
66 66 super(DisplayHook, self).__init__(shell=shell, config=config)
67 67
68 68 cache_size_min = 3
69 69 if cache_size <= 0:
70 70 self.do_full_cache = 0
71 71 cache_size = 0
72 72 elif cache_size < cache_size_min:
73 73 self.do_full_cache = 0
74 74 cache_size = 0
75 75 warn('caching was disabled (min value for cache size is %s).' %
76 76 cache_size_min,level=3)
77 77 else:
78 78 self.do_full_cache = 1
79 79
80 80 self.cache_size = cache_size
81 81 self.input_sep = input_sep
82 82
83 83 # we need a reference to the user-level namespace
84 84 self.shell = shell
85 85
86 86 # Set input prompt strings and colors
87 87 if cache_size == 0:
88 88 if ps1.find('%n') > -1 or ps1.find(r'\#') > -1 \
89 89 or ps1.find(r'\N') > -1:
90 90 ps1 = '>>> '
91 91 if ps2.find('%n') > -1 or ps2.find(r'\#') > -1 \
92 92 or ps2.find(r'\N') > -1:
93 93 ps2 = '... '
94 94 self.ps1_str = self._set_prompt_str(ps1,'In [\\#]: ','>>> ')
95 95 self.ps2_str = self._set_prompt_str(ps2,' .\\D.: ','... ')
96 96 self.ps_out_str = self._set_prompt_str(ps_out,'Out[\\#]: ','')
97 97
98 98 self.color_table = prompts.PromptColors
99 99 self.prompt1 = prompts.Prompt1(self,sep=input_sep,prompt=self.ps1_str,
100 100 pad_left=pad_left)
101 101 self.prompt2 = prompts.Prompt2(self,prompt=self.ps2_str,pad_left=pad_left)
102 102 self.prompt_out = prompts.PromptOut(self,sep='',prompt=self.ps_out_str,
103 103 pad_left=pad_left)
104 104 self.set_colors(colors)
105 105
106 106 # Store the last prompt string each time, we need it for aligning
107 107 # continuation and auto-rewrite prompts
108 108 self.last_prompt = ''
109 109 self.output_sep = output_sep
110 110 self.output_sep2 = output_sep2
111 111 self._,self.__,self.___ = '','',''
112 112 self.pprint_types = map(type,[(),[],{}])
113 113
114 114 # these are deliberately global:
115 115 to_user_ns = {'_':self._,'__':self.__,'___':self.___}
116 116 self.shell.user_ns.update(to_user_ns)
117 117
118 118 @property
119 119 def prompt_count(self):
120 120 return self.shell.execution_count
121 121
122 122 def _set_prompt_str(self,p_str,cache_def,no_cache_def):
123 123 if p_str is None:
124 124 if self.do_full_cache:
125 125 return cache_def
126 126 else:
127 127 return no_cache_def
128 128 else:
129 129 return p_str
130 130
131 131 def set_colors(self, colors):
132 132 """Set the active color scheme and configure colors for the three
133 133 prompt subsystems."""
134 134
135 135 # FIXME: This modifying of the global prompts.prompt_specials needs
136 136 # to be fixed. We need to refactor all of the prompts stuff to use
137 137 # proper configuration and traits notifications.
138 138 if colors.lower()=='nocolor':
139 139 prompts.prompt_specials = prompts.prompt_specials_nocolor
140 140 else:
141 141 prompts.prompt_specials = prompts.prompt_specials_color
142 142
143 143 self.color_table.set_active_scheme(colors)
144 144 self.prompt1.set_colors()
145 145 self.prompt2.set_colors()
146 146 self.prompt_out.set_colors()
147 147
148 148 #-------------------------------------------------------------------------
149 149 # Methods used in __call__. Override these methods to modify the behavior
150 150 # of the displayhook.
151 151 #-------------------------------------------------------------------------
152 152
153 153 def check_for_underscore(self):
154 154 """Check if the user has set the '_' variable by hand."""
155 155 # If something injected a '_' variable in __builtin__, delete
156 156 # ipython's automatic one so we don't clobber that. gettext() in
157 157 # particular uses _, so we need to stay away from it.
158 158 if '_' in __builtin__.__dict__:
159 159 try:
160 160 del self.shell.user_ns['_']
161 161 except KeyError:
162 162 pass
163 163
164 164 def quiet(self):
165 165 """Should we silence the display hook because of ';'?"""
166 166 # do not print output if input ends in ';'
167 167 try:
168 168 if self.shell.input_hist[self.prompt_count].endswith(';\n'):
169 169 return True
170 170 except IndexError:
171 171 # some uses of ipshellembed may fail here
172 172 pass
173 173 return False
174 174
175 175 def start_displayhook(self):
176 176 """Start the displayhook, initializing resources."""
177 177 pass
178 178
179 179 def write_output_prompt(self):
180 180 """Write the output prompt."""
181 181 # Use write, not print which adds an extra space.
182 182 IPython.utils.io.Term.cout.write(self.output_sep)
183 183 outprompt = str(self.prompt_out)
184 184 if self.do_full_cache:
185 185 IPython.utils.io.Term.cout.write(outprompt)
186 186
187 187 # TODO: Make this method an extension point. The previous implementation
188 188 # has both a result_display hook as well as a result_display generic
189 189 # function to customize the repr on a per class basis. We need to rethink
190 190 # the hooks mechanism before doing this though.
191 191 def compute_result_repr(self, result):
192 192 """Compute and return the repr of the object to be displayed.
193 193
194 194 This method only compute the string form of the repr and should NOT
195 195 actual print or write that to a stream. This method may also transform
196 196 the result itself, but the default implementation passes the original
197 197 through.
198 198 """
199 199 try:
200 200 if self.shell.pprint:
201 201 result_repr = pformat(result)
202 202 if '\n' in result_repr:
203 203 # So that multi-line strings line up with the left column of
204 204 # the screen, instead of having the output prompt mess up
205 205 # their first line.
206 206 result_repr = '\n' + result_repr
207 207 else:
208 208 result_repr = repr(result)
209 209 except TypeError:
210 210 # This happens when result.__repr__ doesn't return a string,
211 211 # such as when it returns None.
212 212 result_repr = '\n'
213 213 return result, result_repr
214 214
215 215 def write_result_repr(self, result_repr):
216 216 # We want to print because we want to always make sure we have a
217 217 # newline, even if all the prompt separators are ''. This is the
218 218 # standard IPython behavior.
219 219 print >>IPython.utils.io.Term.cout, result_repr
220 220
221 221 def update_user_ns(self, result):
222 222 """Update user_ns with various things like _, __, _1, etc."""
223 223
224 224 # Avoid recursive reference when displaying _oh/Out
225 225 if result is not self.shell.user_ns['_oh']:
226 226 if len(self.shell.user_ns['_oh']) >= self.cache_size and self.do_full_cache:
227 227 warn('Output cache limit (currently '+
228 228 `self.cache_size`+' entries) hit.\n'
229 229 'Flushing cache and resetting history counter...\n'
230 230 'The only history variables available will be _,__,___ and _1\n'
231 231 'with the current result.')
232 232
233 233 self.flush()
234 234 # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise
235 235 # we cause buggy behavior for things like gettext).
236 236 if '_' not in __builtin__.__dict__:
237 237 self.___ = self.__
238 238 self.__ = self._
239 239 self._ = result
240 240 self.shell.user_ns.update({'_':self._,'__':self.__,'___':self.___})
241 241
242 242 # hackish access to top-level namespace to create _1,_2... dynamically
243 243 to_main = {}
244 244 if self.do_full_cache:
245 245 new_result = '_'+`self.prompt_count`
246 246 to_main[new_result] = result
247 247 self.shell.user_ns.update(to_main)
248 248 self.shell.user_ns['_oh'][self.prompt_count] = result
249 249
250 250 def log_output(self, result):
251 251 """Log the output."""
252 252 if self.shell.logger.log_output:
253 self.shell.logger.log_write(repr(result),'output')
253 self.shell.logger.log_write(repr(result), 'output')
254 254
255 255 def finish_displayhook(self):
256 256 """Finish up all displayhook activities."""
257 257 IPython.utils.io.Term.cout.write(self.output_sep2)
258 258 IPython.utils.io.Term.cout.flush()
259 259
260 260 def __call__(self, result=None):
261 261 """Printing with history cache management.
262 262
263 263 This is invoked everytime the interpreter needs to print, and is
264 264 activated by setting the variable sys.displayhook to it.
265 265 """
266 266 self.check_for_underscore()
267 267 if result is not None and not self.quiet():
268 268 self.start_displayhook()
269 269 self.write_output_prompt()
270 270 result, result_repr = self.compute_result_repr(result)
271 271 self.write_result_repr(result_repr)
272 272 self.update_user_ns(result)
273 273 self.log_output(result)
274 274 self.finish_displayhook()
275 275
276 276 def flush(self):
277 277 if not self.do_full_cache:
278 278 raise ValueError,"You shouldn't have reached the cache flush "\
279 279 "if full caching is not enabled!"
280 280 # delete auto-generated vars from global namespace
281 281
282 282 for n in range(1,self.prompt_count + 1):
283 283 key = '_'+`n`
284 284 try:
285 285 del self.shell.user_ns[key]
286 286 except: pass
287 287 self.shell.user_ns['_oh'].clear()
288 288
289 289 if '_' not in __builtin__.__dict__:
290 290 self.shell.user_ns.update({'_':None,'__':None, '___':None})
291 291 import gc
292 292 gc.collect() # xxx needed?
293 293
@@ -1,461 +1,484 b''
1 1 """ History related magics and functionality """
2 2 #-----------------------------------------------------------------------------
3 3 # Copyright (C) 2010 The IPython Development Team.
4 4 #
5 5 # Distributed under the terms of the BSD License.
6 6 #
7 7 # The full license is in the file COPYING.txt, distributed with this software.
8 8 #-----------------------------------------------------------------------------
9 9
10 10 #-----------------------------------------------------------------------------
11 11 # Imports
12 12 #-----------------------------------------------------------------------------
13 13 from __future__ import print_function
14 14
15 15 # Stdlib imports
16 16 import fnmatch
17 17 import os
18 18 import sys
19 19
20 20 # Our own packages
21 21 import IPython.utils.io
22 22
23 23 from IPython.core import ipapi
24 24 from IPython.core.inputlist import InputList
25 25 from IPython.utils.pickleshare import PickleShareDB
26 26 from IPython.utils.io import ask_yes_no
27 27 from IPython.utils.warn import warn
28 28
29 29 #-----------------------------------------------------------------------------
30 30 # Classes and functions
31 31 #-----------------------------------------------------------------------------
32 32
33 33 class HistoryManager(object):
34 34 """A class to organize all history-related functionality in one place.
35 35 """
36 36 def __init__(self, shell):
37 37 """Create a new history manager associated with a shell instance.
38 38 """
39 # We need a pointer back to the shell for various tasks.
39 40 self.shell = shell
40 41
41 42 # List of input with multi-line handling.
42 43 self.input_hist = InputList()
43 44 # This one will hold the 'raw' input history, without any
44 45 # pre-processing. This will allow users to retrieve the input just as
45 46 # it was exactly typed in by the user, with %hist -r.
46 47 self.input_hist_raw = InputList()
47 48
48 49 # list of visited directories
49 50 try:
50 51 self.dir_hist = [os.getcwd()]
51 52 except OSError:
52 53 self.dir_hist = []
53 54
54 55 # dict of output history
55 56 self.output_hist = {}
56 57
57 58 # Now the history file
58 59 if shell.profile:
59 60 histfname = 'history-%s' % shell.profile
60 61 else:
61 62 histfname = 'history'
62 63 self.hist_file = os.path.join(shell.ipython_dir, histfname)
63 64
64 65 # Objects related to shadow history management
65 66 self._init_shadow_hist()
66 67
68 # Variables used to store the three last inputs from the user. On each
69 # new history update, we populate the user's namespace with these,
70 # shifted as necessary.
71 self._i00, self._i, self._ii, self._iii = '','','',''
72
73 # Object is fully initialized, we can now call methods on it.
74
67 75 # Fill the history zero entry, user counter starts at 1
68 76 self.store_inputs('\n', '\n')
69 77
70 78 # For backwards compatibility, we must put these back in the shell
71 79 # object, until we've removed all direct uses of the history objects in
72 80 # the shell itself.
73 81 shell.input_hist = self.input_hist
74 82 shell.input_hist_raw = self.input_hist_raw
75 83 shell.output_hist = self.output_hist
76 84 shell.dir_hist = self.dir_hist
77 85 shell.histfile = self.hist_file
78 86 shell.shadowhist = self.shadow_hist
79 87 shell.db = self.shadow_db
80 88
81 89 def _init_shadow_hist(self):
82 90 try:
83 91 self.shadow_db = PickleShareDB(os.path.join(
84 92 self.shell.ipython_dir, 'db'))
85 93 except UnicodeDecodeError:
86 94 print("Your ipython_dir can't be decoded to unicode!")
87 95 print("Please set HOME environment variable to something that")
88 96 print(r"only has ASCII characters, e.g. c:\home")
89 97 print("Now it is", self.ipython_dir)
90 98 sys.exit()
91 99 self.shadow_hist = ShadowHist(self.shadow_db)
92 100
93 101 def save_hist(self):
94 102 """Save input history to a file (via readline library)."""
95 103
96 104 try:
97 105 self.shell.readline.write_history_file(self.hist_file)
98 106 except:
99 107 print('Unable to save IPython command history to file: ' +
100 108 `self.hist_file`)
101 109
102 110 def reload_hist(self):
103 111 """Reload the input history from disk file."""
104 112
105 113 try:
106 114 self.shell.readline.clear_history()
107 115 self.shell.readline.read_history_file(self.hist_file)
108 116 except AttributeError:
109 117 pass
110 118
111 119 def get_history(self, index=None, raw=False, output=True):
112 120 """Get the history list.
113 121
114 122 Get the input and output history.
115 123
116 124 Parameters
117 125 ----------
118 126 index : n or (n1, n2) or None
119 127 If n, then the last entries. If a tuple, then all in
120 128 range(n1, n2). If None, then all entries. Raises IndexError if
121 129 the format of index is incorrect.
122 130 raw : bool
123 131 If True, return the raw input.
124 132 output : bool
125 133 If True, then return the output as well.
126 134
127 135 Returns
128 136 -------
129 137 If output is True, then return a dict of tuples, keyed by the prompt
130 138 numbers and with values of (input, output). If output is False, then
131 139 a dict, keyed by the prompt number with the values of input. Raises
132 140 IndexError if no history is found.
133 141 """
134 142 if raw:
135 143 input_hist = self.input_hist_raw
136 144 else:
137 145 input_hist = self.input_hist
138 146 if output:
139 147 output_hist = self.output_hist
140 148 n = len(input_hist)
141 149 if index is None:
142 150 start=0; stop=n
143 151 elif isinstance(index, int):
144 152 start=n-index; stop=n
145 153 elif isinstance(index, tuple) and len(index) == 2:
146 154 start=index[0]; stop=index[1]
147 155 else:
148 156 raise IndexError('Not a valid index for the input history: %r'
149 157 % index)
150 158 hist = {}
151 159 for i in range(start, stop):
152 160 if output:
153 161 hist[i] = (input_hist[i], output_hist.get(i))
154 162 else:
155 163 hist[i] = input_hist[i]
156 164 if not hist:
157 165 raise IndexError('No history for range of indices: %r' % index)
158 166 return hist
159 167
160 168 def store_inputs(self, source, source_raw=None):
161 """Store source and raw input in history.
162
169 """Store source and raw input in history and create input cache
170 variables _i*.
171
163 172 Parameters
164 173 ----------
165 174 source : str
166 175 Python input.
167 176
168 177 source_raw : str, optional
169 178 If given, this is the raw input without any IPython transformations
170 179 applied to it. If not given, ``source`` is used.
171 180 """
172 181 if source_raw is None:
173 182 source_raw = source
174 183 self.input_hist.append(source)
175 184 self.input_hist_raw.append(source_raw)
176 185 self.shadow_hist.add(source)
177 186
187 # update the auto _i variables
188 self._iii = self._ii
189 self._ii = self._i
190 self._i = self._i00
191 self._i00 = source_raw
192
193 # hackish access to user namespace to create _i1,_i2... dynamically
194 new_i = '_i%s' % self.shell.execution_count
195 to_main = {'_i': self._i,
196 '_ii': self._ii,
197 '_iii': self._iii,
198 new_i : self._i00 }
199 self.shell.user_ns.update(to_main)
200
178 201 def sync_inputs(self):
179 202 """Ensure raw and translated histories have same length."""
180 203 if len(self.input_hist) != len (self.input_hist_raw):
181 204 self.input_hist_raw = InputList(self.input_hist)
182 205
183 206 def reset(self):
184 207 """Clear all histories managed by this object."""
185 208 self.input_hist[:] = []
186 209 self.input_hist_raw[:] = []
187 210 self.output_hist.clear()
188 211 # The directory history can't be completely empty
189 212 self.dir_hist[:] = [os.getcwd()]
190 213
191 214
192 215 def magic_history(self, parameter_s = ''):
193 216 """Print input history (_i<n> variables), with most recent last.
194 217
195 218 %history -> print at most 40 inputs (some may be multi-line)\\
196 219 %history n -> print at most n inputs\\
197 220 %history n1 n2 -> print inputs between n1 and n2 (n2 not included)\\
198 221
199 222 By default, input history is printed without line numbers so it can be
200 223 directly pasted into an editor.
201 224
202 225 With -n, each input's number <n> is shown, and is accessible as the
203 226 automatically generated variable _i<n> as well as In[<n>]. Multi-line
204 227 statements are printed starting at a new line for easy copy/paste.
205 228
206 229 Options:
207 230
208 231 -n: print line numbers for each input.
209 232 This feature is only available if numbered prompts are in use.
210 233
211 234 -o: also print outputs for each input.
212 235
213 236 -p: print classic '>>>' python prompts before each input. This is useful
214 237 for making documentation, and in conjunction with -o, for producing
215 238 doctest-ready output.
216 239
217 240 -r: (default) print the 'raw' history, i.e. the actual commands you typed.
218 241
219 242 -t: print the 'translated' history, as IPython understands it. IPython
220 243 filters your input and converts it all into valid Python source before
221 244 executing it (things like magics or aliases are turned into function
222 245 calls, for example). With this option, you'll see the native history
223 246 instead of the user-entered version: '%cd /' will be seen as
224 247 'get_ipython().magic("%cd /")' instead of '%cd /'.
225 248
226 249 -g: treat the arg as a pattern to grep for in (full) history.
227 250 This includes the "shadow history" (almost all commands ever written).
228 251 Use '%hist -g' to show full shadow history (may be very long).
229 252 In shadow history, every index nuwber starts with 0.
230 253
231 254 -f FILENAME: instead of printing the output to the screen, redirect it to
232 255 the given file. The file is always overwritten, though IPython asks for
233 256 confirmation first if it already exists.
234 257 """
235 258
236 259 if not self.shell.displayhook.do_full_cache:
237 260 print('This feature is only available if numbered prompts are in use.')
238 261 return
239 262 opts,args = self.parse_options(parameter_s,'gnoptsrf:',mode='list')
240 263
241 264 # Check if output to specific file was requested.
242 265 try:
243 266 outfname = opts['f']
244 267 except KeyError:
245 268 outfile = IPython.utils.io.Term.cout # default
246 269 # We don't want to close stdout at the end!
247 270 close_at_end = False
248 271 else:
249 272 if os.path.exists(outfname):
250 273 if not ask_yes_no("File %r exists. Overwrite?" % outfname):
251 274 print('Aborting.')
252 275 return
253 276
254 277 outfile = open(outfname,'w')
255 278 close_at_end = True
256 279
257 280 if 't' in opts:
258 281 input_hist = self.shell.input_hist
259 282 elif 'r' in opts:
260 283 input_hist = self.shell.input_hist_raw
261 284 else:
262 285 # Raw history is the default
263 286 input_hist = self.shell.input_hist_raw
264 287
265 288 default_length = 40
266 289 pattern = None
267 290 if 'g' in opts:
268 291 init = 1
269 292 final = len(input_hist)
270 293 parts = parameter_s.split(None, 1)
271 294 if len(parts) == 1:
272 295 parts += '*'
273 296 head, pattern = parts
274 297 pattern = "*" + pattern + "*"
275 298 elif len(args) == 0:
276 299 final = len(input_hist)-1
277 300 init = max(1,final-default_length)
278 301 elif len(args) == 1:
279 302 final = len(input_hist)
280 303 init = max(1, final-int(args[0]))
281 304 elif len(args) == 2:
282 305 init, final = map(int, args)
283 306 else:
284 307 warn('%hist takes 0, 1 or 2 arguments separated by spaces.')
285 308 print(self.magic_hist.__doc__, file=IPython.utils.io.Term.cout)
286 309 return
287 310
288 311 width = len(str(final))
289 312 line_sep = ['','\n']
290 313 print_nums = 'n' in opts
291 314 print_outputs = 'o' in opts
292 315 pyprompts = 'p' in opts
293 316
294 317 found = False
295 318 if pattern is not None:
296 319 sh = self.shell.shadowhist.all()
297 320 for idx, s in sh:
298 321 if fnmatch.fnmatch(s, pattern):
299 322 print("0%d: %s" %(idx, s.expandtabs(4)), file=outfile)
300 323 found = True
301 324
302 325 if found:
303 326 print("===", file=outfile)
304 327 print("shadow history ends, fetch by %rep <number> (must start with 0)",
305 328 file=outfile)
306 329 print("=== start of normal history ===", file=outfile)
307 330
308 331 for in_num in range(init, final):
309 332 # Print user history with tabs expanded to 4 spaces. The GUI clients
310 333 # use hard tabs for easier usability in auto-indented code, but we want
311 334 # to produce PEP-8 compliant history for safe pasting into an editor.
312 335 inline = input_hist[in_num].expandtabs(4)
313 336
314 337 if pattern is not None and not fnmatch.fnmatch(inline, pattern):
315 338 continue
316 339
317 340 multiline = int(inline.count('\n') > 1)
318 341 if print_nums:
319 342 print('%s:%s' % (str(in_num).ljust(width), line_sep[multiline]),
320 343 file=outfile)
321 344 if pyprompts:
322 345 print('>>>', file=outfile)
323 346 if multiline:
324 347 lines = inline.splitlines()
325 348 print('\n... '.join(lines), file=outfile)
326 349 print('... ', file=outfile)
327 350 else:
328 351 print(inline, end='', file=outfile)
329 352 else:
330 353 print(inline,end='', file=outfile)
331 354 if print_outputs:
332 355 output = self.shell.output_hist.get(in_num)
333 356 if output is not None:
334 357 print(repr(output), file=outfile)
335 358
336 359 if close_at_end:
337 360 outfile.close()
338 361
339 362
340 363 def magic_hist(self, parameter_s=''):
341 364 """Alternate name for %history."""
342 365 return self.magic_history(parameter_s)
343 366
344 367
345 368 def rep_f(self, arg):
346 369 r""" Repeat a command, or get command to input line for editing
347 370
348 371 - %rep (no arguments):
349 372
350 373 Place a string version of last computation result (stored in the special '_'
351 374 variable) to the next input prompt. Allows you to create elaborate command
352 375 lines without using copy-paste::
353 376
354 377 $ l = ["hei", "vaan"]
355 378 $ "".join(l)
356 379 ==> heivaan
357 380 $ %rep
358 381 $ heivaan_ <== cursor blinking
359 382
360 383 %rep 45
361 384
362 385 Place history line 45 to next input prompt. Use %hist to find out the
363 386 number.
364 387
365 388 %rep 1-4 6-7 3
366 389
367 390 Repeat the specified lines immediately. Input slice syntax is the same as
368 391 in %macro and %save.
369 392
370 393 %rep foo
371 394
372 395 Place the most recent line that has the substring "foo" to next input.
373 396 (e.g. 'svn ci -m foobar').
374 397 """
375 398
376 399 opts,args = self.parse_options(arg,'',mode='list')
377 400 if not args:
378 401 self.set_next_input(str(self.shell.user_ns["_"]))
379 402 return
380 403
381 404 if len(args) == 1 and not '-' in args[0]:
382 405 arg = args[0]
383 406 if len(arg) > 1 and arg.startswith('0'):
384 407 # get from shadow hist
385 408 num = int(arg[1:])
386 409 line = self.shell.shadowhist.get(num)
387 410 self.set_next_input(str(line))
388 411 return
389 412 try:
390 413 num = int(args[0])
391 414 self.set_next_input(str(self.shell.input_hist_raw[num]).rstrip())
392 415 return
393 416 except ValueError:
394 417 pass
395 418
396 419 for h in reversed(self.shell.input_hist_raw):
397 420 if 'rep' in h:
398 421 continue
399 422 if fnmatch.fnmatch(h,'*' + arg + '*'):
400 423 self.set_next_input(str(h).rstrip())
401 424 return
402 425
403 426 try:
404 427 lines = self.extract_input_slices(args, True)
405 428 print("lines", lines)
406 self.runlines(lines)
429 self.run_cell(lines)
407 430 except ValueError:
408 431 print("Not found in recent history:", args)
409 432
410 433
411 434 _sentinel = object()
412 435
413 436 class ShadowHist(object):
414 437 def __init__(self, db):
415 438 # cmd => idx mapping
416 439 self.curidx = 0
417 440 self.db = db
418 441 self.disabled = False
419 442
420 443 def inc_idx(self):
421 444 idx = self.db.get('shadowhist_idx', 1)
422 445 self.db['shadowhist_idx'] = idx + 1
423 446 return idx
424 447
425 448 def add(self, ent):
426 449 if self.disabled:
427 450 return
428 451 try:
429 452 old = self.db.hget('shadowhist', ent, _sentinel)
430 453 if old is not _sentinel:
431 454 return
432 455 newidx = self.inc_idx()
433 456 #print("new", newidx) # dbg
434 457 self.db.hset('shadowhist',ent, newidx)
435 458 except:
436 459 ipapi.get().showtraceback()
437 460 print("WARNING: disabling shadow history")
438 461 self.disabled = True
439 462
440 463 def all(self):
441 464 d = self.db.hdict('shadowhist')
442 465 items = [(i,s) for (s,i) in d.items()]
443 466 items.sort()
444 467 return items
445 468
446 469 def get(self, idx):
447 470 all = self.all()
448 471
449 472 for k, v in all:
450 473 if k == idx:
451 474 return v
452 475
453 476
454 477 def init_ipython(ip):
455 478 ip.define_magic("rep",rep_f)
456 479 ip.define_magic("hist",magic_hist)
457 480 ip.define_magic("history",magic_history)
458 481
459 482 # XXX - ipy_completers are in quarantine, need to be updated to new apis
460 483 #import ipy_completers
461 484 #ipy_completers.quick_completer('%hist' ,'-g -t -r -n')
@@ -1,2516 +1,2516 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Main IPython class."""
3 3
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
6 6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
7 7 # Copyright (C) 2008-2010 The IPython Development Team
8 8 #
9 9 # Distributed under the terms of the BSD License. The full license is in
10 10 # the file COPYING, distributed as part of this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16
17 17 from __future__ import with_statement
18 18 from __future__ import absolute_import
19 19
20 20 import __builtin__
21 21 import __future__
22 22 import abc
23 23 import atexit
24 24 import codeop
25 25 import exceptions
26 26 import new
27 27 import os
28 28 import re
29 29 import string
30 30 import sys
31 31 import tempfile
32 32 from contextlib import nested
33 33
34 34 from IPython.config.configurable import Configurable
35 35 from IPython.core import debugger, oinspect
36 36 from IPython.core import history as ipcorehist
37 37 from IPython.core import page
38 38 from IPython.core import prefilter
39 39 from IPython.core import shadowns
40 40 from IPython.core import ultratb
41 41 from IPython.core.alias import AliasManager
42 42 from IPython.core.builtin_trap import BuiltinTrap
43 43 from IPython.core.display_trap import DisplayTrap
44 44 from IPython.core.displayhook import DisplayHook
45 45 from IPython.core.error import TryNext, UsageError
46 46 from IPython.core.extensions import ExtensionManager
47 47 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
48 48 from IPython.core.history import HistoryManager
49 49 from IPython.core.inputlist import InputList
50 50 from IPython.core.inputsplitter import IPythonInputSplitter
51 51 from IPython.core.logger import Logger
52 52 from IPython.core.magic import Magic
53 53 from IPython.core.payload import PayloadManager
54 54 from IPython.core.plugin import PluginManager
55 55 from IPython.core.prefilter import PrefilterManager, ESC_MAGIC
56 56 from IPython.external.Itpl import ItplNS
57 57 from IPython.utils import PyColorize
58 58 from IPython.utils import io
59 59 from IPython.utils import pickleshare
60 60 from IPython.utils.doctestreload import doctest_reload
61 61 from IPython.utils.io import ask_yes_no, rprint
62 62 from IPython.utils.ipstruct import Struct
63 63 from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError
64 64 from IPython.utils.process import system, getoutput
65 65 from IPython.utils.strdispatch import StrDispatch
66 66 from IPython.utils.syspathcontext import prepended_to_syspath
67 67 from IPython.utils.text import num_ini_spaces, format_screen, LSString, SList
68 68 from IPython.utils.traitlets import (Int, Str, CBool, CaselessStrEnum, Enum,
69 69 List, Unicode, Instance, Type)
70 70 from IPython.utils.warn import warn, error, fatal
71 71 import IPython.core.hooks
72 72
73 73 #-----------------------------------------------------------------------------
74 74 # Globals
75 75 #-----------------------------------------------------------------------------
76 76
77 77 # compiled regexps for autoindent management
78 78 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
79 79
80 80 #-----------------------------------------------------------------------------
81 81 # Utilities
82 82 #-----------------------------------------------------------------------------
83 83
84 84 # store the builtin raw_input globally, and use this always, in case user code
85 85 # overwrites it (like wx.py.PyShell does)
86 86 raw_input_original = raw_input
87 87
88 88 def softspace(file, newvalue):
89 89 """Copied from code.py, to remove the dependency"""
90 90
91 91 oldvalue = 0
92 92 try:
93 93 oldvalue = file.softspace
94 94 except AttributeError:
95 95 pass
96 96 try:
97 97 file.softspace = newvalue
98 98 except (AttributeError, TypeError):
99 99 # "attribute-less object" or "read-only attributes"
100 100 pass
101 101 return oldvalue
102 102
103 103
104 104 def no_op(*a, **kw): pass
105 105
106 106 class SpaceInInput(exceptions.Exception): pass
107 107
108 108 class Bunch: pass
109 109
110 110
111 111 def get_default_colors():
112 112 if sys.platform=='darwin':
113 113 return "LightBG"
114 114 elif os.name=='nt':
115 115 return 'Linux'
116 116 else:
117 117 return 'Linux'
118 118
119 119
120 120 class SeparateStr(Str):
121 121 """A Str subclass to validate separate_in, separate_out, etc.
122 122
123 123 This is a Str based trait that converts '0'->'' and '\\n'->'\n'.
124 124 """
125 125
126 126 def validate(self, obj, value):
127 127 if value == '0': value = ''
128 128 value = value.replace('\\n','\n')
129 129 return super(SeparateStr, self).validate(obj, value)
130 130
131 131 class MultipleInstanceError(Exception):
132 132 pass
133 133
134 134
135 135 #-----------------------------------------------------------------------------
136 136 # Main IPython class
137 137 #-----------------------------------------------------------------------------
138 138
139 139
140 140 class InteractiveShell(Configurable, Magic):
141 141 """An enhanced, interactive shell for Python."""
142 142
143 143 _instance = None
144 144 autocall = Enum((0,1,2), default_value=1, config=True)
145 145 # TODO: remove all autoindent logic and put into frontends.
146 146 # We can't do this yet because even runlines uses the autoindent.
147 147 autoindent = CBool(True, config=True)
148 148 automagic = CBool(True, config=True)
149 149 cache_size = Int(1000, config=True)
150 150 color_info = CBool(True, config=True)
151 151 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
152 152 default_value=get_default_colors(), config=True)
153 153 debug = CBool(False, config=True)
154 154 deep_reload = CBool(False, config=True)
155 155 displayhook_class = Type(DisplayHook)
156 156 exit_now = CBool(False)
157 157 filename = Str("<ipython console>")
158 158 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
159 159
160 160 # Input splitter, to split entire cells of input into either individual
161 161 # interactive statements or whole blocks.
162 162 input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
163 163 (), {})
164 164 logstart = CBool(False, config=True)
165 165 logfile = Str('', config=True)
166 166 logappend = Str('', config=True)
167 167 object_info_string_level = Enum((0,1,2), default_value=0,
168 168 config=True)
169 169 pdb = CBool(False, config=True)
170 170
171 171 pprint = CBool(True, config=True)
172 172 profile = Str('', config=True)
173 173 prompt_in1 = Str('In [\\#]: ', config=True)
174 174 prompt_in2 = Str(' .\\D.: ', config=True)
175 175 prompt_out = Str('Out[\\#]: ', config=True)
176 176 prompts_pad_left = CBool(True, config=True)
177 177 quiet = CBool(False, config=True)
178 178
179 179 # The readline stuff will eventually be moved to the terminal subclass
180 180 # but for now, we can't do that as readline is welded in everywhere.
181 181 readline_use = CBool(True, config=True)
182 182 readline_merge_completions = CBool(True, config=True)
183 183 readline_omit__names = Enum((0,1,2), default_value=0, config=True)
184 184 readline_remove_delims = Str('-/~', config=True)
185 185 readline_parse_and_bind = List([
186 186 'tab: complete',
187 187 '"\C-l": clear-screen',
188 188 'set show-all-if-ambiguous on',
189 189 '"\C-o": tab-insert',
190 190 '"\M-i": " "',
191 191 '"\M-o": "\d\d\d\d"',
192 192 '"\M-I": "\d\d\d\d"',
193 193 '"\C-r": reverse-search-history',
194 194 '"\C-s": forward-search-history',
195 195 '"\C-p": history-search-backward',
196 196 '"\C-n": history-search-forward',
197 197 '"\e[A": history-search-backward',
198 198 '"\e[B": history-search-forward',
199 199 '"\C-k": kill-line',
200 200 '"\C-u": unix-line-discard',
201 201 ], allow_none=False, config=True)
202 202
203 203 # TODO: this part of prompt management should be moved to the frontends.
204 204 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
205 205 separate_in = SeparateStr('\n', config=True)
206 206 separate_out = SeparateStr('', config=True)
207 207 separate_out2 = SeparateStr('', config=True)
208 208 wildcards_case_sensitive = CBool(True, config=True)
209 209 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
210 210 default_value='Context', config=True)
211 211
212 212 # Subcomponents of InteractiveShell
213 213 alias_manager = Instance('IPython.core.alias.AliasManager')
214 214 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
215 215 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap')
216 216 display_trap = Instance('IPython.core.display_trap.DisplayTrap')
217 217 extension_manager = Instance('IPython.core.extensions.ExtensionManager')
218 218 plugin_manager = Instance('IPython.core.plugin.PluginManager')
219 219 payload_manager = Instance('IPython.core.payload.PayloadManager')
220 220 history_manager = Instance('IPython.core.history.HistoryManager')
221 221
222 222 # Private interface
223 223 _post_execute = set()
224 224
225 225 def __init__(self, config=None, ipython_dir=None,
226 226 user_ns=None, user_global_ns=None,
227 227 custom_exceptions=((), None)):
228 228
229 229 # This is where traits with a config_key argument are updated
230 230 # from the values on config.
231 231 super(InteractiveShell, self).__init__(config=config)
232 232
233 233 # These are relatively independent and stateless
234 234 self.init_ipython_dir(ipython_dir)
235 235 self.init_instance_attrs()
236 236 self.init_environment()
237 237
238 238 # Create namespaces (user_ns, user_global_ns, etc.)
239 239 self.init_create_namespaces(user_ns, user_global_ns)
240 240 # This has to be done after init_create_namespaces because it uses
241 241 # something in self.user_ns, but before init_sys_modules, which
242 242 # is the first thing to modify sys.
243 243 # TODO: When we override sys.stdout and sys.stderr before this class
244 244 # is created, we are saving the overridden ones here. Not sure if this
245 245 # is what we want to do.
246 246 self.save_sys_module_state()
247 247 self.init_sys_modules()
248 248
249 249 self.init_history()
250 250 self.init_encoding()
251 251 self.init_prefilter()
252 252
253 253 Magic.__init__(self, self)
254 254
255 255 self.init_syntax_highlighting()
256 256 self.init_hooks()
257 257 self.init_pushd_popd_magic()
258 258 # self.init_traceback_handlers use to be here, but we moved it below
259 259 # because it and init_io have to come after init_readline.
260 260 self.init_user_ns()
261 261 self.init_logger()
262 262 self.init_alias()
263 263 self.init_builtins()
264 264
265 265 # pre_config_initialization
266 266
267 267 # The next section should contain everything that was in ipmaker.
268 268 self.init_logstart()
269 269
270 270 # The following was in post_config_initialization
271 271 self.init_inspector()
272 272 # init_readline() must come before init_io(), because init_io uses
273 273 # readline related things.
274 274 self.init_readline()
275 275 # init_completer must come after init_readline, because it needs to
276 276 # know whether readline is present or not system-wide to configure the
277 277 # completers, since the completion machinery can now operate
278 278 # independently of readline (e.g. over the network)
279 279 self.init_completer()
280 280 # TODO: init_io() needs to happen before init_traceback handlers
281 281 # because the traceback handlers hardcode the stdout/stderr streams.
282 282 # This logic in in debugger.Pdb and should eventually be changed.
283 283 self.init_io()
284 284 self.init_traceback_handlers(custom_exceptions)
285 285 self.init_prompts()
286 286 self.init_displayhook()
287 287 self.init_reload_doctest()
288 288 self.init_magics()
289 289 self.init_pdb()
290 290 self.init_extension_manager()
291 291 self.init_plugin_manager()
292 292 self.init_payload()
293 293 self.hooks.late_startup_hook()
294 294 atexit.register(self.atexit_operations)
295 295
296 296 @classmethod
297 297 def instance(cls, *args, **kwargs):
298 298 """Returns a global InteractiveShell instance."""
299 299 if cls._instance is None:
300 300 inst = cls(*args, **kwargs)
301 301 # Now make sure that the instance will also be returned by
302 302 # the subclasses instance attribute.
303 303 for subclass in cls.mro():
304 304 if issubclass(cls, subclass) and \
305 305 issubclass(subclass, InteractiveShell):
306 306 subclass._instance = inst
307 307 else:
308 308 break
309 309 if isinstance(cls._instance, cls):
310 310 return cls._instance
311 311 else:
312 312 raise MultipleInstanceError(
313 313 'Multiple incompatible subclass instances of '
314 314 'InteractiveShell are being created.'
315 315 )
316 316
317 317 @classmethod
318 318 def initialized(cls):
319 319 return hasattr(cls, "_instance")
320 320
321 321 def get_ipython(self):
322 322 """Return the currently running IPython instance."""
323 323 return self
324 324
325 325 #-------------------------------------------------------------------------
326 326 # Trait changed handlers
327 327 #-------------------------------------------------------------------------
328 328
329 329 def _ipython_dir_changed(self, name, new):
330 330 if not os.path.isdir(new):
331 331 os.makedirs(new, mode = 0777)
332 332
333 333 def set_autoindent(self,value=None):
334 334 """Set the autoindent flag, checking for readline support.
335 335
336 336 If called with no arguments, it acts as a toggle."""
337 337
338 338 if not self.has_readline:
339 339 if os.name == 'posix':
340 340 warn("The auto-indent feature requires the readline library")
341 341 self.autoindent = 0
342 342 return
343 343 if value is None:
344 344 self.autoindent = not self.autoindent
345 345 else:
346 346 self.autoindent = value
347 347
348 348 #-------------------------------------------------------------------------
349 349 # init_* methods called by __init__
350 350 #-------------------------------------------------------------------------
351 351
352 352 def init_ipython_dir(self, ipython_dir):
353 353 if ipython_dir is not None:
354 354 self.ipython_dir = ipython_dir
355 355 self.config.Global.ipython_dir = self.ipython_dir
356 356 return
357 357
358 358 if hasattr(self.config.Global, 'ipython_dir'):
359 359 self.ipython_dir = self.config.Global.ipython_dir
360 360 else:
361 361 self.ipython_dir = get_ipython_dir()
362 362
363 363 # All children can just read this
364 364 self.config.Global.ipython_dir = self.ipython_dir
365 365
366 366 def init_instance_attrs(self):
367 367 self.more = False
368 368
369 369 # command compiler
370 370 self.compile = codeop.CommandCompiler()
371 371
372 372 # User input buffers
373 373 self.buffer = []
374 374 self.buffer_raw = []
375 375
376 376 # Make an empty namespace, which extension writers can rely on both
377 377 # existing and NEVER being used by ipython itself. This gives them a
378 378 # convenient location for storing additional information and state
379 379 # their extensions may require, without fear of collisions with other
380 380 # ipython names that may develop later.
381 381 self.meta = Struct()
382 382
383 383 # Object variable to store code object waiting execution. This is
384 384 # used mainly by the multithreaded shells, but it can come in handy in
385 385 # other situations. No need to use a Queue here, since it's a single
386 386 # item which gets cleared once run.
387 387 self.code_to_run = None
388 388
389 389 # Temporary files used for various purposes. Deleted at exit.
390 390 self.tempfiles = []
391 391
392 392 # Keep track of readline usage (later set by init_readline)
393 393 self.has_readline = False
394 394
395 395 # keep track of where we started running (mainly for crash post-mortem)
396 396 # This is not being used anywhere currently.
397 397 self.starting_dir = os.getcwd()
398 398
399 399 # Indentation management
400 400 self.indent_current_nsp = 0
401 401
402 402 # Increasing execution counter
403 403 self.execution_count = 1
404 404
405 405 def init_environment(self):
406 406 """Any changes we need to make to the user's environment."""
407 407 pass
408 408
409 409 def init_encoding(self):
410 410 # Get system encoding at startup time. Certain terminals (like Emacs
411 411 # under Win32 have it set to None, and we need to have a known valid
412 412 # encoding to use in the raw_input() method
413 413 try:
414 414 self.stdin_encoding = sys.stdin.encoding or 'ascii'
415 415 except AttributeError:
416 416 self.stdin_encoding = 'ascii'
417 417
418 418 def init_syntax_highlighting(self):
419 419 # Python source parser/formatter for syntax highlighting
420 420 pyformat = PyColorize.Parser().format
421 421 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
422 422
423 423 def init_pushd_popd_magic(self):
424 424 # for pushd/popd management
425 425 try:
426 426 self.home_dir = get_home_dir()
427 427 except HomeDirError, msg:
428 428 fatal(msg)
429 429
430 430 self.dir_stack = []
431 431
432 432 def init_logger(self):
433 self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate')
434 # local shortcut, this is used a LOT
435 self.log = self.logger.log
433 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
434 logmode='rotate')
436 435
437 436 def init_logstart(self):
437 """Initialize logging in case it was requested at the command line.
438 """
438 439 if self.logappend:
439 440 self.magic_logstart(self.logappend + ' append')
440 441 elif self.logfile:
441 442 self.magic_logstart(self.logfile)
442 443 elif self.logstart:
443 444 self.magic_logstart()
444 445
445 446 def init_builtins(self):
446 447 self.builtin_trap = BuiltinTrap(shell=self)
447 448
448 449 def init_inspector(self):
449 450 # Object inspector
450 451 self.inspector = oinspect.Inspector(oinspect.InspectColors,
451 452 PyColorize.ANSICodeColors,
452 453 'NoColor',
453 454 self.object_info_string_level)
454 455
455 456 def init_io(self):
456 457 # This will just use sys.stdout and sys.stderr. If you want to
457 458 # override sys.stdout and sys.stderr themselves, you need to do that
458 459 # *before* instantiating this class, because Term holds onto
459 460 # references to the underlying streams.
460 461 if sys.platform == 'win32' and self.has_readline:
461 462 Term = io.IOTerm(cout=self.readline._outputfile,
462 463 cerr=self.readline._outputfile)
463 464 else:
464 465 Term = io.IOTerm()
465 466 io.Term = Term
466 467
467 468 def init_prompts(self):
468 469 # TODO: This is a pass for now because the prompts are managed inside
469 470 # the DisplayHook. Once there is a separate prompt manager, this
470 471 # will initialize that object and all prompt related information.
471 472 pass
472 473
473 474 def init_displayhook(self):
474 475 # Initialize displayhook, set in/out prompts and printing system
475 476 self.displayhook = self.displayhook_class(
476 477 shell=self,
477 478 cache_size=self.cache_size,
478 479 input_sep = self.separate_in,
479 480 output_sep = self.separate_out,
480 481 output_sep2 = self.separate_out2,
481 482 ps1 = self.prompt_in1,
482 483 ps2 = self.prompt_in2,
483 484 ps_out = self.prompt_out,
484 485 pad_left = self.prompts_pad_left
485 486 )
486 487 # This is a context manager that installs/revmoes the displayhook at
487 488 # the appropriate time.
488 489 self.display_trap = DisplayTrap(hook=self.displayhook)
489 490
490 491 def init_reload_doctest(self):
491 492 # Do a proper resetting of doctest, including the necessary displayhook
492 493 # monkeypatching
493 494 try:
494 495 doctest_reload()
495 496 except ImportError:
496 497 warn("doctest module does not exist.")
497 498
498 499 #-------------------------------------------------------------------------
499 500 # Things related to injections into the sys module
500 501 #-------------------------------------------------------------------------
501 502
502 503 def save_sys_module_state(self):
503 504 """Save the state of hooks in the sys module.
504 505
505 506 This has to be called after self.user_ns is created.
506 507 """
507 508 self._orig_sys_module_state = {}
508 509 self._orig_sys_module_state['stdin'] = sys.stdin
509 510 self._orig_sys_module_state['stdout'] = sys.stdout
510 511 self._orig_sys_module_state['stderr'] = sys.stderr
511 512 self._orig_sys_module_state['excepthook'] = sys.excepthook
512 513 try:
513 514 self._orig_sys_modules_main_name = self.user_ns['__name__']
514 515 except KeyError:
515 516 pass
516 517
517 518 def restore_sys_module_state(self):
518 519 """Restore the state of the sys module."""
519 520 try:
520 521 for k, v in self._orig_sys_module_state.items():
521 522 setattr(sys, k, v)
522 523 except AttributeError:
523 524 pass
524 525 # Reset what what done in self.init_sys_modules
525 526 try:
526 527 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
527 528 except (AttributeError, KeyError):
528 529 pass
529 530
530 531 #-------------------------------------------------------------------------
531 532 # Things related to hooks
532 533 #-------------------------------------------------------------------------
533 534
534 535 def init_hooks(self):
535 536 # hooks holds pointers used for user-side customizations
536 537 self.hooks = Struct()
537 538
538 539 self.strdispatchers = {}
539 540
540 541 # Set all default hooks, defined in the IPython.hooks module.
541 542 hooks = IPython.core.hooks
542 543 for hook_name in hooks.__all__:
543 544 # default hooks have priority 100, i.e. low; user hooks should have
544 545 # 0-100 priority
545 546 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
546 547
547 548 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
548 549 """set_hook(name,hook) -> sets an internal IPython hook.
549 550
550 551 IPython exposes some of its internal API as user-modifiable hooks. By
551 552 adding your function to one of these hooks, you can modify IPython's
552 553 behavior to call at runtime your own routines."""
553 554
554 555 # At some point in the future, this should validate the hook before it
555 556 # accepts it. Probably at least check that the hook takes the number
556 557 # of args it's supposed to.
557 558
558 559 f = new.instancemethod(hook,self,self.__class__)
559 560
560 561 # check if the hook is for strdispatcher first
561 562 if str_key is not None:
562 563 sdp = self.strdispatchers.get(name, StrDispatch())
563 564 sdp.add_s(str_key, f, priority )
564 565 self.strdispatchers[name] = sdp
565 566 return
566 567 if re_key is not None:
567 568 sdp = self.strdispatchers.get(name, StrDispatch())
568 569 sdp.add_re(re.compile(re_key), f, priority )
569 570 self.strdispatchers[name] = sdp
570 571 return
571 572
572 573 dp = getattr(self.hooks, name, None)
573 574 if name not in IPython.core.hooks.__all__:
574 575 print "Warning! Hook '%s' is not one of %s" % \
575 576 (name, IPython.core.hooks.__all__ )
576 577 if not dp:
577 578 dp = IPython.core.hooks.CommandChainDispatcher()
578 579
579 580 try:
580 581 dp.add(f,priority)
581 582 except AttributeError:
582 583 # it was not commandchain, plain old func - replace
583 584 dp = f
584 585
585 586 setattr(self.hooks,name, dp)
586 587
587 588 def register_post_execute(self, func):
588 589 """Register a function for calling after code execution.
589 590 """
590 591 if not callable(func):
591 592 raise ValueError('argument %s must be callable' % func)
592 593 self._post_execute.add(func)
593 594
594 595 #-------------------------------------------------------------------------
595 596 # Things related to the "main" module
596 597 #-------------------------------------------------------------------------
597 598
598 599 def new_main_mod(self,ns=None):
599 600 """Return a new 'main' module object for user code execution.
600 601 """
601 602 main_mod = self._user_main_module
602 603 init_fakemod_dict(main_mod,ns)
603 604 return main_mod
604 605
605 606 def cache_main_mod(self,ns,fname):
606 607 """Cache a main module's namespace.
607 608
608 609 When scripts are executed via %run, we must keep a reference to the
609 610 namespace of their __main__ module (a FakeModule instance) around so
610 611 that Python doesn't clear it, rendering objects defined therein
611 612 useless.
612 613
613 614 This method keeps said reference in a private dict, keyed by the
614 615 absolute path of the module object (which corresponds to the script
615 616 path). This way, for multiple executions of the same script we only
616 617 keep one copy of the namespace (the last one), thus preventing memory
617 618 leaks from old references while allowing the objects from the last
618 619 execution to be accessible.
619 620
620 621 Note: we can not allow the actual FakeModule instances to be deleted,
621 622 because of how Python tears down modules (it hard-sets all their
622 623 references to None without regard for reference counts). This method
623 624 must therefore make a *copy* of the given namespace, to allow the
624 625 original module's __dict__ to be cleared and reused.
625 626
626 627
627 628 Parameters
628 629 ----------
629 630 ns : a namespace (a dict, typically)
630 631
631 632 fname : str
632 633 Filename associated with the namespace.
633 634
634 635 Examples
635 636 --------
636 637
637 638 In [10]: import IPython
638 639
639 640 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
640 641
641 642 In [12]: IPython.__file__ in _ip._main_ns_cache
642 643 Out[12]: True
643 644 """
644 645 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
645 646
646 647 def clear_main_mod_cache(self):
647 648 """Clear the cache of main modules.
648 649
649 650 Mainly for use by utilities like %reset.
650 651
651 652 Examples
652 653 --------
653 654
654 655 In [15]: import IPython
655 656
656 657 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
657 658
658 659 In [17]: len(_ip._main_ns_cache) > 0
659 660 Out[17]: True
660 661
661 662 In [18]: _ip.clear_main_mod_cache()
662 663
663 664 In [19]: len(_ip._main_ns_cache) == 0
664 665 Out[19]: True
665 666 """
666 667 self._main_ns_cache.clear()
667 668
668 669 #-------------------------------------------------------------------------
669 670 # Things related to debugging
670 671 #-------------------------------------------------------------------------
671 672
672 673 def init_pdb(self):
673 674 # Set calling of pdb on exceptions
674 675 # self.call_pdb is a property
675 676 self.call_pdb = self.pdb
676 677
677 678 def _get_call_pdb(self):
678 679 return self._call_pdb
679 680
680 681 def _set_call_pdb(self,val):
681 682
682 683 if val not in (0,1,False,True):
683 684 raise ValueError,'new call_pdb value must be boolean'
684 685
685 686 # store value in instance
686 687 self._call_pdb = val
687 688
688 689 # notify the actual exception handlers
689 690 self.InteractiveTB.call_pdb = val
690 691
691 692 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
692 693 'Control auto-activation of pdb at exceptions')
693 694
694 695 def debugger(self,force=False):
695 696 """Call the pydb/pdb debugger.
696 697
697 698 Keywords:
698 699
699 700 - force(False): by default, this routine checks the instance call_pdb
700 701 flag and does not actually invoke the debugger if the flag is false.
701 702 The 'force' option forces the debugger to activate even if the flag
702 703 is false.
703 704 """
704 705
705 706 if not (force or self.call_pdb):
706 707 return
707 708
708 709 if not hasattr(sys,'last_traceback'):
709 710 error('No traceback has been produced, nothing to debug.')
710 711 return
711 712
712 713 # use pydb if available
713 714 if debugger.has_pydb:
714 715 from pydb import pm
715 716 else:
716 717 # fallback to our internal debugger
717 718 pm = lambda : self.InteractiveTB.debugger(force=True)
718 719 self.history_saving_wrapper(pm)()
719 720
720 721 #-------------------------------------------------------------------------
721 722 # Things related to IPython's various namespaces
722 723 #-------------------------------------------------------------------------
723 724
724 725 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
725 726 # Create the namespace where the user will operate. user_ns is
726 727 # normally the only one used, and it is passed to the exec calls as
727 728 # the locals argument. But we do carry a user_global_ns namespace
728 729 # given as the exec 'globals' argument, This is useful in embedding
729 730 # situations where the ipython shell opens in a context where the
730 731 # distinction between locals and globals is meaningful. For
731 732 # non-embedded contexts, it is just the same object as the user_ns dict.
732 733
733 734 # FIXME. For some strange reason, __builtins__ is showing up at user
734 735 # level as a dict instead of a module. This is a manual fix, but I
735 736 # should really track down where the problem is coming from. Alex
736 737 # Schmolck reported this problem first.
737 738
738 739 # A useful post by Alex Martelli on this topic:
739 740 # Re: inconsistent value from __builtins__
740 741 # Von: Alex Martelli <aleaxit@yahoo.com>
741 742 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
742 743 # Gruppen: comp.lang.python
743 744
744 745 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
745 746 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
746 747 # > <type 'dict'>
747 748 # > >>> print type(__builtins__)
748 749 # > <type 'module'>
749 750 # > Is this difference in return value intentional?
750 751
751 752 # Well, it's documented that '__builtins__' can be either a dictionary
752 753 # or a module, and it's been that way for a long time. Whether it's
753 754 # intentional (or sensible), I don't know. In any case, the idea is
754 755 # that if you need to access the built-in namespace directly, you
755 756 # should start with "import __builtin__" (note, no 's') which will
756 757 # definitely give you a module. Yeah, it's somewhat confusing:-(.
757 758
758 759 # These routines return properly built dicts as needed by the rest of
759 760 # the code, and can also be used by extension writers to generate
760 761 # properly initialized namespaces.
761 762 user_ns, user_global_ns = self.make_user_namespaces(user_ns,
762 763 user_global_ns)
763 764
764 765 # Assign namespaces
765 766 # This is the namespace where all normal user variables live
766 767 self.user_ns = user_ns
767 768 self.user_global_ns = user_global_ns
768 769
769 770 # An auxiliary namespace that checks what parts of the user_ns were
770 771 # loaded at startup, so we can list later only variables defined in
771 772 # actual interactive use. Since it is always a subset of user_ns, it
772 773 # doesn't need to be separately tracked in the ns_table.
773 774 self.user_ns_hidden = {}
774 775
775 776 # A namespace to keep track of internal data structures to prevent
776 777 # them from cluttering user-visible stuff. Will be updated later
777 778 self.internal_ns = {}
778 779
779 780 # Now that FakeModule produces a real module, we've run into a nasty
780 781 # problem: after script execution (via %run), the module where the user
781 782 # code ran is deleted. Now that this object is a true module (needed
782 783 # so docetst and other tools work correctly), the Python module
783 784 # teardown mechanism runs over it, and sets to None every variable
784 785 # present in that module. Top-level references to objects from the
785 786 # script survive, because the user_ns is updated with them. However,
786 787 # calling functions defined in the script that use other things from
787 788 # the script will fail, because the function's closure had references
788 789 # to the original objects, which are now all None. So we must protect
789 790 # these modules from deletion by keeping a cache.
790 791 #
791 792 # To avoid keeping stale modules around (we only need the one from the
792 793 # last run), we use a dict keyed with the full path to the script, so
793 794 # only the last version of the module is held in the cache. Note,
794 795 # however, that we must cache the module *namespace contents* (their
795 796 # __dict__). Because if we try to cache the actual modules, old ones
796 797 # (uncached) could be destroyed while still holding references (such as
797 798 # those held by GUI objects that tend to be long-lived)>
798 799 #
799 800 # The %reset command will flush this cache. See the cache_main_mod()
800 801 # and clear_main_mod_cache() methods for details on use.
801 802
802 803 # This is the cache used for 'main' namespaces
803 804 self._main_ns_cache = {}
804 805 # And this is the single instance of FakeModule whose __dict__ we keep
805 806 # copying and clearing for reuse on each %run
806 807 self._user_main_module = FakeModule()
807 808
808 809 # A table holding all the namespaces IPython deals with, so that
809 810 # introspection facilities can search easily.
810 811 self.ns_table = {'user':user_ns,
811 812 'user_global':user_global_ns,
812 813 'internal':self.internal_ns,
813 814 'builtin':__builtin__.__dict__
814 815 }
815 816
816 817 # Similarly, track all namespaces where references can be held and that
817 818 # we can safely clear (so it can NOT include builtin). This one can be
818 819 # a simple list.
819 820 self.ns_refs_table = [ user_ns, user_global_ns, self.user_ns_hidden,
820 821 self.internal_ns, self._main_ns_cache ]
821 822
822 823 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
823 824 """Return a valid local and global user interactive namespaces.
824 825
825 826 This builds a dict with the minimal information needed to operate as a
826 827 valid IPython user namespace, which you can pass to the various
827 828 embedding classes in ipython. The default implementation returns the
828 829 same dict for both the locals and the globals to allow functions to
829 830 refer to variables in the namespace. Customized implementations can
830 831 return different dicts. The locals dictionary can actually be anything
831 832 following the basic mapping protocol of a dict, but the globals dict
832 833 must be a true dict, not even a subclass. It is recommended that any
833 834 custom object for the locals namespace synchronize with the globals
834 835 dict somehow.
835 836
836 837 Raises TypeError if the provided globals namespace is not a true dict.
837 838
838 839 Parameters
839 840 ----------
840 841 user_ns : dict-like, optional
841 842 The current user namespace. The items in this namespace should
842 843 be included in the output. If None, an appropriate blank
843 844 namespace should be created.
844 845 user_global_ns : dict, optional
845 846 The current user global namespace. The items in this namespace
846 847 should be included in the output. If None, an appropriate
847 848 blank namespace should be created.
848 849
849 850 Returns
850 851 -------
851 852 A pair of dictionary-like object to be used as the local namespace
852 853 of the interpreter and a dict to be used as the global namespace.
853 854 """
854 855
855 856
856 857 # We must ensure that __builtin__ (without the final 's') is always
857 858 # available and pointing to the __builtin__ *module*. For more details:
858 859 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
859 860
860 861 if user_ns is None:
861 862 # Set __name__ to __main__ to better match the behavior of the
862 863 # normal interpreter.
863 864 user_ns = {'__name__' :'__main__',
864 865 '__builtin__' : __builtin__,
865 866 '__builtins__' : __builtin__,
866 867 }
867 868 else:
868 869 user_ns.setdefault('__name__','__main__')
869 870 user_ns.setdefault('__builtin__',__builtin__)
870 871 user_ns.setdefault('__builtins__',__builtin__)
871 872
872 873 if user_global_ns is None:
873 874 user_global_ns = user_ns
874 875 if type(user_global_ns) is not dict:
875 876 raise TypeError("user_global_ns must be a true dict; got %r"
876 877 % type(user_global_ns))
877 878
878 879 return user_ns, user_global_ns
879 880
880 881 def init_sys_modules(self):
881 882 # We need to insert into sys.modules something that looks like a
882 883 # module but which accesses the IPython namespace, for shelve and
883 884 # pickle to work interactively. Normally they rely on getting
884 885 # everything out of __main__, but for embedding purposes each IPython
885 886 # instance has its own private namespace, so we can't go shoving
886 887 # everything into __main__.
887 888
888 889 # note, however, that we should only do this for non-embedded
889 890 # ipythons, which really mimic the __main__.__dict__ with their own
890 891 # namespace. Embedded instances, on the other hand, should not do
891 892 # this because they need to manage the user local/global namespaces
892 893 # only, but they live within a 'normal' __main__ (meaning, they
893 894 # shouldn't overtake the execution environment of the script they're
894 895 # embedded in).
895 896
896 897 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
897 898
898 899 try:
899 900 main_name = self.user_ns['__name__']
900 901 except KeyError:
901 902 raise KeyError('user_ns dictionary MUST have a "__name__" key')
902 903 else:
903 904 sys.modules[main_name] = FakeModule(self.user_ns)
904 905
905 906 def init_user_ns(self):
906 907 """Initialize all user-visible namespaces to their minimum defaults.
907 908
908 909 Certain history lists are also initialized here, as they effectively
909 910 act as user namespaces.
910 911
911 912 Notes
912 913 -----
913 914 All data structures here are only filled in, they are NOT reset by this
914 915 method. If they were not empty before, data will simply be added to
915 916 therm.
916 917 """
917 918 # This function works in two parts: first we put a few things in
918 919 # user_ns, and we sync that contents into user_ns_hidden so that these
919 920 # initial variables aren't shown by %who. After the sync, we add the
920 921 # rest of what we *do* want the user to see with %who even on a new
921 922 # session (probably nothing, so theye really only see their own stuff)
922 923
923 924 # The user dict must *always* have a __builtin__ reference to the
924 925 # Python standard __builtin__ namespace, which must be imported.
925 926 # This is so that certain operations in prompt evaluation can be
926 927 # reliably executed with builtins. Note that we can NOT use
927 928 # __builtins__ (note the 's'), because that can either be a dict or a
928 929 # module, and can even mutate at runtime, depending on the context
929 930 # (Python makes no guarantees on it). In contrast, __builtin__ is
930 931 # always a module object, though it must be explicitly imported.
931 932
932 933 # For more details:
933 934 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
934 935 ns = dict(__builtin__ = __builtin__)
935 936
936 937 # Put 'help' in the user namespace
937 938 try:
938 939 from site import _Helper
939 940 ns['help'] = _Helper()
940 941 except ImportError:
941 942 warn('help() not available - check site.py')
942 943
943 944 # make global variables for user access to the histories
944 945 ns['_ih'] = self.input_hist
945 946 ns['_oh'] = self.output_hist
946 947 ns['_dh'] = self.dir_hist
947 948
948 949 ns['_sh'] = shadowns
949 950
950 951 # user aliases to input and output histories. These shouldn't show up
951 952 # in %who, as they can have very large reprs.
952 953 ns['In'] = self.input_hist
953 954 ns['Out'] = self.output_hist
954 955
955 956 # Store myself as the public api!!!
956 957 ns['get_ipython'] = self.get_ipython
957 958
958 959 # Sync what we've added so far to user_ns_hidden so these aren't seen
959 960 # by %who
960 961 self.user_ns_hidden.update(ns)
961 962
962 963 # Anything put into ns now would show up in %who. Think twice before
963 964 # putting anything here, as we really want %who to show the user their
964 965 # stuff, not our variables.
965 966
966 967 # Finally, update the real user's namespace
967 968 self.user_ns.update(ns)
968 969
969 970 def reset(self):
970 971 """Clear all internal namespaces.
971 972
972 973 Note that this is much more aggressive than %reset, since it clears
973 974 fully all namespaces, as well as all input/output lists.
974 975 """
975 976 # Clear histories
976 977 self.history_manager.reset()
977 978
978 979 # Reset counter used to index all histories
979 980 self.execution_count = 0
980 981
981 982 # Restore the user namespaces to minimal usability
982 983 for ns in self.ns_refs_table:
983 984 ns.clear()
984 985 self.init_user_ns()
985 986
986 987 # Restore the default and user aliases
987 988 self.alias_manager.clear_aliases()
988 989 self.alias_manager.init_aliases()
989 990
990 991 def reset_selective(self, regex=None):
991 992 """Clear selective variables from internal namespaces based on a
992 993 specified regular expression.
993 994
994 995 Parameters
995 996 ----------
996 997 regex : string or compiled pattern, optional
997 998 A regular expression pattern that will be used in searching
998 999 variable names in the users namespaces.
999 1000 """
1000 1001 if regex is not None:
1001 1002 try:
1002 1003 m = re.compile(regex)
1003 1004 except TypeError:
1004 1005 raise TypeError('regex must be a string or compiled pattern')
1005 1006 # Search for keys in each namespace that match the given regex
1006 1007 # If a match is found, delete the key/value pair.
1007 1008 for ns in self.ns_refs_table:
1008 1009 for var in ns:
1009 1010 if m.search(var):
1010 1011 del ns[var]
1011 1012
1012 1013 def push(self, variables, interactive=True):
1013 1014 """Inject a group of variables into the IPython user namespace.
1014 1015
1015 1016 Parameters
1016 1017 ----------
1017 1018 variables : dict, str or list/tuple of str
1018 1019 The variables to inject into the user's namespace. If a dict, a
1019 1020 simple update is done. If a str, the string is assumed to have
1020 1021 variable names separated by spaces. A list/tuple of str can also
1021 1022 be used to give the variable names. If just the variable names are
1022 1023 give (list/tuple/str) then the variable values looked up in the
1023 1024 callers frame.
1024 1025 interactive : bool
1025 1026 If True (default), the variables will be listed with the ``who``
1026 1027 magic.
1027 1028 """
1028 1029 vdict = None
1029 1030
1030 1031 # We need a dict of name/value pairs to do namespace updates.
1031 1032 if isinstance(variables, dict):
1032 1033 vdict = variables
1033 1034 elif isinstance(variables, (basestring, list, tuple)):
1034 1035 if isinstance(variables, basestring):
1035 1036 vlist = variables.split()
1036 1037 else:
1037 1038 vlist = variables
1038 1039 vdict = {}
1039 1040 cf = sys._getframe(1)
1040 1041 for name in vlist:
1041 1042 try:
1042 1043 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1043 1044 except:
1044 1045 print ('Could not get variable %s from %s' %
1045 1046 (name,cf.f_code.co_name))
1046 1047 else:
1047 1048 raise ValueError('variables must be a dict/str/list/tuple')
1048 1049
1049 1050 # Propagate variables to user namespace
1050 1051 self.user_ns.update(vdict)
1051 1052
1052 1053 # And configure interactive visibility
1053 1054 config_ns = self.user_ns_hidden
1054 1055 if interactive:
1055 1056 for name, val in vdict.iteritems():
1056 1057 config_ns.pop(name, None)
1057 1058 else:
1058 1059 for name,val in vdict.iteritems():
1059 1060 config_ns[name] = val
1060 1061
1061 1062 #-------------------------------------------------------------------------
1062 1063 # Things related to object introspection
1063 1064 #-------------------------------------------------------------------------
1064 1065
1065 1066 def _ofind(self, oname, namespaces=None):
1066 1067 """Find an object in the available namespaces.
1067 1068
1068 1069 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1069 1070
1070 1071 Has special code to detect magic functions.
1071 1072 """
1072 1073 #oname = oname.strip()
1073 1074 #print '1- oname: <%r>' % oname # dbg
1074 1075 try:
1075 1076 oname = oname.strip().encode('ascii')
1076 1077 #print '2- oname: <%r>' % oname # dbg
1077 1078 except UnicodeEncodeError:
1078 1079 print 'Python identifiers can only contain ascii characters.'
1079 1080 return dict(found=False)
1080 1081
1081 1082 alias_ns = None
1082 1083 if namespaces is None:
1083 1084 # Namespaces to search in:
1084 1085 # Put them in a list. The order is important so that we
1085 1086 # find things in the same order that Python finds them.
1086 1087 namespaces = [ ('Interactive', self.user_ns),
1087 1088 ('IPython internal', self.internal_ns),
1088 1089 ('Python builtin', __builtin__.__dict__),
1089 1090 ('Alias', self.alias_manager.alias_table),
1090 1091 ]
1091 1092 alias_ns = self.alias_manager.alias_table
1092 1093
1093 1094 # initialize results to 'null'
1094 1095 found = False; obj = None; ospace = None; ds = None;
1095 1096 ismagic = False; isalias = False; parent = None
1096 1097
1097 1098 # We need to special-case 'print', which as of python2.6 registers as a
1098 1099 # function but should only be treated as one if print_function was
1099 1100 # loaded with a future import. In this case, just bail.
1100 1101 if (oname == 'print' and not (self.compile.compiler.flags &
1101 1102 __future__.CO_FUTURE_PRINT_FUNCTION)):
1102 1103 return {'found':found, 'obj':obj, 'namespace':ospace,
1103 1104 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1104 1105
1105 1106 # Look for the given name by splitting it in parts. If the head is
1106 1107 # found, then we look for all the remaining parts as members, and only
1107 1108 # declare success if we can find them all.
1108 1109 oname_parts = oname.split('.')
1109 1110 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1110 1111 for nsname,ns in namespaces:
1111 1112 try:
1112 1113 obj = ns[oname_head]
1113 1114 except KeyError:
1114 1115 continue
1115 1116 else:
1116 1117 #print 'oname_rest:', oname_rest # dbg
1117 1118 for part in oname_rest:
1118 1119 try:
1119 1120 parent = obj
1120 1121 obj = getattr(obj,part)
1121 1122 except:
1122 1123 # Blanket except b/c some badly implemented objects
1123 1124 # allow __getattr__ to raise exceptions other than
1124 1125 # AttributeError, which then crashes IPython.
1125 1126 break
1126 1127 else:
1127 1128 # If we finish the for loop (no break), we got all members
1128 1129 found = True
1129 1130 ospace = nsname
1130 1131 if ns == alias_ns:
1131 1132 isalias = True
1132 1133 break # namespace loop
1133 1134
1134 1135 # Try to see if it's magic
1135 1136 if not found:
1136 1137 if oname.startswith(ESC_MAGIC):
1137 1138 oname = oname[1:]
1138 1139 obj = getattr(self,'magic_'+oname,None)
1139 1140 if obj is not None:
1140 1141 found = True
1141 1142 ospace = 'IPython internal'
1142 1143 ismagic = True
1143 1144
1144 1145 # Last try: special-case some literals like '', [], {}, etc:
1145 1146 if not found and oname_head in ["''",'""','[]','{}','()']:
1146 1147 obj = eval(oname_head)
1147 1148 found = True
1148 1149 ospace = 'Interactive'
1149 1150
1150 1151 return {'found':found, 'obj':obj, 'namespace':ospace,
1151 1152 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1152 1153
1153 1154 def _ofind_property(self, oname, info):
1154 1155 """Second part of object finding, to look for property details."""
1155 1156 if info.found:
1156 1157 # Get the docstring of the class property if it exists.
1157 1158 path = oname.split('.')
1158 1159 root = '.'.join(path[:-1])
1159 1160 if info.parent is not None:
1160 1161 try:
1161 1162 target = getattr(info.parent, '__class__')
1162 1163 # The object belongs to a class instance.
1163 1164 try:
1164 1165 target = getattr(target, path[-1])
1165 1166 # The class defines the object.
1166 1167 if isinstance(target, property):
1167 1168 oname = root + '.__class__.' + path[-1]
1168 1169 info = Struct(self._ofind(oname))
1169 1170 except AttributeError: pass
1170 1171 except AttributeError: pass
1171 1172
1172 1173 # We return either the new info or the unmodified input if the object
1173 1174 # hadn't been found
1174 1175 return info
1175 1176
1176 1177 def _object_find(self, oname, namespaces=None):
1177 1178 """Find an object and return a struct with info about it."""
1178 1179 inf = Struct(self._ofind(oname, namespaces))
1179 1180 return Struct(self._ofind_property(oname, inf))
1180 1181
1181 1182 def _inspect(self, meth, oname, namespaces=None, **kw):
1182 1183 """Generic interface to the inspector system.
1183 1184
1184 1185 This function is meant to be called by pdef, pdoc & friends."""
1185 1186 info = self._object_find(oname)
1186 1187 if info.found:
1187 1188 pmethod = getattr(self.inspector, meth)
1188 1189 formatter = format_screen if info.ismagic else None
1189 1190 if meth == 'pdoc':
1190 1191 pmethod(info.obj, oname, formatter)
1191 1192 elif meth == 'pinfo':
1192 1193 pmethod(info.obj, oname, formatter, info, **kw)
1193 1194 else:
1194 1195 pmethod(info.obj, oname)
1195 1196 else:
1196 1197 print 'Object `%s` not found.' % oname
1197 1198 return 'not found' # so callers can take other action
1198 1199
1199 1200 def object_inspect(self, oname):
1200 1201 info = self._object_find(oname)
1201 1202 if info.found:
1202 1203 return self.inspector.info(info.obj, oname, info=info)
1203 1204 else:
1204 1205 return oinspect.object_info(name=oname, found=False)
1205 1206
1206 1207 #-------------------------------------------------------------------------
1207 1208 # Things related to history management
1208 1209 #-------------------------------------------------------------------------
1209 1210
1210 1211 def init_history(self):
1211 1212 self.history_manager = HistoryManager(shell=self)
1212 1213
1213 1214 def savehist(self):
1214 1215 """Save input history to a file (via readline library)."""
1215 1216 self.history_manager.save_hist()
1216 1217
1217 1218 def reloadhist(self):
1218 1219 """Reload the input history from disk file."""
1219 1220 self.history_manager.reload_hist()
1220 1221
1221 1222 def history_saving_wrapper(self, func):
1222 1223 """ Wrap func for readline history saving
1223 1224
1224 1225 Convert func into callable that saves & restores
1225 1226 history around the call """
1226 1227
1227 1228 if self.has_readline:
1228 1229 from IPython.utils import rlineimpl as readline
1229 1230 else:
1230 1231 return func
1231 1232
1232 1233 def wrapper():
1233 1234 self.savehist()
1234 1235 try:
1235 1236 func()
1236 1237 finally:
1237 1238 readline.read_history_file(self.histfile)
1238 1239 return wrapper
1239 1240
1240 1241 #-------------------------------------------------------------------------
1241 1242 # Things related to exception handling and tracebacks (not debugging)
1242 1243 #-------------------------------------------------------------------------
1243 1244
1244 1245 def init_traceback_handlers(self, custom_exceptions):
1245 1246 # Syntax error handler.
1246 1247 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1247 1248
1248 1249 # The interactive one is initialized with an offset, meaning we always
1249 1250 # want to remove the topmost item in the traceback, which is our own
1250 1251 # internal code. Valid modes: ['Plain','Context','Verbose']
1251 1252 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1252 1253 color_scheme='NoColor',
1253 1254 tb_offset = 1)
1254 1255
1255 1256 # The instance will store a pointer to the system-wide exception hook,
1256 1257 # so that runtime code (such as magics) can access it. This is because
1257 1258 # during the read-eval loop, it may get temporarily overwritten.
1258 1259 self.sys_excepthook = sys.excepthook
1259 1260
1260 1261 # and add any custom exception handlers the user may have specified
1261 1262 self.set_custom_exc(*custom_exceptions)
1262 1263
1263 1264 # Set the exception mode
1264 1265 self.InteractiveTB.set_mode(mode=self.xmode)
1265 1266
1266 1267 def set_custom_exc(self, exc_tuple, handler):
1267 1268 """set_custom_exc(exc_tuple,handler)
1268 1269
1269 1270 Set a custom exception handler, which will be called if any of the
1270 1271 exceptions in exc_tuple occur in the mainloop (specifically, in the
1271 1272 runcode() method.
1272 1273
1273 1274 Inputs:
1274 1275
1275 1276 - exc_tuple: a *tuple* of valid exceptions to call the defined
1276 1277 handler for. It is very important that you use a tuple, and NOT A
1277 1278 LIST here, because of the way Python's except statement works. If
1278 1279 you only want to trap a single exception, use a singleton tuple:
1279 1280
1280 1281 exc_tuple == (MyCustomException,)
1281 1282
1282 1283 - handler: this must be defined as a function with the following
1283 1284 basic interface::
1284 1285
1285 1286 def my_handler(self, etype, value, tb, tb_offset=None)
1286 1287 ...
1287 1288 # The return value must be
1288 1289 return structured_traceback
1289 1290
1290 1291 This will be made into an instance method (via new.instancemethod)
1291 1292 of IPython itself, and it will be called if any of the exceptions
1292 1293 listed in the exc_tuple are caught. If the handler is None, an
1293 1294 internal basic one is used, which just prints basic info.
1294 1295
1295 1296 WARNING: by putting in your own exception handler into IPython's main
1296 1297 execution loop, you run a very good chance of nasty crashes. This
1297 1298 facility should only be used if you really know what you are doing."""
1298 1299
1299 1300 assert type(exc_tuple)==type(()) , \
1300 1301 "The custom exceptions must be given AS A TUPLE."
1301 1302
1302 1303 def dummy_handler(self,etype,value,tb):
1303 1304 print '*** Simple custom exception handler ***'
1304 1305 print 'Exception type :',etype
1305 1306 print 'Exception value:',value
1306 1307 print 'Traceback :',tb
1307 1308 print 'Source code :','\n'.join(self.buffer)
1308 1309
1309 1310 if handler is None: handler = dummy_handler
1310 1311
1311 1312 self.CustomTB = new.instancemethod(handler,self,self.__class__)
1312 1313 self.custom_exceptions = exc_tuple
1313 1314
1314 1315 def excepthook(self, etype, value, tb):
1315 1316 """One more defense for GUI apps that call sys.excepthook.
1316 1317
1317 1318 GUI frameworks like wxPython trap exceptions and call
1318 1319 sys.excepthook themselves. I guess this is a feature that
1319 1320 enables them to keep running after exceptions that would
1320 1321 otherwise kill their mainloop. This is a bother for IPython
1321 1322 which excepts to catch all of the program exceptions with a try:
1322 1323 except: statement.
1323 1324
1324 1325 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1325 1326 any app directly invokes sys.excepthook, it will look to the user like
1326 1327 IPython crashed. In order to work around this, we can disable the
1327 1328 CrashHandler and replace it with this excepthook instead, which prints a
1328 1329 regular traceback using our InteractiveTB. In this fashion, apps which
1329 1330 call sys.excepthook will generate a regular-looking exception from
1330 1331 IPython, and the CrashHandler will only be triggered by real IPython
1331 1332 crashes.
1332 1333
1333 1334 This hook should be used sparingly, only in places which are not likely
1334 1335 to be true IPython errors.
1335 1336 """
1336 1337 self.showtraceback((etype,value,tb),tb_offset=0)
1337 1338
1338 1339 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1339 1340 exception_only=False):
1340 1341 """Display the exception that just occurred.
1341 1342
1342 1343 If nothing is known about the exception, this is the method which
1343 1344 should be used throughout the code for presenting user tracebacks,
1344 1345 rather than directly invoking the InteractiveTB object.
1345 1346
1346 1347 A specific showsyntaxerror() also exists, but this method can take
1347 1348 care of calling it if needed, so unless you are explicitly catching a
1348 1349 SyntaxError exception, don't try to analyze the stack manually and
1349 1350 simply call this method."""
1350 1351
1351 1352 try:
1352 1353 if exc_tuple is None:
1353 1354 etype, value, tb = sys.exc_info()
1354 1355 else:
1355 1356 etype, value, tb = exc_tuple
1356 1357
1357 1358 if etype is None:
1358 1359 if hasattr(sys, 'last_type'):
1359 1360 etype, value, tb = sys.last_type, sys.last_value, \
1360 1361 sys.last_traceback
1361 1362 else:
1362 1363 self.write_err('No traceback available to show.\n')
1363 1364 return
1364 1365
1365 1366 if etype is SyntaxError:
1366 1367 # Though this won't be called by syntax errors in the input
1367 1368 # line, there may be SyntaxError cases whith imported code.
1368 1369 self.showsyntaxerror(filename)
1369 1370 elif etype is UsageError:
1370 1371 print "UsageError:", value
1371 1372 else:
1372 1373 # WARNING: these variables are somewhat deprecated and not
1373 1374 # necessarily safe to use in a threaded environment, but tools
1374 1375 # like pdb depend on their existence, so let's set them. If we
1375 1376 # find problems in the field, we'll need to revisit their use.
1376 1377 sys.last_type = etype
1377 1378 sys.last_value = value
1378 1379 sys.last_traceback = tb
1379 1380
1380 1381 if etype in self.custom_exceptions:
1381 1382 # FIXME: Old custom traceback objects may just return a
1382 1383 # string, in that case we just put it into a list
1383 1384 stb = self.CustomTB(etype, value, tb, tb_offset)
1384 1385 if isinstance(ctb, basestring):
1385 1386 stb = [stb]
1386 1387 else:
1387 1388 if exception_only:
1388 1389 stb = ['An exception has occurred, use %tb to see '
1389 1390 'the full traceback.\n']
1390 1391 stb.extend(self.InteractiveTB.get_exception_only(etype,
1391 1392 value))
1392 1393 else:
1393 1394 stb = self.InteractiveTB.structured_traceback(etype,
1394 1395 value, tb, tb_offset=tb_offset)
1395 1396 # FIXME: the pdb calling should be done by us, not by
1396 1397 # the code computing the traceback.
1397 1398 if self.InteractiveTB.call_pdb:
1398 1399 # pdb mucks up readline, fix it back
1399 1400 self.set_readline_completer()
1400 1401
1401 1402 # Actually show the traceback
1402 1403 self._showtraceback(etype, value, stb)
1403 1404
1404 1405 except KeyboardInterrupt:
1405 1406 self.write_err("\nKeyboardInterrupt\n")
1406 1407
1407 1408 def _showtraceback(self, etype, evalue, stb):
1408 1409 """Actually show a traceback.
1409 1410
1410 1411 Subclasses may override this method to put the traceback on a different
1411 1412 place, like a side channel.
1412 1413 """
1413 1414 print >> io.Term.cout, self.InteractiveTB.stb2text(stb)
1414 1415
1415 1416 def showsyntaxerror(self, filename=None):
1416 1417 """Display the syntax error that just occurred.
1417 1418
1418 1419 This doesn't display a stack trace because there isn't one.
1419 1420
1420 1421 If a filename is given, it is stuffed in the exception instead
1421 1422 of what was there before (because Python's parser always uses
1422 1423 "<string>" when reading from a string).
1423 1424 """
1424 1425 etype, value, last_traceback = sys.exc_info()
1425 1426
1426 1427 # See note about these variables in showtraceback() above
1427 1428 sys.last_type = etype
1428 1429 sys.last_value = value
1429 1430 sys.last_traceback = last_traceback
1430 1431
1431 1432 if filename and etype is SyntaxError:
1432 1433 # Work hard to stuff the correct filename in the exception
1433 1434 try:
1434 1435 msg, (dummy_filename, lineno, offset, line) = value
1435 1436 except:
1436 1437 # Not the format we expect; leave it alone
1437 1438 pass
1438 1439 else:
1439 1440 # Stuff in the right filename
1440 1441 try:
1441 1442 # Assume SyntaxError is a class exception
1442 1443 value = SyntaxError(msg, (filename, lineno, offset, line))
1443 1444 except:
1444 1445 # If that failed, assume SyntaxError is a string
1445 1446 value = msg, (filename, lineno, offset, line)
1446 1447 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1447 1448 self._showtraceback(etype, value, stb)
1448 1449
1449 1450 #-------------------------------------------------------------------------
1450 1451 # Things related to readline
1451 1452 #-------------------------------------------------------------------------
1452 1453
1453 1454 def init_readline(self):
1454 1455 """Command history completion/saving/reloading."""
1455 1456
1456 1457 if self.readline_use:
1457 1458 import IPython.utils.rlineimpl as readline
1458 1459
1459 1460 self.rl_next_input = None
1460 1461 self.rl_do_indent = False
1461 1462
1462 1463 if not self.readline_use or not readline.have_readline:
1463 1464 self.has_readline = False
1464 1465 self.readline = None
1465 1466 # Set a number of methods that depend on readline to be no-op
1466 1467 self.savehist = no_op
1467 1468 self.reloadhist = no_op
1468 1469 self.set_readline_completer = no_op
1469 1470 self.set_custom_completer = no_op
1470 1471 self.set_completer_frame = no_op
1471 1472 warn('Readline services not available or not loaded.')
1472 1473 else:
1473 1474 self.has_readline = True
1474 1475 self.readline = readline
1475 1476 sys.modules['readline'] = readline
1476 1477
1477 1478 # Platform-specific configuration
1478 1479 if os.name == 'nt':
1479 1480 # FIXME - check with Frederick to see if we can harmonize
1480 1481 # naming conventions with pyreadline to avoid this
1481 1482 # platform-dependent check
1482 1483 self.readline_startup_hook = readline.set_pre_input_hook
1483 1484 else:
1484 1485 self.readline_startup_hook = readline.set_startup_hook
1485 1486
1486 1487 # Load user's initrc file (readline config)
1487 1488 # Or if libedit is used, load editrc.
1488 1489 inputrc_name = os.environ.get('INPUTRC')
1489 1490 if inputrc_name is None:
1490 1491 home_dir = get_home_dir()
1491 1492 if home_dir is not None:
1492 1493 inputrc_name = '.inputrc'
1493 1494 if readline.uses_libedit:
1494 1495 inputrc_name = '.editrc'
1495 1496 inputrc_name = os.path.join(home_dir, inputrc_name)
1496 1497 if os.path.isfile(inputrc_name):
1497 1498 try:
1498 1499 readline.read_init_file(inputrc_name)
1499 1500 except:
1500 1501 warn('Problems reading readline initialization file <%s>'
1501 1502 % inputrc_name)
1502 1503
1503 1504 # Configure readline according to user's prefs
1504 1505 # This is only done if GNU readline is being used. If libedit
1505 1506 # is being used (as on Leopard) the readline config is
1506 1507 # not run as the syntax for libedit is different.
1507 1508 if not readline.uses_libedit:
1508 1509 for rlcommand in self.readline_parse_and_bind:
1509 1510 #print "loading rl:",rlcommand # dbg
1510 1511 readline.parse_and_bind(rlcommand)
1511 1512
1512 1513 # Remove some chars from the delimiters list. If we encounter
1513 1514 # unicode chars, discard them.
1514 1515 delims = readline.get_completer_delims().encode("ascii", "ignore")
1515 1516 delims = delims.translate(string._idmap,
1516 1517 self.readline_remove_delims)
1517 1518 delims = delims.replace(ESC_MAGIC, '')
1518 1519 readline.set_completer_delims(delims)
1519 1520 # otherwise we end up with a monster history after a while:
1520 1521 readline.set_history_length(1000)
1521 1522 try:
1522 1523 #print '*** Reading readline history' # dbg
1523 1524 readline.read_history_file(self.histfile)
1524 1525 except IOError:
1525 1526 pass # It doesn't exist yet.
1526 1527
1527 1528 # If we have readline, we want our history saved upon ipython
1528 1529 # exiting.
1529 1530 atexit.register(self.savehist)
1530 1531
1531 1532 # Configure auto-indent for all platforms
1532 1533 self.set_autoindent(self.autoindent)
1533 1534
1534 1535 def set_next_input(self, s):
1535 1536 """ Sets the 'default' input string for the next command line.
1536 1537
1537 1538 Requires readline.
1538 1539
1539 1540 Example:
1540 1541
1541 1542 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1542 1543 [D:\ipython]|2> Hello Word_ # cursor is here
1543 1544 """
1544 1545
1545 1546 self.rl_next_input = s
1546 1547
1547 1548 # Maybe move this to the terminal subclass?
1548 1549 def pre_readline(self):
1549 1550 """readline hook to be used at the start of each line.
1550 1551
1551 1552 Currently it handles auto-indent only."""
1552 1553
1553 1554 if self.rl_do_indent:
1554 1555 self.readline.insert_text(self._indent_current_str())
1555 1556 if self.rl_next_input is not None:
1556 1557 self.readline.insert_text(self.rl_next_input)
1557 1558 self.rl_next_input = None
1558 1559
1559 1560 def _indent_current_str(self):
1560 1561 """return the current level of indentation as a string"""
1561 1562 #return self.indent_current_nsp * ' '
1562 1563 return self.input_splitter.indent_spaces * ' '
1563 1564
1564 1565 #-------------------------------------------------------------------------
1565 1566 # Things related to text completion
1566 1567 #-------------------------------------------------------------------------
1567 1568
1568 1569 def init_completer(self):
1569 1570 """Initialize the completion machinery.
1570 1571
1571 1572 This creates completion machinery that can be used by client code,
1572 1573 either interactively in-process (typically triggered by the readline
1573 1574 library), programatically (such as in test suites) or out-of-prcess
1574 1575 (typically over the network by remote frontends).
1575 1576 """
1576 1577 from IPython.core.completer import IPCompleter
1577 1578 from IPython.core.completerlib import (module_completer,
1578 1579 magic_run_completer, cd_completer)
1579 1580
1580 1581 self.Completer = IPCompleter(self,
1581 1582 self.user_ns,
1582 1583 self.user_global_ns,
1583 1584 self.readline_omit__names,
1584 1585 self.alias_manager.alias_table,
1585 1586 self.has_readline)
1586 1587
1587 1588 # Add custom completers to the basic ones built into IPCompleter
1588 1589 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1589 1590 self.strdispatchers['complete_command'] = sdisp
1590 1591 self.Completer.custom_completers = sdisp
1591 1592
1592 1593 self.set_hook('complete_command', module_completer, str_key = 'import')
1593 1594 self.set_hook('complete_command', module_completer, str_key = 'from')
1594 1595 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
1595 1596 self.set_hook('complete_command', cd_completer, str_key = '%cd')
1596 1597
1597 1598 # Only configure readline if we truly are using readline. IPython can
1598 1599 # do tab-completion over the network, in GUIs, etc, where readline
1599 1600 # itself may be absent
1600 1601 if self.has_readline:
1601 1602 self.set_readline_completer()
1602 1603
1603 1604 def complete(self, text, line=None, cursor_pos=None):
1604 1605 """Return the completed text and a list of completions.
1605 1606
1606 1607 Parameters
1607 1608 ----------
1608 1609
1609 1610 text : string
1610 1611 A string of text to be completed on. It can be given as empty and
1611 1612 instead a line/position pair are given. In this case, the
1612 1613 completer itself will split the line like readline does.
1613 1614
1614 1615 line : string, optional
1615 1616 The complete line that text is part of.
1616 1617
1617 1618 cursor_pos : int, optional
1618 1619 The position of the cursor on the input line.
1619 1620
1620 1621 Returns
1621 1622 -------
1622 1623 text : string
1623 1624 The actual text that was completed.
1624 1625
1625 1626 matches : list
1626 1627 A sorted list with all possible completions.
1627 1628
1628 1629 The optional arguments allow the completion to take more context into
1629 1630 account, and are part of the low-level completion API.
1630 1631
1631 1632 This is a wrapper around the completion mechanism, similar to what
1632 1633 readline does at the command line when the TAB key is hit. By
1633 1634 exposing it as a method, it can be used by other non-readline
1634 1635 environments (such as GUIs) for text completion.
1635 1636
1636 1637 Simple usage example:
1637 1638
1638 1639 In [1]: x = 'hello'
1639 1640
1640 1641 In [2]: _ip.complete('x.l')
1641 1642 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
1642 1643 """
1643 1644
1644 1645 # Inject names into __builtin__ so we can complete on the added names.
1645 1646 with self.builtin_trap:
1646 1647 return self.Completer.complete(text, line, cursor_pos)
1647 1648
1648 1649 def set_custom_completer(self, completer, pos=0):
1649 1650 """Adds a new custom completer function.
1650 1651
1651 1652 The position argument (defaults to 0) is the index in the completers
1652 1653 list where you want the completer to be inserted."""
1653 1654
1654 1655 newcomp = new.instancemethod(completer,self.Completer,
1655 1656 self.Completer.__class__)
1656 1657 self.Completer.matchers.insert(pos,newcomp)
1657 1658
1658 1659 def set_readline_completer(self):
1659 1660 """Reset readline's completer to be our own."""
1660 1661 self.readline.set_completer(self.Completer.rlcomplete)
1661 1662
1662 1663 def set_completer_frame(self, frame=None):
1663 1664 """Set the frame of the completer."""
1664 1665 if frame:
1665 1666 self.Completer.namespace = frame.f_locals
1666 1667 self.Completer.global_namespace = frame.f_globals
1667 1668 else:
1668 1669 self.Completer.namespace = self.user_ns
1669 1670 self.Completer.global_namespace = self.user_global_ns
1670 1671
1671 1672 #-------------------------------------------------------------------------
1672 1673 # Things related to magics
1673 1674 #-------------------------------------------------------------------------
1674 1675
1675 1676 def init_magics(self):
1676 1677 # FIXME: Move the color initialization to the DisplayHook, which
1677 1678 # should be split into a prompt manager and displayhook. We probably
1678 1679 # even need a centralize colors management object.
1679 1680 self.magic_colors(self.colors)
1680 1681 # History was moved to a separate module
1681 1682 from . import history
1682 1683 history.init_ipython(self)
1683 1684
1684 1685 def magic(self,arg_s):
1685 1686 """Call a magic function by name.
1686 1687
1687 1688 Input: a string containing the name of the magic function to call and
1688 1689 any additional arguments to be passed to the magic.
1689 1690
1690 1691 magic('name -opt foo bar') is equivalent to typing at the ipython
1691 1692 prompt:
1692 1693
1693 1694 In[1]: %name -opt foo bar
1694 1695
1695 1696 To call a magic without arguments, simply use magic('name').
1696 1697
1697 1698 This provides a proper Python function to call IPython's magics in any
1698 1699 valid Python code you can type at the interpreter, including loops and
1699 1700 compound statements.
1700 1701 """
1701 1702 args = arg_s.split(' ',1)
1702 1703 magic_name = args[0]
1703 1704 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1704 1705
1705 1706 try:
1706 1707 magic_args = args[1]
1707 1708 except IndexError:
1708 1709 magic_args = ''
1709 1710 fn = getattr(self,'magic_'+magic_name,None)
1710 1711 if fn is None:
1711 1712 error("Magic function `%s` not found." % magic_name)
1712 1713 else:
1713 1714 magic_args = self.var_expand(magic_args,1)
1714 1715 with nested(self.builtin_trap,):
1715 1716 result = fn(magic_args)
1716 1717 return result
1717 1718
1718 1719 def define_magic(self, magicname, func):
1719 1720 """Expose own function as magic function for ipython
1720 1721
1721 1722 def foo_impl(self,parameter_s=''):
1722 1723 'My very own magic!. (Use docstrings, IPython reads them).'
1723 1724 print 'Magic function. Passed parameter is between < >:'
1724 1725 print '<%s>' % parameter_s
1725 1726 print 'The self object is:',self
1726 1727
1727 1728 self.define_magic('foo',foo_impl)
1728 1729 """
1729 1730
1730 1731 import new
1731 1732 im = new.instancemethod(func,self, self.__class__)
1732 1733 old = getattr(self, "magic_" + magicname, None)
1733 1734 setattr(self, "magic_" + magicname, im)
1734 1735 return old
1735 1736
1736 1737 #-------------------------------------------------------------------------
1737 1738 # Things related to macros
1738 1739 #-------------------------------------------------------------------------
1739 1740
1740 1741 def define_macro(self, name, themacro):
1741 1742 """Define a new macro
1742 1743
1743 1744 Parameters
1744 1745 ----------
1745 1746 name : str
1746 1747 The name of the macro.
1747 1748 themacro : str or Macro
1748 1749 The action to do upon invoking the macro. If a string, a new
1749 1750 Macro object is created by passing the string to it.
1750 1751 """
1751 1752
1752 1753 from IPython.core import macro
1753 1754
1754 1755 if isinstance(themacro, basestring):
1755 1756 themacro = macro.Macro(themacro)
1756 1757 if not isinstance(themacro, macro.Macro):
1757 1758 raise ValueError('A macro must be a string or a Macro instance.')
1758 1759 self.user_ns[name] = themacro
1759 1760
1760 1761 #-------------------------------------------------------------------------
1761 1762 # Things related to the running of system commands
1762 1763 #-------------------------------------------------------------------------
1763 1764
1764 1765 def system(self, cmd):
1765 1766 """Call the given cmd in a subprocess.
1766 1767
1767 1768 Parameters
1768 1769 ----------
1769 1770 cmd : str
1770 1771 Command to execute (can not end in '&', as bacground processes are
1771 1772 not supported.
1772 1773 """
1773 1774 # We do not support backgrounding processes because we either use
1774 1775 # pexpect or pipes to read from. Users can always just call
1775 1776 # os.system() if they really want a background process.
1776 1777 if cmd.endswith('&'):
1777 1778 raise OSError("Background processes not supported.")
1778 1779
1779 1780 return system(self.var_expand(cmd, depth=2))
1780 1781
1781 1782 def getoutput(self, cmd, split=True):
1782 1783 """Get output (possibly including stderr) from a subprocess.
1783 1784
1784 1785 Parameters
1785 1786 ----------
1786 1787 cmd : str
1787 1788 Command to execute (can not end in '&', as background processes are
1788 1789 not supported.
1789 1790 split : bool, optional
1790 1791
1791 1792 If True, split the output into an IPython SList. Otherwise, an
1792 1793 IPython LSString is returned. These are objects similar to normal
1793 1794 lists and strings, with a few convenience attributes for easier
1794 1795 manipulation of line-based output. You can use '?' on them for
1795 1796 details.
1796 1797 """
1797 1798 if cmd.endswith('&'):
1798 1799 raise OSError("Background processes not supported.")
1799 1800 out = getoutput(self.var_expand(cmd, depth=2))
1800 1801 if split:
1801 1802 out = SList(out.splitlines())
1802 1803 else:
1803 1804 out = LSString(out)
1804 1805 return out
1805 1806
1806 1807 #-------------------------------------------------------------------------
1807 1808 # Things related to aliases
1808 1809 #-------------------------------------------------------------------------
1809 1810
1810 1811 def init_alias(self):
1811 1812 self.alias_manager = AliasManager(shell=self, config=self.config)
1812 1813 self.ns_table['alias'] = self.alias_manager.alias_table,
1813 1814
1814 1815 #-------------------------------------------------------------------------
1815 1816 # Things related to extensions and plugins
1816 1817 #-------------------------------------------------------------------------
1817 1818
1818 1819 def init_extension_manager(self):
1819 1820 self.extension_manager = ExtensionManager(shell=self, config=self.config)
1820 1821
1821 1822 def init_plugin_manager(self):
1822 1823 self.plugin_manager = PluginManager(config=self.config)
1823 1824
1824 1825 #-------------------------------------------------------------------------
1825 1826 # Things related to payloads
1826 1827 #-------------------------------------------------------------------------
1827 1828
1828 1829 def init_payload(self):
1829 1830 self.payload_manager = PayloadManager(config=self.config)
1830 1831
1831 1832 #-------------------------------------------------------------------------
1832 1833 # Things related to the prefilter
1833 1834 #-------------------------------------------------------------------------
1834 1835
1835 1836 def init_prefilter(self):
1836 1837 self.prefilter_manager = PrefilterManager(shell=self, config=self.config)
1837 1838 # Ultimately this will be refactored in the new interpreter code, but
1838 1839 # for now, we should expose the main prefilter method (there's legacy
1839 1840 # code out there that may rely on this).
1840 1841 self.prefilter = self.prefilter_manager.prefilter_lines
1841 1842
1842
1843 1843 def auto_rewrite_input(self, cmd):
1844 1844 """Print to the screen the rewritten form of the user's command.
1845 1845
1846 1846 This shows visual feedback by rewriting input lines that cause
1847 1847 automatic calling to kick in, like::
1848 1848
1849 1849 /f x
1850 1850
1851 1851 into::
1852 1852
1853 1853 ------> f(x)
1854 1854
1855 1855 after the user's input prompt. This helps the user understand that the
1856 1856 input line was transformed automatically by IPython.
1857 1857 """
1858 1858 rw = self.displayhook.prompt1.auto_rewrite() + cmd
1859 1859
1860 1860 try:
1861 1861 # plain ascii works better w/ pyreadline, on some machines, so
1862 1862 # we use it and only print uncolored rewrite if we have unicode
1863 1863 rw = str(rw)
1864 1864 print >> IPython.utils.io.Term.cout, rw
1865 1865 except UnicodeEncodeError:
1866 1866 print "------> " + cmd
1867 1867
1868 1868 #-------------------------------------------------------------------------
1869 1869 # Things related to extracting values/expressions from kernel and user_ns
1870 1870 #-------------------------------------------------------------------------
1871 1871
1872 1872 def _simple_error(self):
1873 1873 etype, value = sys.exc_info()[:2]
1874 1874 return u'[ERROR] {e.__name__}: {v}'.format(e=etype, v=value)
1875 1875
1876 1876 def user_variables(self, names):
1877 1877 """Get a list of variable names from the user's namespace.
1878 1878
1879 1879 Parameters
1880 1880 ----------
1881 1881 names : list of strings
1882 1882 A list of names of variables to be read from the user namespace.
1883 1883
1884 1884 Returns
1885 1885 -------
1886 1886 A dict, keyed by the input names and with the repr() of each value.
1887 1887 """
1888 1888 out = {}
1889 1889 user_ns = self.user_ns
1890 1890 for varname in names:
1891 1891 try:
1892 1892 value = repr(user_ns[varname])
1893 1893 except:
1894 1894 value = self._simple_error()
1895 1895 out[varname] = value
1896 1896 return out
1897 1897
1898 1898 def user_expressions(self, expressions):
1899 1899 """Evaluate a dict of expressions in the user's namespace.
1900 1900
1901 1901 Parameters
1902 1902 ----------
1903 1903 expressions : dict
1904 1904 A dict with string keys and string values. The expression values
1905 1905 should be valid Python expressions, each of which will be evaluated
1906 1906 in the user namespace.
1907 1907
1908 1908 Returns
1909 1909 -------
1910 1910 A dict, keyed like the input expressions dict, with the repr() of each
1911 1911 value.
1912 1912 """
1913 1913 out = {}
1914 1914 user_ns = self.user_ns
1915 1915 global_ns = self.user_global_ns
1916 1916 for key, expr in expressions.iteritems():
1917 1917 try:
1918 1918 value = repr(eval(expr, global_ns, user_ns))
1919 1919 except:
1920 1920 value = self._simple_error()
1921 1921 out[key] = value
1922 1922 return out
1923 1923
1924 1924 #-------------------------------------------------------------------------
1925 1925 # Things related to the running of code
1926 1926 #-------------------------------------------------------------------------
1927 1927
1928 1928 def ex(self, cmd):
1929 1929 """Execute a normal python statement in user namespace."""
1930 1930 with nested(self.builtin_trap,):
1931 1931 exec cmd in self.user_global_ns, self.user_ns
1932 1932
1933 1933 def ev(self, expr):
1934 1934 """Evaluate python expression expr in user namespace.
1935 1935
1936 1936 Returns the result of evaluation
1937 1937 """
1938 1938 with nested(self.builtin_trap,):
1939 1939 return eval(expr, self.user_global_ns, self.user_ns)
1940 1940
1941 1941 def safe_execfile(self, fname, *where, **kw):
1942 1942 """A safe version of the builtin execfile().
1943 1943
1944 1944 This version will never throw an exception, but instead print
1945 1945 helpful error messages to the screen. This only works on pure
1946 1946 Python files with the .py extension.
1947 1947
1948 1948 Parameters
1949 1949 ----------
1950 1950 fname : string
1951 1951 The name of the file to be executed.
1952 1952 where : tuple
1953 1953 One or two namespaces, passed to execfile() as (globals,locals).
1954 1954 If only one is given, it is passed as both.
1955 1955 exit_ignore : bool (False)
1956 1956 If True, then silence SystemExit for non-zero status (it is always
1957 1957 silenced for zero status, as it is so common).
1958 1958 """
1959 1959 kw.setdefault('exit_ignore', False)
1960 1960
1961 1961 fname = os.path.abspath(os.path.expanduser(fname))
1962 1962
1963 1963 # Make sure we have a .py file
1964 1964 if not fname.endswith('.py'):
1965 1965 warn('File must end with .py to be run using execfile: <%s>' % fname)
1966 1966
1967 1967 # Make sure we can open the file
1968 1968 try:
1969 1969 with open(fname) as thefile:
1970 1970 pass
1971 1971 except:
1972 1972 warn('Could not open file <%s> for safe execution.' % fname)
1973 1973 return
1974 1974
1975 1975 # Find things also in current directory. This is needed to mimic the
1976 1976 # behavior of running a script from the system command line, where
1977 1977 # Python inserts the script's directory into sys.path
1978 1978 dname = os.path.dirname(fname)
1979 1979
1980 1980 with prepended_to_syspath(dname):
1981 1981 try:
1982 1982 execfile(fname,*where)
1983 1983 except SystemExit, status:
1984 1984 # If the call was made with 0 or None exit status (sys.exit(0)
1985 1985 # or sys.exit() ), don't bother showing a traceback, as both of
1986 1986 # these are considered normal by the OS:
1987 1987 # > python -c'import sys;sys.exit(0)'; echo $?
1988 1988 # 0
1989 1989 # > python -c'import sys;sys.exit()'; echo $?
1990 1990 # 0
1991 1991 # For other exit status, we show the exception unless
1992 1992 # explicitly silenced, but only in short form.
1993 1993 if status.code not in (0, None) and not kw['exit_ignore']:
1994 1994 self.showtraceback(exception_only=True)
1995 1995 except:
1996 1996 self.showtraceback()
1997 1997
1998 1998 def safe_execfile_ipy(self, fname):
1999 1999 """Like safe_execfile, but for .ipy files with IPython syntax.
2000 2000
2001 2001 Parameters
2002 2002 ----------
2003 2003 fname : str
2004 2004 The name of the file to execute. The filename must have a
2005 2005 .ipy extension.
2006 2006 """
2007 2007 fname = os.path.abspath(os.path.expanduser(fname))
2008 2008
2009 2009 # Make sure we have a .py file
2010 2010 if not fname.endswith('.ipy'):
2011 2011 warn('File must end with .py to be run using execfile: <%s>' % fname)
2012 2012
2013 2013 # Make sure we can open the file
2014 2014 try:
2015 2015 with open(fname) as thefile:
2016 2016 pass
2017 2017 except:
2018 2018 warn('Could not open file <%s> for safe execution.' % fname)
2019 2019 return
2020 2020
2021 2021 # Find things also in current directory. This is needed to mimic the
2022 2022 # behavior of running a script from the system command line, where
2023 2023 # Python inserts the script's directory into sys.path
2024 2024 dname = os.path.dirname(fname)
2025 2025
2026 2026 with prepended_to_syspath(dname):
2027 2027 try:
2028 2028 with open(fname) as thefile:
2029 script = thefile.read()
2030 # self.runlines currently captures all exceptions
2031 # raise in user code. It would be nice if there were
2029 # self.run_cell currently captures all exceptions
2030 # raised in user code. It would be nice if there were
2032 2031 # versions of runlines, execfile that did raise, so
2033 2032 # we could catch the errors.
2034 self.runlines(script, clean=True)
2033 self.run_cell(thefile.read())
2035 2034 except:
2036 2035 self.showtraceback()
2037 2036 warn('Unknown failure executing file: <%s>' % fname)
2038 2037
2039 2038 def run_cell(self, cell):
2040 2039 """Run the contents of an entire multiline 'cell' of code.
2041 2040
2042 2041 The cell is split into separate blocks which can be executed
2043 2042 individually. Then, based on how many blocks there are, they are
2044 2043 executed as follows:
2045 2044
2046 2045 - A single block: 'single' mode.
2047 2046
2048 2047 If there's more than one block, it depends:
2049 2048
2050 2049 - if the last one is no more than two lines long, run all but the last
2051 2050 in 'exec' mode and the very last one in 'single' mode. This makes it
2052 2051 easy to type simple expressions at the end to see computed values. -
2053 2052 otherwise (last one is also multiline), run all in 'exec' mode
2054 2053
2055 2054 When code is executed in 'single' mode, :func:`sys.displayhook` fires,
2056 2055 results are displayed and output prompts are computed. In 'exec' mode,
2057 2056 no results are displayed unless :func:`print` is called explicitly;
2058 2057 this mode is more akin to running a script.
2059 2058
2060 2059 Parameters
2061 2060 ----------
2062 2061 cell : str
2063 2062 A single or multiline string.
2064 2063 """
2065 2064 #################################################################
2066 2065 # FIXME
2067 2066 # =====
2068 2067 # This execution logic should stop calling runlines altogether, and
2069 2068 # instead we should do what runlines does, in a controlled manner, here
2070 2069 # (runlines mutates lots of state as it goes calling sub-methods that
2071 2070 # also mutate state). Basically we should:
2072 2071 # - apply dynamic transforms for single-line input (the ones that
2073 2072 # split_blocks won't apply since they need context).
2074 2073 # - increment the global execution counter (we need to pull that out
2075 2074 # from outputcache's control; outputcache should instead read it from
2076 2075 # the main object).
2077 2076 # - do any logging of input
2078 2077 # - update histories (raw/translated)
2079 2078 # - then, call plain runsource (for single blocks, so displayhook is
2080 2079 # triggered) or runcode (for multiline blocks in exec mode).
2081 2080 #
2082 2081 # Once this is done, we'll be able to stop using runlines and we'll
2083 2082 # also have a much cleaner separation of logging, input history and
2084 2083 # output cache management.
2085 2084 #################################################################
2086 2085
2087 2086 # We need to break up the input into executable blocks that can be run
2088 2087 # in 'single' mode, to provide comfortable user behavior.
2089 2088 blocks = self.input_splitter.split_blocks(cell)
2090 2089
2091 2090 if not blocks:
2092 2091 return
2093 2092
2094 2093 # Store the 'ipython' version of the cell as well, since that's what
2095 2094 # needs to go into the translated history and get executed (the
2096 2095 # original cell may contain non-python syntax).
2097 2096 ipy_cell = ''.join(blocks)
2098 2097
2099 2098 # Store raw and processed history
2100 2099 self.history_manager.store_inputs(ipy_cell, cell)
2101 2100
2101 self.logger.log(ipy_cell, cell)
2102 2102 # dbg code!!!
2103 2103 if 0:
2104 2104 def myapp(self, val): # dbg
2105 2105 import traceback as tb
2106 2106 stack = ''.join(tb.format_stack())
2107 2107 print 'Value:', val
2108 2108 print 'Stack:\n', stack
2109 2109 list.append(self, val)
2110 2110
2111 2111 import new
2112 2112 self.input_hist.append = new.instancemethod(myapp, self.input_hist,
2113 2113 list)
2114 2114 # End dbg
2115 2115
2116 2116 # All user code execution must happen with our context managers active
2117 2117 with nested(self.builtin_trap, self.display_trap):
2118 2118
2119 2119 # Single-block input should behave like an interactive prompt
2120 2120 if len(blocks) == 1:
2121 2121 # since we return here, we need to update the execution count
2122 2122 out = self.run_one_block(blocks[0])
2123 2123 self.execution_count += 1
2124 2124 return out
2125 2125
2126 2126 # In multi-block input, if the last block is a simple (one-two
2127 2127 # lines) expression, run it in single mode so it produces output.
2128 2128 # Otherwise just feed the whole thing to runcode. This seems like
2129 2129 # a reasonable usability design.
2130 2130 last = blocks[-1]
2131 2131 last_nlines = len(last.splitlines())
2132 2132
2133 2133 # Note: below, whenever we call runcode, we must sync history
2134 2134 # ourselves, because runcode is NOT meant to manage history at all.
2135 2135 if last_nlines < 2:
2136 2136 # Here we consider the cell split between 'body' and 'last',
2137 2137 # store all history and execute 'body', and if successful, then
2138 2138 # proceed to execute 'last'.
2139 2139
2140 2140 # Get the main body to run as a cell
2141 2141 ipy_body = ''.join(blocks[:-1])
2142 2142 retcode = self.runcode(ipy_body, post_execute=False)
2143 2143 if retcode==0:
2144 2144 # And the last expression via runlines so it produces output
2145 2145 self.run_one_block(last)
2146 2146 else:
2147 2147 # Run the whole cell as one entity, storing both raw and
2148 2148 # processed input in history
2149 2149 self.runcode(ipy_cell)
2150 2150
2151 2151 # Each cell is a *single* input, regardless of how many lines it has
2152 2152 self.execution_count += 1
2153 2153
2154 2154 def run_one_block(self, block):
2155 2155 """Run a single interactive block.
2156 2156
2157 2157 If the block is single-line, dynamic transformations are applied to it
2158 2158 (like automagics, autocall and alias recognition).
2159 2159 """
2160 2160 if len(block.splitlines()) <= 1:
2161 2161 out = self.run_single_line(block)
2162 2162 else:
2163 2163 out = self.runcode(block)
2164 2164 return out
2165 2165
2166 2166 def run_single_line(self, line):
2167 2167 """Run a single-line interactive statement.
2168 2168
2169 2169 This assumes the input has been transformed to IPython syntax by
2170 2170 applying all static transformations (those with an explicit prefix like
2171 2171 % or !), but it will further try to apply the dynamic ones.
2172 2172
2173 2173 It does not update history.
2174 2174 """
2175 2175 tline = self.prefilter_manager.prefilter_line(line)
2176 2176 return self.runsource(tline)
2177 2177
2178 2178 def runlines(self, lines, clean=False):
2179 2179 """Run a string of one or more lines of source.
2180 2180
2181 2181 This method is capable of running a string containing multiple source
2182 2182 lines, as if they had been entered at the IPython prompt. Since it
2183 2183 exposes IPython's processing machinery, the given strings can contain
2184 2184 magic calls (%magic), special shell access (!cmd), etc.
2185 2185 """
2186 2186
2187 2187 if isinstance(lines, (list, tuple)):
2188 2188 lines = '\n'.join(lines)
2189 2189
2190 2190 if clean:
2191 2191 lines = self._cleanup_ipy_script(lines)
2192 2192
2193 2193 # We must start with a clean buffer, in case this is run from an
2194 2194 # interactive IPython session (via a magic, for example).
2195 2195 self.resetbuffer()
2196 2196 lines = lines.splitlines()
2197 2197
2198 2198 # Since we will prefilter all lines, store the user's raw input too
2199 2199 # before we apply any transformations
2200 2200 self.buffer_raw[:] = [ l+'\n' for l in lines]
2201 2201
2202 2202 more = False
2203 2203 prefilter_lines = self.prefilter_manager.prefilter_lines
2204 2204 with nested(self.builtin_trap, self.display_trap):
2205 2205 for line in lines:
2206 2206 # skip blank lines so we don't mess up the prompt counter, but
2207 2207 # do NOT skip even a blank line if we are in a code block (more
2208 2208 # is true)
2209 2209
2210 2210 if line or more:
2211 2211 more = self.push_line(prefilter_lines(line, more))
2212 2212 # IPython's runsource returns None if there was an error
2213 2213 # compiling the code. This allows us to stop processing
2214 2214 # right away, so the user gets the error message at the
2215 2215 # right place.
2216 2216 if more is None:
2217 2217 break
2218 2218 # final newline in case the input didn't have it, so that the code
2219 2219 # actually does get executed
2220 2220 if more:
2221 2221 self.push_line('\n')
2222 2222
2223 2223 def runsource(self, source, filename='<ipython console>', symbol='single'):
2224 2224 """Compile and run some source in the interpreter.
2225 2225
2226 2226 Arguments are as for compile_command().
2227 2227
2228 2228 One several things can happen:
2229 2229
2230 2230 1) The input is incorrect; compile_command() raised an
2231 2231 exception (SyntaxError or OverflowError). A syntax traceback
2232 2232 will be printed by calling the showsyntaxerror() method.
2233 2233
2234 2234 2) The input is incomplete, and more input is required;
2235 2235 compile_command() returned None. Nothing happens.
2236 2236
2237 2237 3) The input is complete; compile_command() returned a code
2238 2238 object. The code is executed by calling self.runcode() (which
2239 2239 also handles run-time exceptions, except for SystemExit).
2240 2240
2241 2241 The return value is:
2242 2242
2243 2243 - True in case 2
2244 2244
2245 2245 - False in the other cases, unless an exception is raised, where
2246 2246 None is returned instead. This can be used by external callers to
2247 2247 know whether to continue feeding input or not.
2248 2248
2249 2249 The return value can be used to decide whether to use sys.ps1 or
2250 2250 sys.ps2 to prompt the next line."""
2251 2251
2252 2252 # We need to ensure that the source is unicode from here on.
2253 2253 if type(source)==str:
2254 2254 source = source.decode(self.stdin_encoding)
2255 2255
2256 2256 try:
2257 2257 code = self.compile(source,filename,symbol)
2258 2258 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
2259 2259 # Case 1
2260 2260 self.showsyntaxerror(filename)
2261 2261 return None
2262 2262
2263 2263 if code is None:
2264 2264 # Case 2
2265 2265 return True
2266 2266
2267 2267 # Case 3
2268 2268 # We store the code object so that threaded shells and
2269 2269 # custom exception handlers can access all this info if needed.
2270 2270 # The source corresponding to this can be obtained from the
2271 2271 # buffer attribute as '\n'.join(self.buffer).
2272 2272 self.code_to_run = code
2273 2273 # now actually execute the code object
2274 2274 if self.runcode(code) == 0:
2275 2275 return False
2276 2276 else:
2277 2277 return None
2278 2278
2279 2279 def runcode(self, code_obj, post_execute=True):
2280 2280 """Execute a code object.
2281 2281
2282 2282 When an exception occurs, self.showtraceback() is called to display a
2283 2283 traceback.
2284 2284
2285 2285 Return value: a flag indicating whether the code to be run completed
2286 2286 successfully:
2287 2287
2288 2288 - 0: successful execution.
2289 2289 - 1: an error occurred.
2290 2290 """
2291 2291
2292 2292 # Set our own excepthook in case the user code tries to call it
2293 2293 # directly, so that the IPython crash handler doesn't get triggered
2294 2294 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2295 2295
2296 2296 # we save the original sys.excepthook in the instance, in case config
2297 2297 # code (such as magics) needs access to it.
2298 2298 self.sys_excepthook = old_excepthook
2299 2299 outflag = 1 # happens in more places, so it's easier as default
2300 2300 try:
2301 2301 try:
2302 2302 self.hooks.pre_runcode_hook()
2303 2303 #rprint('Running code') # dbg
2304 2304 exec code_obj in self.user_global_ns, self.user_ns
2305 2305 finally:
2306 2306 # Reset our crash handler in place
2307 2307 sys.excepthook = old_excepthook
2308 2308 except SystemExit:
2309 2309 self.resetbuffer()
2310 2310 self.showtraceback(exception_only=True)
2311 2311 warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1)
2312 2312 except self.custom_exceptions:
2313 2313 etype,value,tb = sys.exc_info()
2314 2314 self.CustomTB(etype,value,tb)
2315 2315 except:
2316 2316 self.showtraceback()
2317 2317 else:
2318 2318 outflag = 0
2319 2319 if softspace(sys.stdout, 0):
2320 2320 print
2321 2321
2322 2322 # Execute any registered post-execution functions. Here, any errors
2323 2323 # are reported only minimally and just on the terminal, because the
2324 2324 # main exception channel may be occupied with a user traceback.
2325 2325 # FIXME: we need to think this mechanism a little more carefully.
2326 2326 if post_execute:
2327 2327 for func in self._post_execute:
2328 2328 try:
2329 2329 func()
2330 2330 except:
2331 2331 head = '[ ERROR ] Evaluating post_execute function: %s' % \
2332 2332 func
2333 2333 print >> io.Term.cout, head
2334 2334 print >> io.Term.cout, self._simple_error()
2335 2335 print >> io.Term.cout, 'Removing from post_execute'
2336 2336 self._post_execute.remove(func)
2337 2337
2338 2338 # Flush out code object which has been run (and source)
2339 2339 self.code_to_run = None
2340 2340 return outflag
2341 2341
2342 2342 def push_line(self, line):
2343 2343 """Push a line to the interpreter.
2344 2344
2345 2345 The line should not have a trailing newline; it may have
2346 2346 internal newlines. The line is appended to a buffer and the
2347 2347 interpreter's runsource() method is called with the
2348 2348 concatenated contents of the buffer as source. If this
2349 2349 indicates that the command was executed or invalid, the buffer
2350 2350 is reset; otherwise, the command is incomplete, and the buffer
2351 2351 is left as it was after the line was appended. The return
2352 2352 value is 1 if more input is required, 0 if the line was dealt
2353 2353 with in some way (this is the same as runsource()).
2354 2354 """
2355 2355
2356 2356 # autoindent management should be done here, and not in the
2357 2357 # interactive loop, since that one is only seen by keyboard input. We
2358 2358 # need this done correctly even for code run via runlines (which uses
2359 2359 # push).
2360 2360
2361 2361 #print 'push line: <%s>' % line # dbg
2362 2362 self.buffer.append(line)
2363 2363 full_source = '\n'.join(self.buffer)
2364 2364 more = self.runsource(full_source, self.filename)
2365 2365 if not more:
2366 2366 self.history_manager.store_inputs('\n'.join(self.buffer_raw),
2367 2367 full_source)
2368 2368 self.resetbuffer()
2369 2369 self.execution_count += 1
2370 2370 return more
2371 2371
2372 2372 def resetbuffer(self):
2373 2373 """Reset the input buffer."""
2374 2374 self.buffer[:] = []
2375 2375 self.buffer_raw[:] = []
2376 2376 self.input_splitter.reset()
2377 2377
2378 2378 def _is_secondary_block_start(self, s):
2379 2379 if not s.endswith(':'):
2380 2380 return False
2381 2381 if (s.startswith('elif') or
2382 2382 s.startswith('else') or
2383 2383 s.startswith('except') or
2384 2384 s.startswith('finally')):
2385 2385 return True
2386 2386
2387 2387 def _cleanup_ipy_script(self, script):
2388 2388 """Make a script safe for self.runlines()
2389 2389
2390 2390 Currently, IPython is lines based, with blocks being detected by
2391 2391 empty lines. This is a problem for block based scripts that may
2392 2392 not have empty lines after blocks. This script adds those empty
2393 2393 lines to make scripts safe for running in the current line based
2394 2394 IPython.
2395 2395 """
2396 2396 res = []
2397 2397 lines = script.splitlines()
2398 2398 level = 0
2399 2399
2400 2400 for l in lines:
2401 2401 lstripped = l.lstrip()
2402 2402 stripped = l.strip()
2403 2403 if not stripped:
2404 2404 continue
2405 2405 newlevel = len(l) - len(lstripped)
2406 2406 if level > 0 and newlevel == 0 and \
2407 2407 not self._is_secondary_block_start(stripped):
2408 2408 # add empty line
2409 2409 res.append('')
2410 2410 res.append(l)
2411 2411 level = newlevel
2412 2412
2413 2413 return '\n'.join(res) + '\n'
2414 2414
2415 2415 #-------------------------------------------------------------------------
2416 2416 # Things related to GUI support and pylab
2417 2417 #-------------------------------------------------------------------------
2418 2418
2419 2419 def enable_pylab(self, gui=None):
2420 2420 raise NotImplementedError('Implement enable_pylab in a subclass')
2421 2421
2422 2422 #-------------------------------------------------------------------------
2423 2423 # Utilities
2424 2424 #-------------------------------------------------------------------------
2425 2425
2426 2426 def var_expand(self,cmd,depth=0):
2427 2427 """Expand python variables in a string.
2428 2428
2429 2429 The depth argument indicates how many frames above the caller should
2430 2430 be walked to look for the local namespace where to expand variables.
2431 2431
2432 2432 The global namespace for expansion is always the user's interactive
2433 2433 namespace.
2434 2434 """
2435 2435
2436 2436 return str(ItplNS(cmd,
2437 2437 self.user_ns, # globals
2438 2438 # Skip our own frame in searching for locals:
2439 2439 sys._getframe(depth+1).f_locals # locals
2440 2440 ))
2441 2441
2442 2442 def mktempfile(self,data=None):
2443 2443 """Make a new tempfile and return its filename.
2444 2444
2445 2445 This makes a call to tempfile.mktemp, but it registers the created
2446 2446 filename internally so ipython cleans it up at exit time.
2447 2447
2448 2448 Optional inputs:
2449 2449
2450 2450 - data(None): if data is given, it gets written out to the temp file
2451 2451 immediately, and the file is closed again."""
2452 2452
2453 2453 filename = tempfile.mktemp('.py','ipython_edit_')
2454 2454 self.tempfiles.append(filename)
2455 2455
2456 2456 if data:
2457 2457 tmp_file = open(filename,'w')
2458 2458 tmp_file.write(data)
2459 2459 tmp_file.close()
2460 2460 return filename
2461 2461
2462 2462 # TODO: This should be removed when Term is refactored.
2463 2463 def write(self,data):
2464 2464 """Write a string to the default output"""
2465 2465 io.Term.cout.write(data)
2466 2466
2467 2467 # TODO: This should be removed when Term is refactored.
2468 2468 def write_err(self,data):
2469 2469 """Write a string to the default error output"""
2470 2470 io.Term.cerr.write(data)
2471 2471
2472 2472 def ask_yes_no(self,prompt,default=True):
2473 2473 if self.quiet:
2474 2474 return True
2475 2475 return ask_yes_no(prompt,default)
2476 2476
2477 2477 def show_usage(self):
2478 2478 """Show a usage message"""
2479 2479 page.page(IPython.core.usage.interactive_usage)
2480 2480
2481 2481 #-------------------------------------------------------------------------
2482 2482 # Things related to IPython exiting
2483 2483 #-------------------------------------------------------------------------
2484 2484 def atexit_operations(self):
2485 2485 """This will be executed at the time of exit.
2486 2486
2487 2487 Cleanup operations and saving of persistent data that is done
2488 2488 unconditionally by IPython should be performed here.
2489 2489
2490 2490 For things that may depend on startup flags or platform specifics (such
2491 2491 as having readline or not), register a separate atexit function in the
2492 2492 code that has the appropriate information, rather than trying to
2493 2493 clutter
2494 2494 """
2495 2495 # Cleanup all tempfiles left around
2496 2496 for tfile in self.tempfiles:
2497 2497 try:
2498 2498 os.unlink(tfile)
2499 2499 except OSError:
2500 2500 pass
2501 2501
2502 2502 # Clear all user namespaces to release all references cleanly.
2503 2503 self.reset()
2504 2504
2505 2505 # Run user hooks
2506 2506 self.hooks.shutdown_hook()
2507 2507
2508 2508 def cleanup(self):
2509 2509 self.restore_sys_module_state()
2510 2510
2511 2511
2512 2512 class InteractiveShellABC(object):
2513 2513 """An abstract base class for InteractiveShell."""
2514 2514 __metaclass__ = abc.ABCMeta
2515 2515
2516 2516 InteractiveShellABC.register(InteractiveShell)
@@ -1,41 +1,39 b''
1 1 """Support for interactive macros in IPython"""
2 2
3 3 #*****************************************************************************
4 4 # Copyright (C) 2001-2005 Fernando Perez <fperez@colorado.edu>
5 5 #
6 6 # Distributed under the terms of the BSD License. The full license is in
7 7 # the file COPYING, distributed as part of this software.
8 8 #*****************************************************************************
9 9
10 10 import IPython.utils.io
11 11 from IPython.core.autocall import IPyAutocall
12 12
13 13 class Macro(IPyAutocall):
14 14 """Simple class to store the value of macros as strings.
15 15
16 16 Macro is just a callable that executes a string of IPython
17 17 input when called.
18 18
19 19 Args to macro are available in _margv list if you need them.
20 20 """
21 21
22 22 def __init__(self,data):
23
24 # store the macro value, as a single string which can be evaluated by
25 # runlines()
23 # store the macro value, as a single string which can be executed
26 24 self.value = ''.join(data).rstrip()+'\n'
27 25
28 26 def __str__(self):
29 27 return self.value
30 28
31 29 def __repr__(self):
32 30 return 'IPython.macro.Macro(%s)' % repr(self.value)
33 31
34 32 def __call__(self,*args):
35 33 IPython.utils.io.Term.cout.flush()
36 34 self._ip.user_ns['_margv'] = args
37 self._ip.runlines(self.value)
35 self._ip.run_cell(self.value)
38 36
39 37 def __getstate__(self):
40 38 """ needed for safe pickling via %store """
41 39 return {'value': self.value}
@@ -1,3372 +1,3367 b''
1 1 # encoding: utf-8
2 2 """Magic functions for InteractiveShell.
3 3 """
4 4
5 5 #-----------------------------------------------------------------------------
6 6 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
7 7 # Copyright (C) 2001-2007 Fernando Perez <fperez@colorado.edu>
8 8 # Copyright (C) 2008-2009 The IPython Development Team
9 9
10 10 # Distributed under the terms of the BSD License. The full license is in
11 11 # the file COPYING, distributed as part of this software.
12 12 #-----------------------------------------------------------------------------
13 13
14 14 #-----------------------------------------------------------------------------
15 15 # Imports
16 16 #-----------------------------------------------------------------------------
17 17
18 18 import __builtin__
19 19 import __future__
20 20 import bdb
21 21 import inspect
22 22 import os
23 23 import sys
24 24 import shutil
25 25 import re
26 26 import time
27 27 import textwrap
28 28 import types
29 29 from cStringIO import StringIO
30 30 from getopt import getopt,GetoptError
31 31 from pprint import pformat
32 32
33 33 # cProfile was added in Python2.5
34 34 try:
35 35 import cProfile as profile
36 36 import pstats
37 37 except ImportError:
38 38 # profile isn't bundled by default in Debian for license reasons
39 39 try:
40 40 import profile,pstats
41 41 except ImportError:
42 42 profile = pstats = None
43 43
44 # print_function was added to __future__ in Python2.6, remove this when we drop
45 # 2.5 compatibility
46 if not hasattr(__future__,'CO_FUTURE_PRINT_FUNCTION'):
47 __future__.CO_FUTURE_PRINT_FUNCTION = 65536
48
49 44 import IPython
50 45 from IPython.core import debugger, oinspect
51 46 from IPython.core.error import TryNext
52 47 from IPython.core.error import UsageError
53 48 from IPython.core.fakemodule import FakeModule
54 49 from IPython.core.macro import Macro
55 50 from IPython.core import page
56 51 from IPython.core.prefilter import ESC_MAGIC
57 52 from IPython.lib.pylabtools import mpl_runner
58 53 from IPython.external.Itpl import itpl, printpl
59 54 from IPython.testing import decorators as testdec
60 55 from IPython.utils.io import file_read, nlprint
61 56 import IPython.utils.io
62 57 from IPython.utils.path import get_py_filename
63 58 from IPython.utils.process import arg_split, abbrev_cwd
64 59 from IPython.utils.terminal import set_term_title
65 60 from IPython.utils.text import LSString, SList, StringTypes, format_screen
66 61 from IPython.utils.timing import clock, clock2
67 62 from IPython.utils.warn import warn, error
68 63 from IPython.utils.ipstruct import Struct
69 64 import IPython.utils.generics
70 65
71 66 #-----------------------------------------------------------------------------
72 67 # Utility functions
73 68 #-----------------------------------------------------------------------------
74 69
75 70 def on_off(tag):
76 71 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
77 72 return ['OFF','ON'][tag]
78 73
79 74 class Bunch: pass
80 75
81 76 def compress_dhist(dh):
82 77 head, tail = dh[:-10], dh[-10:]
83 78
84 79 newhead = []
85 80 done = set()
86 81 for h in head:
87 82 if h in done:
88 83 continue
89 84 newhead.append(h)
90 85 done.add(h)
91 86
92 87 return newhead + tail
93 88
94 89
95 90 #***************************************************************************
96 91 # Main class implementing Magic functionality
97 92
98 93 # XXX - for some odd reason, if Magic is made a new-style class, we get errors
99 94 # on construction of the main InteractiveShell object. Something odd is going
100 95 # on with super() calls, Configurable and the MRO... For now leave it as-is, but
101 96 # eventually this needs to be clarified.
102 97 # BG: This is because InteractiveShell inherits from this, but is itself a
103 98 # Configurable. This messes up the MRO in some way. The fix is that we need to
104 99 # make Magic a configurable that InteractiveShell does not subclass.
105 100
106 101 class Magic:
107 102 """Magic functions for InteractiveShell.
108 103
109 104 Shell functions which can be reached as %function_name. All magic
110 105 functions should accept a string, which they can parse for their own
111 106 needs. This can make some functions easier to type, eg `%cd ../`
112 107 vs. `%cd("../")`
113 108
114 109 ALL definitions MUST begin with the prefix magic_. The user won't need it
115 110 at the command line, but it is is needed in the definition. """
116 111
117 112 # class globals
118 113 auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.',
119 114 'Automagic is ON, % prefix NOT needed for magic functions.']
120 115
121 116 #......................................................................
122 117 # some utility functions
123 118
124 119 def __init__(self,shell):
125 120
126 121 self.options_table = {}
127 122 if profile is None:
128 123 self.magic_prun = self.profile_missing_notice
129 124 self.shell = shell
130 125
131 126 # namespace for holding state we may need
132 127 self._magic_state = Bunch()
133 128
134 129 def profile_missing_notice(self, *args, **kwargs):
135 130 error("""\
136 131 The profile module could not be found. It has been removed from the standard
137 132 python packages because of its non-free license. To use profiling, install the
138 133 python-profiler package from non-free.""")
139 134
140 135 def default_option(self,fn,optstr):
141 136 """Make an entry in the options_table for fn, with value optstr"""
142 137
143 138 if fn not in self.lsmagic():
144 139 error("%s is not a magic function" % fn)
145 140 self.options_table[fn] = optstr
146 141
147 142 def lsmagic(self):
148 143 """Return a list of currently available magic functions.
149 144
150 145 Gives a list of the bare names after mangling (['ls','cd', ...], not
151 146 ['magic_ls','magic_cd',...]"""
152 147
153 148 # FIXME. This needs a cleanup, in the way the magics list is built.
154 149
155 150 # magics in class definition
156 151 class_magic = lambda fn: fn.startswith('magic_') and \
157 152 callable(Magic.__dict__[fn])
158 153 # in instance namespace (run-time user additions)
159 154 inst_magic = lambda fn: fn.startswith('magic_') and \
160 155 callable(self.__dict__[fn])
161 156 # and bound magics by user (so they can access self):
162 157 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
163 158 callable(self.__class__.__dict__[fn])
164 159 magics = filter(class_magic,Magic.__dict__.keys()) + \
165 160 filter(inst_magic,self.__dict__.keys()) + \
166 161 filter(inst_bound_magic,self.__class__.__dict__.keys())
167 162 out = []
168 163 for fn in set(magics):
169 164 out.append(fn.replace('magic_','',1))
170 165 out.sort()
171 166 return out
172 167
173 168 def extract_input_slices(self,slices,raw=False):
174 169 """Return as a string a set of input history slices.
175 170
176 171 Inputs:
177 172
178 173 - slices: the set of slices is given as a list of strings (like
179 174 ['1','4:8','9'], since this function is for use by magic functions
180 175 which get their arguments as strings.
181 176
182 177 Optional inputs:
183 178
184 179 - raw(False): by default, the processed input is used. If this is
185 180 true, the raw input history is used instead.
186 181
187 182 Note that slices can be called with two notations:
188 183
189 184 N:M -> standard python form, means including items N...(M-1).
190 185
191 186 N-M -> include items N..M (closed endpoint)."""
192 187
193 188 if raw:
194 189 hist = self.shell.input_hist_raw
195 190 else:
196 191 hist = self.shell.input_hist
197 192
198 193 cmds = []
199 194 for chunk in slices:
200 195 if ':' in chunk:
201 196 ini,fin = map(int,chunk.split(':'))
202 197 elif '-' in chunk:
203 198 ini,fin = map(int,chunk.split('-'))
204 199 fin += 1
205 200 else:
206 201 ini = int(chunk)
207 202 fin = ini+1
208 203 cmds.append(hist[ini:fin])
209 204 return cmds
210 205
211 206 def arg_err(self,func):
212 207 """Print docstring if incorrect arguments were passed"""
213 208 print 'Error in arguments:'
214 209 print oinspect.getdoc(func)
215 210
216 211 def format_latex(self,strng):
217 212 """Format a string for latex inclusion."""
218 213
219 214 # Characters that need to be escaped for latex:
220 215 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
221 216 # Magic command names as headers:
222 217 cmd_name_re = re.compile(r'^(%s.*?):' % ESC_MAGIC,
223 218 re.MULTILINE)
224 219 # Magic commands
225 220 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % ESC_MAGIC,
226 221 re.MULTILINE)
227 222 # Paragraph continue
228 223 par_re = re.compile(r'\\$',re.MULTILINE)
229 224
230 225 # The "\n" symbol
231 226 newline_re = re.compile(r'\\n')
232 227
233 228 # Now build the string for output:
234 229 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
235 230 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
236 231 strng)
237 232 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
238 233 strng = par_re.sub(r'\\\\',strng)
239 234 strng = escape_re.sub(r'\\\1',strng)
240 235 strng = newline_re.sub(r'\\textbackslash{}n',strng)
241 236 return strng
242 237
243 238 def parse_options(self,arg_str,opt_str,*long_opts,**kw):
244 239 """Parse options passed to an argument string.
245 240
246 241 The interface is similar to that of getopt(), but it returns back a
247 242 Struct with the options as keys and the stripped argument string still
248 243 as a string.
249 244
250 245 arg_str is quoted as a true sys.argv vector by using shlex.split.
251 246 This allows us to easily expand variables, glob files, quote
252 247 arguments, etc.
253 248
254 249 Options:
255 250 -mode: default 'string'. If given as 'list', the argument string is
256 251 returned as a list (split on whitespace) instead of a string.
257 252
258 253 -list_all: put all option values in lists. Normally only options
259 254 appearing more than once are put in a list.
260 255
261 256 -posix (True): whether to split the input line in POSIX mode or not,
262 257 as per the conventions outlined in the shlex module from the
263 258 standard library."""
264 259
265 260 # inject default options at the beginning of the input line
266 261 caller = sys._getframe(1).f_code.co_name.replace('magic_','')
267 262 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
268 263
269 264 mode = kw.get('mode','string')
270 265 if mode not in ['string','list']:
271 266 raise ValueError,'incorrect mode given: %s' % mode
272 267 # Get options
273 268 list_all = kw.get('list_all',0)
274 269 posix = kw.get('posix', os.name == 'posix')
275 270
276 271 # Check if we have more than one argument to warrant extra processing:
277 272 odict = {} # Dictionary with options
278 273 args = arg_str.split()
279 274 if len(args) >= 1:
280 275 # If the list of inputs only has 0 or 1 thing in it, there's no
281 276 # need to look for options
282 277 argv = arg_split(arg_str,posix)
283 278 # Do regular option processing
284 279 try:
285 280 opts,args = getopt(argv,opt_str,*long_opts)
286 281 except GetoptError,e:
287 282 raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
288 283 " ".join(long_opts)))
289 284 for o,a in opts:
290 285 if o.startswith('--'):
291 286 o = o[2:]
292 287 else:
293 288 o = o[1:]
294 289 try:
295 290 odict[o].append(a)
296 291 except AttributeError:
297 292 odict[o] = [odict[o],a]
298 293 except KeyError:
299 294 if list_all:
300 295 odict[o] = [a]
301 296 else:
302 297 odict[o] = a
303 298
304 299 # Prepare opts,args for return
305 300 opts = Struct(odict)
306 301 if mode == 'string':
307 302 args = ' '.join(args)
308 303
309 304 return opts,args
310 305
311 306 #......................................................................
312 307 # And now the actual magic functions
313 308
314 309 # Functions for IPython shell work (vars,funcs, config, etc)
315 310 def magic_lsmagic(self, parameter_s = ''):
316 311 """List currently available magic functions."""
317 312 mesc = ESC_MAGIC
318 313 print 'Available magic functions:\n'+mesc+\
319 314 (' '+mesc).join(self.lsmagic())
320 315 print '\n' + Magic.auto_status[self.shell.automagic]
321 316 return None
322 317
323 318 def magic_magic(self, parameter_s = ''):
324 319 """Print information about the magic function system.
325 320
326 321 Supported formats: -latex, -brief, -rest
327 322 """
328 323
329 324 mode = ''
330 325 try:
331 326 if parameter_s.split()[0] == '-latex':
332 327 mode = 'latex'
333 328 if parameter_s.split()[0] == '-brief':
334 329 mode = 'brief'
335 330 if parameter_s.split()[0] == '-rest':
336 331 mode = 'rest'
337 332 rest_docs = []
338 333 except:
339 334 pass
340 335
341 336 magic_docs = []
342 337 for fname in self.lsmagic():
343 338 mname = 'magic_' + fname
344 339 for space in (Magic,self,self.__class__):
345 340 try:
346 341 fn = space.__dict__[mname]
347 342 except KeyError:
348 343 pass
349 344 else:
350 345 break
351 346 if mode == 'brief':
352 347 # only first line
353 348 if fn.__doc__:
354 349 fndoc = fn.__doc__.split('\n',1)[0]
355 350 else:
356 351 fndoc = 'No documentation'
357 352 else:
358 353 if fn.__doc__:
359 354 fndoc = fn.__doc__.rstrip()
360 355 else:
361 356 fndoc = 'No documentation'
362 357
363 358
364 359 if mode == 'rest':
365 360 rest_docs.append('**%s%s**::\n\n\t%s\n\n' %(ESC_MAGIC,
366 361 fname,fndoc))
367 362
368 363 else:
369 364 magic_docs.append('%s%s:\n\t%s\n' %(ESC_MAGIC,
370 365 fname,fndoc))
371 366
372 367 magic_docs = ''.join(magic_docs)
373 368
374 369 if mode == 'rest':
375 370 return "".join(rest_docs)
376 371
377 372 if mode == 'latex':
378 373 print self.format_latex(magic_docs)
379 374 return
380 375 else:
381 376 magic_docs = format_screen(magic_docs)
382 377 if mode == 'brief':
383 378 return magic_docs
384 379
385 380 outmsg = """
386 381 IPython's 'magic' functions
387 382 ===========================
388 383
389 384 The magic function system provides a series of functions which allow you to
390 385 control the behavior of IPython itself, plus a lot of system-type
391 386 features. All these functions are prefixed with a % character, but parameters
392 387 are given without parentheses or quotes.
393 388
394 389 NOTE: If you have 'automagic' enabled (via the command line option or with the
395 390 %automagic function), you don't need to type in the % explicitly. By default,
396 391 IPython ships with automagic on, so you should only rarely need the % escape.
397 392
398 393 Example: typing '%cd mydir' (without the quotes) changes you working directory
399 394 to 'mydir', if it exists.
400 395
401 396 You can define your own magic functions to extend the system. See the supplied
402 397 ipythonrc and example-magic.py files for details (in your ipython
403 398 configuration directory, typically $HOME/.ipython/).
404 399
405 400 You can also define your own aliased names for magic functions. In your
406 401 ipythonrc file, placing a line like:
407 402
408 403 execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile
409 404
410 405 will define %pf as a new name for %profile.
411 406
412 407 You can also call magics in code using the magic() function, which IPython
413 408 automatically adds to the builtin namespace. Type 'magic?' for details.
414 409
415 410 For a list of the available magic functions, use %lsmagic. For a description
416 411 of any of them, type %magic_name?, e.g. '%cd?'.
417 412
418 413 Currently the magic system has the following functions:\n"""
419 414
420 415 mesc = ESC_MAGIC
421 416 outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):"
422 417 "\n\n%s%s\n\n%s" % (outmsg,
423 418 magic_docs,mesc,mesc,
424 419 (' '+mesc).join(self.lsmagic()),
425 420 Magic.auto_status[self.shell.automagic] ) )
426 421 page.page(outmsg)
427 422
428 423 def magic_automagic(self, parameter_s = ''):
429 424 """Make magic functions callable without having to type the initial %.
430 425
431 426 Without argumentsl toggles on/off (when off, you must call it as
432 427 %automagic, of course). With arguments it sets the value, and you can
433 428 use any of (case insensitive):
434 429
435 430 - on,1,True: to activate
436 431
437 432 - off,0,False: to deactivate.
438 433
439 434 Note that magic functions have lowest priority, so if there's a
440 435 variable whose name collides with that of a magic fn, automagic won't
441 436 work for that function (you get the variable instead). However, if you
442 437 delete the variable (del var), the previously shadowed magic function
443 438 becomes visible to automagic again."""
444 439
445 440 arg = parameter_s.lower()
446 441 if parameter_s in ('on','1','true'):
447 442 self.shell.automagic = True
448 443 elif parameter_s in ('off','0','false'):
449 444 self.shell.automagic = False
450 445 else:
451 446 self.shell.automagic = not self.shell.automagic
452 447 print '\n' + Magic.auto_status[self.shell.automagic]
453 448
454 449 @testdec.skip_doctest
455 450 def magic_autocall(self, parameter_s = ''):
456 451 """Make functions callable without having to type parentheses.
457 452
458 453 Usage:
459 454
460 455 %autocall [mode]
461 456
462 457 The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the
463 458 value is toggled on and off (remembering the previous state).
464 459
465 460 In more detail, these values mean:
466 461
467 462 0 -> fully disabled
468 463
469 464 1 -> active, but do not apply if there are no arguments on the line.
470 465
471 466 In this mode, you get:
472 467
473 468 In [1]: callable
474 469 Out[1]: <built-in function callable>
475 470
476 471 In [2]: callable 'hello'
477 472 ------> callable('hello')
478 473 Out[2]: False
479 474
480 475 2 -> Active always. Even if no arguments are present, the callable
481 476 object is called:
482 477
483 478 In [2]: float
484 479 ------> float()
485 480 Out[2]: 0.0
486 481
487 482 Note that even with autocall off, you can still use '/' at the start of
488 483 a line to treat the first argument on the command line as a function
489 484 and add parentheses to it:
490 485
491 486 In [8]: /str 43
492 487 ------> str(43)
493 488 Out[8]: '43'
494 489
495 490 # all-random (note for auto-testing)
496 491 """
497 492
498 493 if parameter_s:
499 494 arg = int(parameter_s)
500 495 else:
501 496 arg = 'toggle'
502 497
503 498 if not arg in (0,1,2,'toggle'):
504 499 error('Valid modes: (0->Off, 1->Smart, 2->Full')
505 500 return
506 501
507 502 if arg in (0,1,2):
508 503 self.shell.autocall = arg
509 504 else: # toggle
510 505 if self.shell.autocall:
511 506 self._magic_state.autocall_save = self.shell.autocall
512 507 self.shell.autocall = 0
513 508 else:
514 509 try:
515 510 self.shell.autocall = self._magic_state.autocall_save
516 511 except AttributeError:
517 512 self.shell.autocall = self._magic_state.autocall_save = 1
518 513
519 514 print "Automatic calling is:",['OFF','Smart','Full'][self.shell.autocall]
520 515
521 516
522 517 def magic_page(self, parameter_s=''):
523 518 """Pretty print the object and display it through a pager.
524 519
525 520 %page [options] OBJECT
526 521
527 522 If no object is given, use _ (last output).
528 523
529 524 Options:
530 525
531 526 -r: page str(object), don't pretty-print it."""
532 527
533 528 # After a function contributed by Olivier Aubert, slightly modified.
534 529
535 530 # Process options/args
536 531 opts,args = self.parse_options(parameter_s,'r')
537 532 raw = 'r' in opts
538 533
539 534 oname = args and args or '_'
540 535 info = self._ofind(oname)
541 536 if info['found']:
542 537 txt = (raw and str or pformat)( info['obj'] )
543 538 page.page(txt)
544 539 else:
545 540 print 'Object `%s` not found' % oname
546 541
547 542 def magic_profile(self, parameter_s=''):
548 543 """Print your currently active IPython profile."""
549 544 if self.shell.profile:
550 545 printpl('Current IPython profile: $self.shell.profile.')
551 546 else:
552 547 print 'No profile active.'
553 548
554 549 def magic_pinfo(self, parameter_s='', namespaces=None):
555 550 """Provide detailed information about an object.
556 551
557 552 '%pinfo object' is just a synonym for object? or ?object."""
558 553
559 554 #print 'pinfo par: <%s>' % parameter_s # dbg
560 555
561 556
562 557 # detail_level: 0 -> obj? , 1 -> obj??
563 558 detail_level = 0
564 559 # We need to detect if we got called as 'pinfo pinfo foo', which can
565 560 # happen if the user types 'pinfo foo?' at the cmd line.
566 561 pinfo,qmark1,oname,qmark2 = \
567 562 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
568 563 if pinfo or qmark1 or qmark2:
569 564 detail_level = 1
570 565 if "*" in oname:
571 566 self.magic_psearch(oname)
572 567 else:
573 568 self.shell._inspect('pinfo', oname, detail_level=detail_level,
574 569 namespaces=namespaces)
575 570
576 571 def magic_pinfo2(self, parameter_s='', namespaces=None):
577 572 """Provide extra detailed information about an object.
578 573
579 574 '%pinfo2 object' is just a synonym for object?? or ??object."""
580 575 self.shell._inspect('pinfo', parameter_s, detail_level=1,
581 576 namespaces=namespaces)
582 577
583 578 def magic_pdef(self, parameter_s='', namespaces=None):
584 579 """Print the definition header for any callable object.
585 580
586 581 If the object is a class, print the constructor information."""
587 582 self._inspect('pdef',parameter_s, namespaces)
588 583
589 584 def magic_pdoc(self, parameter_s='', namespaces=None):
590 585 """Print the docstring for an object.
591 586
592 587 If the given object is a class, it will print both the class and the
593 588 constructor docstrings."""
594 589 self._inspect('pdoc',parameter_s, namespaces)
595 590
596 591 def magic_psource(self, parameter_s='', namespaces=None):
597 592 """Print (or run through pager) the source code for an object."""
598 593 self._inspect('psource',parameter_s, namespaces)
599 594
600 595 def magic_pfile(self, parameter_s=''):
601 596 """Print (or run through pager) the file where an object is defined.
602 597
603 598 The file opens at the line where the object definition begins. IPython
604 599 will honor the environment variable PAGER if set, and otherwise will
605 600 do its best to print the file in a convenient form.
606 601
607 602 If the given argument is not an object currently defined, IPython will
608 603 try to interpret it as a filename (automatically adding a .py extension
609 604 if needed). You can thus use %pfile as a syntax highlighting code
610 605 viewer."""
611 606
612 607 # first interpret argument as an object name
613 608 out = self._inspect('pfile',parameter_s)
614 609 # if not, try the input as a filename
615 610 if out == 'not found':
616 611 try:
617 612 filename = get_py_filename(parameter_s)
618 613 except IOError,msg:
619 614 print msg
620 615 return
621 616 page.page(self.shell.inspector.format(file(filename).read()))
622 617
623 618 def magic_psearch(self, parameter_s=''):
624 619 """Search for object in namespaces by wildcard.
625 620
626 621 %psearch [options] PATTERN [OBJECT TYPE]
627 622
628 623 Note: ? can be used as a synonym for %psearch, at the beginning or at
629 624 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
630 625 rest of the command line must be unchanged (options come first), so
631 626 for example the following forms are equivalent
632 627
633 628 %psearch -i a* function
634 629 -i a* function?
635 630 ?-i a* function
636 631
637 632 Arguments:
638 633
639 634 PATTERN
640 635
641 636 where PATTERN is a string containing * as a wildcard similar to its
642 637 use in a shell. The pattern is matched in all namespaces on the
643 638 search path. By default objects starting with a single _ are not
644 639 matched, many IPython generated objects have a single
645 640 underscore. The default is case insensitive matching. Matching is
646 641 also done on the attributes of objects and not only on the objects
647 642 in a module.
648 643
649 644 [OBJECT TYPE]
650 645
651 646 Is the name of a python type from the types module. The name is
652 647 given in lowercase without the ending type, ex. StringType is
653 648 written string. By adding a type here only objects matching the
654 649 given type are matched. Using all here makes the pattern match all
655 650 types (this is the default).
656 651
657 652 Options:
658 653
659 654 -a: makes the pattern match even objects whose names start with a
660 655 single underscore. These names are normally ommitted from the
661 656 search.
662 657
663 658 -i/-c: make the pattern case insensitive/sensitive. If neither of
664 659 these options is given, the default is read from your ipythonrc
665 660 file. The option name which sets this value is
666 661 'wildcards_case_sensitive'. If this option is not specified in your
667 662 ipythonrc file, IPython's internal default is to do a case sensitive
668 663 search.
669 664
670 665 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
671 666 specifiy can be searched in any of the following namespaces:
672 667 'builtin', 'user', 'user_global','internal', 'alias', where
673 668 'builtin' and 'user' are the search defaults. Note that you should
674 669 not use quotes when specifying namespaces.
675 670
676 671 'Builtin' contains the python module builtin, 'user' contains all
677 672 user data, 'alias' only contain the shell aliases and no python
678 673 objects, 'internal' contains objects used by IPython. The
679 674 'user_global' namespace is only used by embedded IPython instances,
680 675 and it contains module-level globals. You can add namespaces to the
681 676 search with -s or exclude them with -e (these options can be given
682 677 more than once).
683 678
684 679 Examples:
685 680
686 681 %psearch a* -> objects beginning with an a
687 682 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
688 683 %psearch a* function -> all functions beginning with an a
689 684 %psearch re.e* -> objects beginning with an e in module re
690 685 %psearch r*.e* -> objects that start with e in modules starting in r
691 686 %psearch r*.* string -> all strings in modules beginning with r
692 687
693 688 Case sensitve search:
694 689
695 690 %psearch -c a* list all object beginning with lower case a
696 691
697 692 Show objects beginning with a single _:
698 693
699 694 %psearch -a _* list objects beginning with a single underscore"""
700 695 try:
701 696 parameter_s = parameter_s.encode('ascii')
702 697 except UnicodeEncodeError:
703 698 print 'Python identifiers can only contain ascii characters.'
704 699 return
705 700
706 701 # default namespaces to be searched
707 702 def_search = ['user','builtin']
708 703
709 704 # Process options/args
710 705 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
711 706 opt = opts.get
712 707 shell = self.shell
713 708 psearch = shell.inspector.psearch
714 709
715 710 # select case options
716 711 if opts.has_key('i'):
717 712 ignore_case = True
718 713 elif opts.has_key('c'):
719 714 ignore_case = False
720 715 else:
721 716 ignore_case = not shell.wildcards_case_sensitive
722 717
723 718 # Build list of namespaces to search from user options
724 719 def_search.extend(opt('s',[]))
725 720 ns_exclude = ns_exclude=opt('e',[])
726 721 ns_search = [nm for nm in def_search if nm not in ns_exclude]
727 722
728 723 # Call the actual search
729 724 try:
730 725 psearch(args,shell.ns_table,ns_search,
731 726 show_all=opt('a'),ignore_case=ignore_case)
732 727 except:
733 728 shell.showtraceback()
734 729
735 730 def magic_who_ls(self, parameter_s=''):
736 731 """Return a sorted list of all interactive variables.
737 732
738 733 If arguments are given, only variables of types matching these
739 734 arguments are returned."""
740 735
741 736 user_ns = self.shell.user_ns
742 737 internal_ns = self.shell.internal_ns
743 738 user_ns_hidden = self.shell.user_ns_hidden
744 739 out = [ i for i in user_ns
745 740 if not i.startswith('_') \
746 741 and not (i in internal_ns or i in user_ns_hidden) ]
747 742
748 743 typelist = parameter_s.split()
749 744 if typelist:
750 745 typeset = set(typelist)
751 746 out = [i for i in out if type(i).__name__ in typeset]
752 747
753 748 out.sort()
754 749 return out
755 750
756 751 def magic_who(self, parameter_s=''):
757 752 """Print all interactive variables, with some minimal formatting.
758 753
759 754 If any arguments are given, only variables whose type matches one of
760 755 these are printed. For example:
761 756
762 757 %who function str
763 758
764 759 will only list functions and strings, excluding all other types of
765 760 variables. To find the proper type names, simply use type(var) at a
766 761 command line to see how python prints type names. For example:
767 762
768 763 In [1]: type('hello')\\
769 764 Out[1]: <type 'str'>
770 765
771 766 indicates that the type name for strings is 'str'.
772 767
773 768 %who always excludes executed names loaded through your configuration
774 769 file and things which are internal to IPython.
775 770
776 771 This is deliberate, as typically you may load many modules and the
777 772 purpose of %who is to show you only what you've manually defined."""
778 773
779 774 varlist = self.magic_who_ls(parameter_s)
780 775 if not varlist:
781 776 if parameter_s:
782 777 print 'No variables match your requested type.'
783 778 else:
784 779 print 'Interactive namespace is empty.'
785 780 return
786 781
787 782 # if we have variables, move on...
788 783 count = 0
789 784 for i in varlist:
790 785 print i+'\t',
791 786 count += 1
792 787 if count > 8:
793 788 count = 0
794 789 print
795 790 print
796 791
797 792 def magic_whos(self, parameter_s=''):
798 793 """Like %who, but gives some extra information about each variable.
799 794
800 795 The same type filtering of %who can be applied here.
801 796
802 797 For all variables, the type is printed. Additionally it prints:
803 798
804 799 - For {},[],(): their length.
805 800
806 801 - For numpy and Numeric arrays, a summary with shape, number of
807 802 elements, typecode and size in memory.
808 803
809 804 - Everything else: a string representation, snipping their middle if
810 805 too long."""
811 806
812 807 varnames = self.magic_who_ls(parameter_s)
813 808 if not varnames:
814 809 if parameter_s:
815 810 print 'No variables match your requested type.'
816 811 else:
817 812 print 'Interactive namespace is empty.'
818 813 return
819 814
820 815 # if we have variables, move on...
821 816
822 817 # for these types, show len() instead of data:
823 818 seq_types = [types.DictType,types.ListType,types.TupleType]
824 819
825 820 # for numpy/Numeric arrays, display summary info
826 821 try:
827 822 import numpy
828 823 except ImportError:
829 824 ndarray_type = None
830 825 else:
831 826 ndarray_type = numpy.ndarray.__name__
832 827 try:
833 828 import Numeric
834 829 except ImportError:
835 830 array_type = None
836 831 else:
837 832 array_type = Numeric.ArrayType.__name__
838 833
839 834 # Find all variable names and types so we can figure out column sizes
840 835 def get_vars(i):
841 836 return self.shell.user_ns[i]
842 837
843 838 # some types are well known and can be shorter
844 839 abbrevs = {'IPython.core.macro.Macro' : 'Macro'}
845 840 def type_name(v):
846 841 tn = type(v).__name__
847 842 return abbrevs.get(tn,tn)
848 843
849 844 varlist = map(get_vars,varnames)
850 845
851 846 typelist = []
852 847 for vv in varlist:
853 848 tt = type_name(vv)
854 849
855 850 if tt=='instance':
856 851 typelist.append( abbrevs.get(str(vv.__class__),
857 852 str(vv.__class__)))
858 853 else:
859 854 typelist.append(tt)
860 855
861 856 # column labels and # of spaces as separator
862 857 varlabel = 'Variable'
863 858 typelabel = 'Type'
864 859 datalabel = 'Data/Info'
865 860 colsep = 3
866 861 # variable format strings
867 862 vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)"
868 863 vfmt_short = '$vstr[:25]<...>$vstr[-25:]'
869 864 aformat = "%s: %s elems, type `%s`, %s bytes"
870 865 # find the size of the columns to format the output nicely
871 866 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
872 867 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
873 868 # table header
874 869 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
875 870 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
876 871 # and the table itself
877 872 kb = 1024
878 873 Mb = 1048576 # kb**2
879 874 for vname,var,vtype in zip(varnames,varlist,typelist):
880 875 print itpl(vformat),
881 876 if vtype in seq_types:
882 877 print len(var)
883 878 elif vtype in [array_type,ndarray_type]:
884 879 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
885 880 if vtype==ndarray_type:
886 881 # numpy
887 882 vsize = var.size
888 883 vbytes = vsize*var.itemsize
889 884 vdtype = var.dtype
890 885 else:
891 886 # Numeric
892 887 vsize = Numeric.size(var)
893 888 vbytes = vsize*var.itemsize()
894 889 vdtype = var.typecode()
895 890
896 891 if vbytes < 100000:
897 892 print aformat % (vshape,vsize,vdtype,vbytes)
898 893 else:
899 894 print aformat % (vshape,vsize,vdtype,vbytes),
900 895 if vbytes < Mb:
901 896 print '(%s kb)' % (vbytes/kb,)
902 897 else:
903 898 print '(%s Mb)' % (vbytes/Mb,)
904 899 else:
905 900 try:
906 901 vstr = str(var)
907 902 except UnicodeEncodeError:
908 903 vstr = unicode(var).encode(sys.getdefaultencoding(),
909 904 'backslashreplace')
910 905 vstr = vstr.replace('\n','\\n')
911 906 if len(vstr) < 50:
912 907 print vstr
913 908 else:
914 909 printpl(vfmt_short)
915 910
916 911 def magic_reset(self, parameter_s=''):
917 912 """Resets the namespace by removing all names defined by the user.
918 913
919 914 Input/Output history are left around in case you need them.
920 915
921 916 Parameters
922 917 ----------
923 918 -y : force reset without asking for confirmation.
924 919
925 920 Examples
926 921 --------
927 922 In [6]: a = 1
928 923
929 924 In [7]: a
930 925 Out[7]: 1
931 926
932 927 In [8]: 'a' in _ip.user_ns
933 928 Out[8]: True
934 929
935 930 In [9]: %reset -f
936 931
937 932 In [10]: 'a' in _ip.user_ns
938 933 Out[10]: False
939 934 """
940 935
941 936 if parameter_s == '-f':
942 937 ans = True
943 938 else:
944 939 ans = self.shell.ask_yes_no(
945 940 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ")
946 941 if not ans:
947 942 print 'Nothing done.'
948 943 return
949 944 user_ns = self.shell.user_ns
950 945 for i in self.magic_who_ls():
951 946 del(user_ns[i])
952 947
953 948 # Also flush the private list of module references kept for script
954 949 # execution protection
955 950 self.shell.clear_main_mod_cache()
956 951
957 952 def magic_reset_selective(self, parameter_s=''):
958 953 """Resets the namespace by removing names defined by the user.
959 954
960 955 Input/Output history are left around in case you need them.
961 956
962 957 %reset_selective [-f] regex
963 958
964 959 No action is taken if regex is not included
965 960
966 961 Options
967 962 -f : force reset without asking for confirmation.
968 963
969 964 Examples
970 965 --------
971 966
972 967 We first fully reset the namespace so your output looks identical to
973 968 this example for pedagogical reasons; in practice you do not need a
974 969 full reset.
975 970
976 971 In [1]: %reset -f
977 972
978 973 Now, with a clean namespace we can make a few variables and use
979 974 %reset_selective to only delete names that match our regexp:
980 975
981 976 In [2]: a=1; b=2; c=3; b1m=4; b2m=5; b3m=6; b4m=7; b2s=8
982 977
983 978 In [3]: who_ls
984 979 Out[3]: ['a', 'b', 'b1m', 'b2m', 'b2s', 'b3m', 'b4m', 'c']
985 980
986 981 In [4]: %reset_selective -f b[2-3]m
987 982
988 983 In [5]: who_ls
989 984 Out[5]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']
990 985
991 986 In [6]: %reset_selective -f d
992 987
993 988 In [7]: who_ls
994 989 Out[7]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']
995 990
996 991 In [8]: %reset_selective -f c
997 992
998 993 In [9]: who_ls
999 994 Out[9]: ['a', 'b', 'b1m', 'b2s', 'b4m']
1000 995
1001 996 In [10]: %reset_selective -f b
1002 997
1003 998 In [11]: who_ls
1004 999 Out[11]: ['a']
1005 1000 """
1006 1001
1007 1002 opts, regex = self.parse_options(parameter_s,'f')
1008 1003
1009 1004 if opts.has_key('f'):
1010 1005 ans = True
1011 1006 else:
1012 1007 ans = self.shell.ask_yes_no(
1013 1008 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ")
1014 1009 if not ans:
1015 1010 print 'Nothing done.'
1016 1011 return
1017 1012 user_ns = self.shell.user_ns
1018 1013 if not regex:
1019 1014 print 'No regex pattern specified. Nothing done.'
1020 1015 return
1021 1016 else:
1022 1017 try:
1023 1018 m = re.compile(regex)
1024 1019 except TypeError:
1025 1020 raise TypeError('regex must be a string or compiled pattern')
1026 1021 for i in self.magic_who_ls():
1027 1022 if m.search(i):
1028 1023 del(user_ns[i])
1029 1024
1030 1025 def magic_logstart(self,parameter_s=''):
1031 1026 """Start logging anywhere in a session.
1032 1027
1033 1028 %logstart [-o|-r|-t] [log_name [log_mode]]
1034 1029
1035 1030 If no name is given, it defaults to a file named 'ipython_log.py' in your
1036 1031 current directory, in 'rotate' mode (see below).
1037 1032
1038 1033 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
1039 1034 history up to that point and then continues logging.
1040 1035
1041 1036 %logstart takes a second optional parameter: logging mode. This can be one
1042 1037 of (note that the modes are given unquoted):\\
1043 1038 append: well, that says it.\\
1044 1039 backup: rename (if exists) to name~ and start name.\\
1045 1040 global: single logfile in your home dir, appended to.\\
1046 1041 over : overwrite existing log.\\
1047 1042 rotate: create rotating logs name.1~, name.2~, etc.
1048 1043
1049 1044 Options:
1050 1045
1051 1046 -o: log also IPython's output. In this mode, all commands which
1052 1047 generate an Out[NN] prompt are recorded to the logfile, right after
1053 1048 their corresponding input line. The output lines are always
1054 1049 prepended with a '#[Out]# ' marker, so that the log remains valid
1055 1050 Python code.
1056 1051
1057 1052 Since this marker is always the same, filtering only the output from
1058 1053 a log is very easy, using for example a simple awk call:
1059 1054
1060 1055 awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py
1061 1056
1062 1057 -r: log 'raw' input. Normally, IPython's logs contain the processed
1063 1058 input, so that user lines are logged in their final form, converted
1064 1059 into valid Python. For example, %Exit is logged as
1065 1060 '_ip.magic("Exit"). If the -r flag is given, all input is logged
1066 1061 exactly as typed, with no transformations applied.
1067 1062
1068 1063 -t: put timestamps before each input line logged (these are put in
1069 1064 comments)."""
1070 1065
1071 1066 opts,par = self.parse_options(parameter_s,'ort')
1072 1067 log_output = 'o' in opts
1073 1068 log_raw_input = 'r' in opts
1074 1069 timestamp = 't' in opts
1075 1070
1076 1071 logger = self.shell.logger
1077 1072
1078 1073 # if no args are given, the defaults set in the logger constructor by
1079 1074 # ipytohn remain valid
1080 1075 if par:
1081 1076 try:
1082 1077 logfname,logmode = par.split()
1083 1078 except:
1084 1079 logfname = par
1085 1080 logmode = 'backup'
1086 1081 else:
1087 1082 logfname = logger.logfname
1088 1083 logmode = logger.logmode
1089 1084 # put logfname into rc struct as if it had been called on the command
1090 1085 # line, so it ends up saved in the log header Save it in case we need
1091 1086 # to restore it...
1092 1087 old_logfile = self.shell.logfile
1093 1088 if logfname:
1094 1089 logfname = os.path.expanduser(logfname)
1095 1090 self.shell.logfile = logfname
1096 1091
1097 1092 loghead = '# IPython log file\n\n'
1098 1093 try:
1099 1094 started = logger.logstart(logfname,loghead,logmode,
1100 1095 log_output,timestamp,log_raw_input)
1101 1096 except:
1102 1097 self.shell.logfile = old_logfile
1103 1098 warn("Couldn't start log: %s" % sys.exc_info()[1])
1104 1099 else:
1105 1100 # log input history up to this point, optionally interleaving
1106 1101 # output if requested
1107 1102
1108 1103 if timestamp:
1109 1104 # disable timestamping for the previous history, since we've
1110 1105 # lost those already (no time machine here).
1111 1106 logger.timestamp = False
1112 1107
1113 1108 if log_raw_input:
1114 1109 input_hist = self.shell.input_hist_raw
1115 1110 else:
1116 1111 input_hist = self.shell.input_hist
1117 1112
1118 1113 if log_output:
1119 1114 log_write = logger.log_write
1120 1115 output_hist = self.shell.output_hist
1121 1116 for n in range(1,len(input_hist)-1):
1122 1117 log_write(input_hist[n].rstrip())
1123 1118 if n in output_hist:
1124 1119 log_write(repr(output_hist[n]),'output')
1125 1120 else:
1126 1121 logger.log_write(input_hist[1:])
1127 1122 if timestamp:
1128 1123 # re-enable timestamping
1129 1124 logger.timestamp = True
1130 1125
1131 1126 print ('Activating auto-logging. '
1132 1127 'Current session state plus future input saved.')
1133 1128 logger.logstate()
1134 1129
1135 1130 def magic_logstop(self,parameter_s=''):
1136 1131 """Fully stop logging and close log file.
1137 1132
1138 1133 In order to start logging again, a new %logstart call needs to be made,
1139 1134 possibly (though not necessarily) with a new filename, mode and other
1140 1135 options."""
1141 1136 self.logger.logstop()
1142 1137
1143 1138 def magic_logoff(self,parameter_s=''):
1144 1139 """Temporarily stop logging.
1145 1140
1146 1141 You must have previously started logging."""
1147 1142 self.shell.logger.switch_log(0)
1148 1143
1149 1144 def magic_logon(self,parameter_s=''):
1150 1145 """Restart logging.
1151 1146
1152 1147 This function is for restarting logging which you've temporarily
1153 1148 stopped with %logoff. For starting logging for the first time, you
1154 1149 must use the %logstart function, which allows you to specify an
1155 1150 optional log filename."""
1156 1151
1157 1152 self.shell.logger.switch_log(1)
1158 1153
1159 1154 def magic_logstate(self,parameter_s=''):
1160 1155 """Print the status of the logging system."""
1161 1156
1162 1157 self.shell.logger.logstate()
1163 1158
1164 1159 def magic_pdb(self, parameter_s=''):
1165 1160 """Control the automatic calling of the pdb interactive debugger.
1166 1161
1167 1162 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
1168 1163 argument it works as a toggle.
1169 1164
1170 1165 When an exception is triggered, IPython can optionally call the
1171 1166 interactive pdb debugger after the traceback printout. %pdb toggles
1172 1167 this feature on and off.
1173 1168
1174 1169 The initial state of this feature is set in your ipythonrc
1175 1170 configuration file (the variable is called 'pdb').
1176 1171
1177 1172 If you want to just activate the debugger AFTER an exception has fired,
1178 1173 without having to type '%pdb on' and rerunning your code, you can use
1179 1174 the %debug magic."""
1180 1175
1181 1176 par = parameter_s.strip().lower()
1182 1177
1183 1178 if par:
1184 1179 try:
1185 1180 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
1186 1181 except KeyError:
1187 1182 print ('Incorrect argument. Use on/1, off/0, '
1188 1183 'or nothing for a toggle.')
1189 1184 return
1190 1185 else:
1191 1186 # toggle
1192 1187 new_pdb = not self.shell.call_pdb
1193 1188
1194 1189 # set on the shell
1195 1190 self.shell.call_pdb = new_pdb
1196 1191 print 'Automatic pdb calling has been turned',on_off(new_pdb)
1197 1192
1198 1193 def magic_debug(self, parameter_s=''):
1199 1194 """Activate the interactive debugger in post-mortem mode.
1200 1195
1201 1196 If an exception has just occurred, this lets you inspect its stack
1202 1197 frames interactively. Note that this will always work only on the last
1203 1198 traceback that occurred, so you must call this quickly after an
1204 1199 exception that you wish to inspect has fired, because if another one
1205 1200 occurs, it clobbers the previous one.
1206 1201
1207 1202 If you want IPython to automatically do this on every exception, see
1208 1203 the %pdb magic for more details.
1209 1204 """
1210 1205 self.shell.debugger(force=True)
1211 1206
1212 1207 @testdec.skip_doctest
1213 1208 def magic_prun(self, parameter_s ='',user_mode=1,
1214 1209 opts=None,arg_lst=None,prog_ns=None):
1215 1210
1216 1211 """Run a statement through the python code profiler.
1217 1212
1218 1213 Usage:
1219 1214 %prun [options] statement
1220 1215
1221 1216 The given statement (which doesn't require quote marks) is run via the
1222 1217 python profiler in a manner similar to the profile.run() function.
1223 1218 Namespaces are internally managed to work correctly; profile.run
1224 1219 cannot be used in IPython because it makes certain assumptions about
1225 1220 namespaces which do not hold under IPython.
1226 1221
1227 1222 Options:
1228 1223
1229 1224 -l <limit>: you can place restrictions on what or how much of the
1230 1225 profile gets printed. The limit value can be:
1231 1226
1232 1227 * A string: only information for function names containing this string
1233 1228 is printed.
1234 1229
1235 1230 * An integer: only these many lines are printed.
1236 1231
1237 1232 * A float (between 0 and 1): this fraction of the report is printed
1238 1233 (for example, use a limit of 0.4 to see the topmost 40% only).
1239 1234
1240 1235 You can combine several limits with repeated use of the option. For
1241 1236 example, '-l __init__ -l 5' will print only the topmost 5 lines of
1242 1237 information about class constructors.
1243 1238
1244 1239 -r: return the pstats.Stats object generated by the profiling. This
1245 1240 object has all the information about the profile in it, and you can
1246 1241 later use it for further analysis or in other functions.
1247 1242
1248 1243 -s <key>: sort profile by given key. You can provide more than one key
1249 1244 by using the option several times: '-s key1 -s key2 -s key3...'. The
1250 1245 default sorting key is 'time'.
1251 1246
1252 1247 The following is copied verbatim from the profile documentation
1253 1248 referenced below:
1254 1249
1255 1250 When more than one key is provided, additional keys are used as
1256 1251 secondary criteria when the there is equality in all keys selected
1257 1252 before them.
1258 1253
1259 1254 Abbreviations can be used for any key names, as long as the
1260 1255 abbreviation is unambiguous. The following are the keys currently
1261 1256 defined:
1262 1257
1263 1258 Valid Arg Meaning
1264 1259 "calls" call count
1265 1260 "cumulative" cumulative time
1266 1261 "file" file name
1267 1262 "module" file name
1268 1263 "pcalls" primitive call count
1269 1264 "line" line number
1270 1265 "name" function name
1271 1266 "nfl" name/file/line
1272 1267 "stdname" standard name
1273 1268 "time" internal time
1274 1269
1275 1270 Note that all sorts on statistics are in descending order (placing
1276 1271 most time consuming items first), where as name, file, and line number
1277 1272 searches are in ascending order (i.e., alphabetical). The subtle
1278 1273 distinction between "nfl" and "stdname" is that the standard name is a
1279 1274 sort of the name as printed, which means that the embedded line
1280 1275 numbers get compared in an odd way. For example, lines 3, 20, and 40
1281 1276 would (if the file names were the same) appear in the string order
1282 1277 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
1283 1278 line numbers. In fact, sort_stats("nfl") is the same as
1284 1279 sort_stats("name", "file", "line").
1285 1280
1286 1281 -T <filename>: save profile results as shown on screen to a text
1287 1282 file. The profile is still shown on screen.
1288 1283
1289 1284 -D <filename>: save (via dump_stats) profile statistics to given
1290 1285 filename. This data is in a format understod by the pstats module, and
1291 1286 is generated by a call to the dump_stats() method of profile
1292 1287 objects. The profile is still shown on screen.
1293 1288
1294 1289 If you want to run complete programs under the profiler's control, use
1295 1290 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
1296 1291 contains profiler specific options as described here.
1297 1292
1298 1293 You can read the complete documentation for the profile module with::
1299 1294
1300 1295 In [1]: import profile; profile.help()
1301 1296 """
1302 1297
1303 1298 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
1304 1299 # protect user quote marks
1305 1300 parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'")
1306 1301
1307 1302 if user_mode: # regular user call
1308 1303 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:',
1309 1304 list_all=1)
1310 1305 namespace = self.shell.user_ns
1311 1306 else: # called to run a program by %run -p
1312 1307 try:
1313 1308 filename = get_py_filename(arg_lst[0])
1314 1309 except IOError,msg:
1315 1310 error(msg)
1316 1311 return
1317 1312
1318 1313 arg_str = 'execfile(filename,prog_ns)'
1319 1314 namespace = locals()
1320 1315
1321 1316 opts.merge(opts_def)
1322 1317
1323 1318 prof = profile.Profile()
1324 1319 try:
1325 1320 prof = prof.runctx(arg_str,namespace,namespace)
1326 1321 sys_exit = ''
1327 1322 except SystemExit:
1328 1323 sys_exit = """*** SystemExit exception caught in code being profiled."""
1329 1324
1330 1325 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
1331 1326
1332 1327 lims = opts.l
1333 1328 if lims:
1334 1329 lims = [] # rebuild lims with ints/floats/strings
1335 1330 for lim in opts.l:
1336 1331 try:
1337 1332 lims.append(int(lim))
1338 1333 except ValueError:
1339 1334 try:
1340 1335 lims.append(float(lim))
1341 1336 except ValueError:
1342 1337 lims.append(lim)
1343 1338
1344 1339 # Trap output.
1345 1340 stdout_trap = StringIO()
1346 1341
1347 1342 if hasattr(stats,'stream'):
1348 1343 # In newer versions of python, the stats object has a 'stream'
1349 1344 # attribute to write into.
1350 1345 stats.stream = stdout_trap
1351 1346 stats.print_stats(*lims)
1352 1347 else:
1353 1348 # For older versions, we manually redirect stdout during printing
1354 1349 sys_stdout = sys.stdout
1355 1350 try:
1356 1351 sys.stdout = stdout_trap
1357 1352 stats.print_stats(*lims)
1358 1353 finally:
1359 1354 sys.stdout = sys_stdout
1360 1355
1361 1356 output = stdout_trap.getvalue()
1362 1357 output = output.rstrip()
1363 1358
1364 1359 page.page(output)
1365 1360 print sys_exit,
1366 1361
1367 1362 dump_file = opts.D[0]
1368 1363 text_file = opts.T[0]
1369 1364 if dump_file:
1370 1365 prof.dump_stats(dump_file)
1371 1366 print '\n*** Profile stats marshalled to file',\
1372 1367 `dump_file`+'.',sys_exit
1373 1368 if text_file:
1374 1369 pfile = file(text_file,'w')
1375 1370 pfile.write(output)
1376 1371 pfile.close()
1377 1372 print '\n*** Profile printout saved to text file',\
1378 1373 `text_file`+'.',sys_exit
1379 1374
1380 1375 if opts.has_key('r'):
1381 1376 return stats
1382 1377 else:
1383 1378 return None
1384 1379
1385 1380 @testdec.skip_doctest
1386 1381 def magic_run(self, parameter_s ='',runner=None,
1387 1382 file_finder=get_py_filename):
1388 1383 """Run the named file inside IPython as a program.
1389 1384
1390 1385 Usage:\\
1391 1386 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
1392 1387
1393 1388 Parameters after the filename are passed as command-line arguments to
1394 1389 the program (put in sys.argv). Then, control returns to IPython's
1395 1390 prompt.
1396 1391
1397 1392 This is similar to running at a system prompt:\\
1398 1393 $ python file args\\
1399 1394 but with the advantage of giving you IPython's tracebacks, and of
1400 1395 loading all variables into your interactive namespace for further use
1401 1396 (unless -p is used, see below).
1402 1397
1403 1398 The file is executed in a namespace initially consisting only of
1404 1399 __name__=='__main__' and sys.argv constructed as indicated. It thus
1405 1400 sees its environment as if it were being run as a stand-alone program
1406 1401 (except for sharing global objects such as previously imported
1407 1402 modules). But after execution, the IPython interactive namespace gets
1408 1403 updated with all variables defined in the program (except for __name__
1409 1404 and sys.argv). This allows for very convenient loading of code for
1410 1405 interactive work, while giving each program a 'clean sheet' to run in.
1411 1406
1412 1407 Options:
1413 1408
1414 1409 -n: __name__ is NOT set to '__main__', but to the running file's name
1415 1410 without extension (as python does under import). This allows running
1416 1411 scripts and reloading the definitions in them without calling code
1417 1412 protected by an ' if __name__ == "__main__" ' clause.
1418 1413
1419 1414 -i: run the file in IPython's namespace instead of an empty one. This
1420 1415 is useful if you are experimenting with code written in a text editor
1421 1416 which depends on variables defined interactively.
1422 1417
1423 1418 -e: ignore sys.exit() calls or SystemExit exceptions in the script
1424 1419 being run. This is particularly useful if IPython is being used to
1425 1420 run unittests, which always exit with a sys.exit() call. In such
1426 1421 cases you are interested in the output of the test results, not in
1427 1422 seeing a traceback of the unittest module.
1428 1423
1429 1424 -t: print timing information at the end of the run. IPython will give
1430 1425 you an estimated CPU time consumption for your script, which under
1431 1426 Unix uses the resource module to avoid the wraparound problems of
1432 1427 time.clock(). Under Unix, an estimate of time spent on system tasks
1433 1428 is also given (for Windows platforms this is reported as 0.0).
1434 1429
1435 1430 If -t is given, an additional -N<N> option can be given, where <N>
1436 1431 must be an integer indicating how many times you want the script to
1437 1432 run. The final timing report will include total and per run results.
1438 1433
1439 1434 For example (testing the script uniq_stable.py):
1440 1435
1441 1436 In [1]: run -t uniq_stable
1442 1437
1443 1438 IPython CPU timings (estimated):\\
1444 1439 User : 0.19597 s.\\
1445 1440 System: 0.0 s.\\
1446 1441
1447 1442 In [2]: run -t -N5 uniq_stable
1448 1443
1449 1444 IPython CPU timings (estimated):\\
1450 1445 Total runs performed: 5\\
1451 1446 Times : Total Per run\\
1452 1447 User : 0.910862 s, 0.1821724 s.\\
1453 1448 System: 0.0 s, 0.0 s.
1454 1449
1455 1450 -d: run your program under the control of pdb, the Python debugger.
1456 1451 This allows you to execute your program step by step, watch variables,
1457 1452 etc. Internally, what IPython does is similar to calling:
1458 1453
1459 1454 pdb.run('execfile("YOURFILENAME")')
1460 1455
1461 1456 with a breakpoint set on line 1 of your file. You can change the line
1462 1457 number for this automatic breakpoint to be <N> by using the -bN option
1463 1458 (where N must be an integer). For example:
1464 1459
1465 1460 %run -d -b40 myscript
1466 1461
1467 1462 will set the first breakpoint at line 40 in myscript.py. Note that
1468 1463 the first breakpoint must be set on a line which actually does
1469 1464 something (not a comment or docstring) for it to stop execution.
1470 1465
1471 1466 When the pdb debugger starts, you will see a (Pdb) prompt. You must
1472 1467 first enter 'c' (without qoutes) to start execution up to the first
1473 1468 breakpoint.
1474 1469
1475 1470 Entering 'help' gives information about the use of the debugger. You
1476 1471 can easily see pdb's full documentation with "import pdb;pdb.help()"
1477 1472 at a prompt.
1478 1473
1479 1474 -p: run program under the control of the Python profiler module (which
1480 1475 prints a detailed report of execution times, function calls, etc).
1481 1476
1482 1477 You can pass other options after -p which affect the behavior of the
1483 1478 profiler itself. See the docs for %prun for details.
1484 1479
1485 1480 In this mode, the program's variables do NOT propagate back to the
1486 1481 IPython interactive namespace (because they remain in the namespace
1487 1482 where the profiler executes them).
1488 1483
1489 1484 Internally this triggers a call to %prun, see its documentation for
1490 1485 details on the options available specifically for profiling.
1491 1486
1492 1487 There is one special usage for which the text above doesn't apply:
1493 1488 if the filename ends with .ipy, the file is run as ipython script,
1494 1489 just as if the commands were written on IPython prompt.
1495 1490 """
1496 1491
1497 1492 # get arguments and set sys.argv for program to be run.
1498 1493 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e',
1499 1494 mode='list',list_all=1)
1500 1495
1501 1496 try:
1502 1497 filename = file_finder(arg_lst[0])
1503 1498 except IndexError:
1504 1499 warn('you must provide at least a filename.')
1505 1500 print '\n%run:\n',oinspect.getdoc(self.magic_run)
1506 1501 return
1507 1502 except IOError,msg:
1508 1503 error(msg)
1509 1504 return
1510 1505
1511 1506 if filename.lower().endswith('.ipy'):
1512 1507 self.shell.safe_execfile_ipy(filename)
1513 1508 return
1514 1509
1515 1510 # Control the response to exit() calls made by the script being run
1516 1511 exit_ignore = opts.has_key('e')
1517 1512
1518 1513 # Make sure that the running script gets a proper sys.argv as if it
1519 1514 # were run from a system shell.
1520 1515 save_argv = sys.argv # save it for later restoring
1521 1516 sys.argv = [filename]+ arg_lst[1:] # put in the proper filename
1522 1517
1523 1518 if opts.has_key('i'):
1524 1519 # Run in user's interactive namespace
1525 1520 prog_ns = self.shell.user_ns
1526 1521 __name__save = self.shell.user_ns['__name__']
1527 1522 prog_ns['__name__'] = '__main__'
1528 1523 main_mod = self.shell.new_main_mod(prog_ns)
1529 1524 else:
1530 1525 # Run in a fresh, empty namespace
1531 1526 if opts.has_key('n'):
1532 1527 name = os.path.splitext(os.path.basename(filename))[0]
1533 1528 else:
1534 1529 name = '__main__'
1535 1530
1536 1531 main_mod = self.shell.new_main_mod()
1537 1532 prog_ns = main_mod.__dict__
1538 1533 prog_ns['__name__'] = name
1539 1534
1540 1535 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
1541 1536 # set the __file__ global in the script's namespace
1542 1537 prog_ns['__file__'] = filename
1543 1538
1544 1539 # pickle fix. See interactiveshell for an explanation. But we need to make sure
1545 1540 # that, if we overwrite __main__, we replace it at the end
1546 1541 main_mod_name = prog_ns['__name__']
1547 1542
1548 1543 if main_mod_name == '__main__':
1549 1544 restore_main = sys.modules['__main__']
1550 1545 else:
1551 1546 restore_main = False
1552 1547
1553 1548 # This needs to be undone at the end to prevent holding references to
1554 1549 # every single object ever created.
1555 1550 sys.modules[main_mod_name] = main_mod
1556 1551
1557 1552 stats = None
1558 1553 try:
1559 1554 self.shell.savehist()
1560 1555
1561 1556 if opts.has_key('p'):
1562 1557 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1563 1558 else:
1564 1559 if opts.has_key('d'):
1565 1560 deb = debugger.Pdb(self.shell.colors)
1566 1561 # reset Breakpoint state, which is moronically kept
1567 1562 # in a class
1568 1563 bdb.Breakpoint.next = 1
1569 1564 bdb.Breakpoint.bplist = {}
1570 1565 bdb.Breakpoint.bpbynumber = [None]
1571 1566 # Set an initial breakpoint to stop execution
1572 1567 maxtries = 10
1573 1568 bp = int(opts.get('b',[1])[0])
1574 1569 checkline = deb.checkline(filename,bp)
1575 1570 if not checkline:
1576 1571 for bp in range(bp+1,bp+maxtries+1):
1577 1572 if deb.checkline(filename,bp):
1578 1573 break
1579 1574 else:
1580 1575 msg = ("\nI failed to find a valid line to set "
1581 1576 "a breakpoint\n"
1582 1577 "after trying up to line: %s.\n"
1583 1578 "Please set a valid breakpoint manually "
1584 1579 "with the -b option." % bp)
1585 1580 error(msg)
1586 1581 return
1587 1582 # if we find a good linenumber, set the breakpoint
1588 1583 deb.do_break('%s:%s' % (filename,bp))
1589 1584 # Start file run
1590 1585 print "NOTE: Enter 'c' at the",
1591 1586 print "%s prompt to start your script." % deb.prompt
1592 1587 try:
1593 1588 deb.run('execfile("%s")' % filename,prog_ns)
1594 1589
1595 1590 except:
1596 1591 etype, value, tb = sys.exc_info()
1597 1592 # Skip three frames in the traceback: the %run one,
1598 1593 # one inside bdb.py, and the command-line typed by the
1599 1594 # user (run by exec in pdb itself).
1600 1595 self.shell.InteractiveTB(etype,value,tb,tb_offset=3)
1601 1596 else:
1602 1597 if runner is None:
1603 1598 runner = self.shell.safe_execfile
1604 1599 if opts.has_key('t'):
1605 1600 # timed execution
1606 1601 try:
1607 1602 nruns = int(opts['N'][0])
1608 1603 if nruns < 1:
1609 1604 error('Number of runs must be >=1')
1610 1605 return
1611 1606 except (KeyError):
1612 1607 nruns = 1
1613 1608 if nruns == 1:
1614 1609 t0 = clock2()
1615 1610 runner(filename,prog_ns,prog_ns,
1616 1611 exit_ignore=exit_ignore)
1617 1612 t1 = clock2()
1618 1613 t_usr = t1[0]-t0[0]
1619 1614 t_sys = t1[1]-t0[1]
1620 1615 print "\nIPython CPU timings (estimated):"
1621 1616 print " User : %10s s." % t_usr
1622 1617 print " System: %10s s." % t_sys
1623 1618 else:
1624 1619 runs = range(nruns)
1625 1620 t0 = clock2()
1626 1621 for nr in runs:
1627 1622 runner(filename,prog_ns,prog_ns,
1628 1623 exit_ignore=exit_ignore)
1629 1624 t1 = clock2()
1630 1625 t_usr = t1[0]-t0[0]
1631 1626 t_sys = t1[1]-t0[1]
1632 1627 print "\nIPython CPU timings (estimated):"
1633 1628 print "Total runs performed:",nruns
1634 1629 print " Times : %10s %10s" % ('Total','Per run')
1635 1630 print " User : %10s s, %10s s." % (t_usr,t_usr/nruns)
1636 1631 print " System: %10s s, %10s s." % (t_sys,t_sys/nruns)
1637 1632
1638 1633 else:
1639 1634 # regular execution
1640 1635 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1641 1636
1642 1637 if opts.has_key('i'):
1643 1638 self.shell.user_ns['__name__'] = __name__save
1644 1639 else:
1645 1640 # The shell MUST hold a reference to prog_ns so after %run
1646 1641 # exits, the python deletion mechanism doesn't zero it out
1647 1642 # (leaving dangling references).
1648 1643 self.shell.cache_main_mod(prog_ns,filename)
1649 1644 # update IPython interactive namespace
1650 1645
1651 1646 # Some forms of read errors on the file may mean the
1652 1647 # __name__ key was never set; using pop we don't have to
1653 1648 # worry about a possible KeyError.
1654 1649 prog_ns.pop('__name__', None)
1655 1650
1656 1651 self.shell.user_ns.update(prog_ns)
1657 1652 finally:
1658 1653 # It's a bit of a mystery why, but __builtins__ can change from
1659 1654 # being a module to becoming a dict missing some key data after
1660 1655 # %run. As best I can see, this is NOT something IPython is doing
1661 1656 # at all, and similar problems have been reported before:
1662 1657 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
1663 1658 # Since this seems to be done by the interpreter itself, the best
1664 1659 # we can do is to at least restore __builtins__ for the user on
1665 1660 # exit.
1666 1661 self.shell.user_ns['__builtins__'] = __builtin__
1667 1662
1668 1663 # Ensure key global structures are restored
1669 1664 sys.argv = save_argv
1670 1665 if restore_main:
1671 1666 sys.modules['__main__'] = restore_main
1672 1667 else:
1673 1668 # Remove from sys.modules the reference to main_mod we'd
1674 1669 # added. Otherwise it will trap references to objects
1675 1670 # contained therein.
1676 1671 del sys.modules[main_mod_name]
1677 1672
1678 1673 self.shell.reloadhist()
1679 1674
1680 1675 return stats
1681 1676
1682 1677 @testdec.skip_doctest
1683 1678 def magic_timeit(self, parameter_s =''):
1684 1679 """Time execution of a Python statement or expression
1685 1680
1686 1681 Usage:\\
1687 1682 %timeit [-n<N> -r<R> [-t|-c]] statement
1688 1683
1689 1684 Time execution of a Python statement or expression using the timeit
1690 1685 module.
1691 1686
1692 1687 Options:
1693 1688 -n<N>: execute the given statement <N> times in a loop. If this value
1694 1689 is not given, a fitting value is chosen.
1695 1690
1696 1691 -r<R>: repeat the loop iteration <R> times and take the best result.
1697 1692 Default: 3
1698 1693
1699 1694 -t: use time.time to measure the time, which is the default on Unix.
1700 1695 This function measures wall time.
1701 1696
1702 1697 -c: use time.clock to measure the time, which is the default on
1703 1698 Windows and measures wall time. On Unix, resource.getrusage is used
1704 1699 instead and returns the CPU user time.
1705 1700
1706 1701 -p<P>: use a precision of <P> digits to display the timing result.
1707 1702 Default: 3
1708 1703
1709 1704
1710 1705 Examples:
1711 1706
1712 1707 In [1]: %timeit pass
1713 1708 10000000 loops, best of 3: 53.3 ns per loop
1714 1709
1715 1710 In [2]: u = None
1716 1711
1717 1712 In [3]: %timeit u is None
1718 1713 10000000 loops, best of 3: 184 ns per loop
1719 1714
1720 1715 In [4]: %timeit -r 4 u == None
1721 1716 1000000 loops, best of 4: 242 ns per loop
1722 1717
1723 1718 In [5]: import time
1724 1719
1725 1720 In [6]: %timeit -n1 time.sleep(2)
1726 1721 1 loops, best of 3: 2 s per loop
1727 1722
1728 1723
1729 1724 The times reported by %timeit will be slightly higher than those
1730 1725 reported by the timeit.py script when variables are accessed. This is
1731 1726 due to the fact that %timeit executes the statement in the namespace
1732 1727 of the shell, compared with timeit.py, which uses a single setup
1733 1728 statement to import function or create variables. Generally, the bias
1734 1729 does not matter as long as results from timeit.py are not mixed with
1735 1730 those from %timeit."""
1736 1731
1737 1732 import timeit
1738 1733 import math
1739 1734
1740 1735 # XXX: Unfortunately the unicode 'micro' symbol can cause problems in
1741 1736 # certain terminals. Until we figure out a robust way of
1742 1737 # auto-detecting if the terminal can deal with it, use plain 'us' for
1743 1738 # microseconds. I am really NOT happy about disabling the proper
1744 1739 # 'micro' prefix, but crashing is worse... If anyone knows what the
1745 1740 # right solution for this is, I'm all ears...
1746 1741 #
1747 1742 # Note: using
1748 1743 #
1749 1744 # s = u'\xb5'
1750 1745 # s.encode(sys.getdefaultencoding())
1751 1746 #
1752 1747 # is not sufficient, as I've seen terminals where that fails but
1753 1748 # print s
1754 1749 #
1755 1750 # succeeds
1756 1751 #
1757 1752 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
1758 1753
1759 1754 #units = [u"s", u"ms",u'\xb5',"ns"]
1760 1755 units = [u"s", u"ms",u'us',"ns"]
1761 1756
1762 1757 scaling = [1, 1e3, 1e6, 1e9]
1763 1758
1764 1759 opts, stmt = self.parse_options(parameter_s,'n:r:tcp:',
1765 1760 posix=False)
1766 1761 if stmt == "":
1767 1762 return
1768 1763 timefunc = timeit.default_timer
1769 1764 number = int(getattr(opts, "n", 0))
1770 1765 repeat = int(getattr(opts, "r", timeit.default_repeat))
1771 1766 precision = int(getattr(opts, "p", 3))
1772 1767 if hasattr(opts, "t"):
1773 1768 timefunc = time.time
1774 1769 if hasattr(opts, "c"):
1775 1770 timefunc = clock
1776 1771
1777 1772 timer = timeit.Timer(timer=timefunc)
1778 1773 # this code has tight coupling to the inner workings of timeit.Timer,
1779 1774 # but is there a better way to achieve that the code stmt has access
1780 1775 # to the shell namespace?
1781 1776
1782 1777 src = timeit.template % {'stmt': timeit.reindent(stmt, 8),
1783 1778 'setup': "pass"}
1784 1779 # Track compilation time so it can be reported if too long
1785 1780 # Minimum time above which compilation time will be reported
1786 1781 tc_min = 0.1
1787 1782
1788 1783 t0 = clock()
1789 1784 code = compile(src, "<magic-timeit>", "exec")
1790 1785 tc = clock()-t0
1791 1786
1792 1787 ns = {}
1793 1788 exec code in self.shell.user_ns, ns
1794 1789 timer.inner = ns["inner"]
1795 1790
1796 1791 if number == 0:
1797 1792 # determine number so that 0.2 <= total time < 2.0
1798 1793 number = 1
1799 1794 for i in range(1, 10):
1800 1795 if timer.timeit(number) >= 0.2:
1801 1796 break
1802 1797 number *= 10
1803 1798
1804 1799 best = min(timer.repeat(repeat, number)) / number
1805 1800
1806 1801 if best > 0.0 and best < 1000.0:
1807 1802 order = min(-int(math.floor(math.log10(best)) // 3), 3)
1808 1803 elif best >= 1000.0:
1809 1804 order = 0
1810 1805 else:
1811 1806 order = 3
1812 1807 print u"%d loops, best of %d: %.*g %s per loop" % (number, repeat,
1813 1808 precision,
1814 1809 best * scaling[order],
1815 1810 units[order])
1816 1811 if tc > tc_min:
1817 1812 print "Compiler time: %.2f s" % tc
1818 1813
1819 1814 @testdec.skip_doctest
1820 1815 def magic_time(self,parameter_s = ''):
1821 1816 """Time execution of a Python statement or expression.
1822 1817
1823 1818 The CPU and wall clock times are printed, and the value of the
1824 1819 expression (if any) is returned. Note that under Win32, system time
1825 1820 is always reported as 0, since it can not be measured.
1826 1821
1827 1822 This function provides very basic timing functionality. In Python
1828 1823 2.3, the timeit module offers more control and sophistication, so this
1829 1824 could be rewritten to use it (patches welcome).
1830 1825
1831 1826 Some examples:
1832 1827
1833 1828 In [1]: time 2**128
1834 1829 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1835 1830 Wall time: 0.00
1836 1831 Out[1]: 340282366920938463463374607431768211456L
1837 1832
1838 1833 In [2]: n = 1000000
1839 1834
1840 1835 In [3]: time sum(range(n))
1841 1836 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1842 1837 Wall time: 1.37
1843 1838 Out[3]: 499999500000L
1844 1839
1845 1840 In [4]: time print 'hello world'
1846 1841 hello world
1847 1842 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1848 1843 Wall time: 0.00
1849 1844
1850 1845 Note that the time needed by Python to compile the given expression
1851 1846 will be reported if it is more than 0.1s. In this example, the
1852 1847 actual exponentiation is done by Python at compilation time, so while
1853 1848 the expression can take a noticeable amount of time to compute, that
1854 1849 time is purely due to the compilation:
1855 1850
1856 1851 In [5]: time 3**9999;
1857 1852 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1858 1853 Wall time: 0.00 s
1859 1854
1860 1855 In [6]: time 3**999999;
1861 1856 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1862 1857 Wall time: 0.00 s
1863 1858 Compiler : 0.78 s
1864 1859 """
1865 1860
1866 1861 # fail immediately if the given expression can't be compiled
1867 1862
1868 1863 expr = self.shell.prefilter(parameter_s,False)
1869 1864
1870 1865 # Minimum time above which compilation time will be reported
1871 1866 tc_min = 0.1
1872 1867
1873 1868 try:
1874 1869 mode = 'eval'
1875 1870 t0 = clock()
1876 1871 code = compile(expr,'<timed eval>',mode)
1877 1872 tc = clock()-t0
1878 1873 except SyntaxError:
1879 1874 mode = 'exec'
1880 1875 t0 = clock()
1881 1876 code = compile(expr,'<timed exec>',mode)
1882 1877 tc = clock()-t0
1883 1878 # skew measurement as little as possible
1884 1879 glob = self.shell.user_ns
1885 1880 clk = clock2
1886 1881 wtime = time.time
1887 1882 # time execution
1888 1883 wall_st = wtime()
1889 1884 if mode=='eval':
1890 1885 st = clk()
1891 1886 out = eval(code,glob)
1892 1887 end = clk()
1893 1888 else:
1894 1889 st = clk()
1895 1890 exec code in glob
1896 1891 end = clk()
1897 1892 out = None
1898 1893 wall_end = wtime()
1899 1894 # Compute actual times and report
1900 1895 wall_time = wall_end-wall_st
1901 1896 cpu_user = end[0]-st[0]
1902 1897 cpu_sys = end[1]-st[1]
1903 1898 cpu_tot = cpu_user+cpu_sys
1904 1899 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
1905 1900 (cpu_user,cpu_sys,cpu_tot)
1906 1901 print "Wall time: %.2f s" % wall_time
1907 1902 if tc > tc_min:
1908 1903 print "Compiler : %.2f s" % tc
1909 1904 return out
1910 1905
1911 1906 @testdec.skip_doctest
1912 1907 def magic_macro(self,parameter_s = ''):
1913 1908 """Define a set of input lines as a macro for future re-execution.
1914 1909
1915 1910 Usage:\\
1916 1911 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
1917 1912
1918 1913 Options:
1919 1914
1920 1915 -r: use 'raw' input. By default, the 'processed' history is used,
1921 1916 so that magics are loaded in their transformed version to valid
1922 1917 Python. If this option is given, the raw input as typed as the
1923 1918 command line is used instead.
1924 1919
1925 1920 This will define a global variable called `name` which is a string
1926 1921 made of joining the slices and lines you specify (n1,n2,... numbers
1927 1922 above) from your input history into a single string. This variable
1928 1923 acts like an automatic function which re-executes those lines as if
1929 1924 you had typed them. You just type 'name' at the prompt and the code
1930 1925 executes.
1931 1926
1932 1927 The notation for indicating number ranges is: n1-n2 means 'use line
1933 1928 numbers n1,...n2' (the endpoint is included). That is, '5-7' means
1934 1929 using the lines numbered 5,6 and 7.
1935 1930
1936 1931 Note: as a 'hidden' feature, you can also use traditional python slice
1937 1932 notation, where N:M means numbers N through M-1.
1938 1933
1939 1934 For example, if your history contains (%hist prints it):
1940 1935
1941 1936 44: x=1
1942 1937 45: y=3
1943 1938 46: z=x+y
1944 1939 47: print x
1945 1940 48: a=5
1946 1941 49: print 'x',x,'y',y
1947 1942
1948 1943 you can create a macro with lines 44 through 47 (included) and line 49
1949 1944 called my_macro with:
1950 1945
1951 1946 In [55]: %macro my_macro 44-47 49
1952 1947
1953 1948 Now, typing `my_macro` (without quotes) will re-execute all this code
1954 1949 in one pass.
1955 1950
1956 1951 You don't need to give the line-numbers in order, and any given line
1957 1952 number can appear multiple times. You can assemble macros with any
1958 1953 lines from your input history in any order.
1959 1954
1960 1955 The macro is a simple object which holds its value in an attribute,
1961 1956 but IPython's display system checks for macros and executes them as
1962 1957 code instead of printing them when you type their name.
1963 1958
1964 1959 You can view a macro's contents by explicitly printing it with:
1965 1960
1966 1961 'print macro_name'.
1967 1962
1968 1963 For one-off cases which DON'T contain magic function calls in them you
1969 1964 can obtain similar results by explicitly executing slices from your
1970 1965 input history with:
1971 1966
1972 1967 In [60]: exec In[44:48]+In[49]"""
1973 1968
1974 1969 opts,args = self.parse_options(parameter_s,'r',mode='list')
1975 1970 if not args:
1976 1971 macs = [k for k,v in self.shell.user_ns.items() if isinstance(v, Macro)]
1977 1972 macs.sort()
1978 1973 return macs
1979 1974 if len(args) == 1:
1980 1975 raise UsageError(
1981 1976 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
1982 1977 name,ranges = args[0], args[1:]
1983 1978
1984 1979 #print 'rng',ranges # dbg
1985 1980 lines = self.extract_input_slices(ranges,opts.has_key('r'))
1986 1981 macro = Macro(lines)
1987 1982 self.shell.define_macro(name, macro)
1988 1983 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
1989 1984 print 'Macro contents:'
1990 1985 print macro,
1991 1986
1992 1987 def magic_save(self,parameter_s = ''):
1993 1988 """Save a set of lines to a given filename.
1994 1989
1995 1990 Usage:\\
1996 1991 %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...
1997 1992
1998 1993 Options:
1999 1994
2000 1995 -r: use 'raw' input. By default, the 'processed' history is used,
2001 1996 so that magics are loaded in their transformed version to valid
2002 1997 Python. If this option is given, the raw input as typed as the
2003 1998 command line is used instead.
2004 1999
2005 2000 This function uses the same syntax as %macro for line extraction, but
2006 2001 instead of creating a macro it saves the resulting string to the
2007 2002 filename you specify.
2008 2003
2009 2004 It adds a '.py' extension to the file if you don't do so yourself, and
2010 2005 it asks for confirmation before overwriting existing files."""
2011 2006
2012 2007 opts,args = self.parse_options(parameter_s,'r',mode='list')
2013 2008 fname,ranges = args[0], args[1:]
2014 2009 if not fname.endswith('.py'):
2015 2010 fname += '.py'
2016 2011 if os.path.isfile(fname):
2017 2012 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
2018 2013 if ans.lower() not in ['y','yes']:
2019 2014 print 'Operation cancelled.'
2020 2015 return
2021 2016 cmds = ''.join(self.extract_input_slices(ranges,opts.has_key('r')))
2022 2017 f = file(fname,'w')
2023 2018 f.write(cmds)
2024 2019 f.close()
2025 2020 print 'The following commands were written to file `%s`:' % fname
2026 2021 print cmds
2027 2022
2028 2023 def _edit_macro(self,mname,macro):
2029 2024 """open an editor with the macro data in a file"""
2030 2025 filename = self.shell.mktempfile(macro.value)
2031 2026 self.shell.hooks.editor(filename)
2032 2027
2033 2028 # and make a new macro object, to replace the old one
2034 2029 mfile = open(filename)
2035 2030 mvalue = mfile.read()
2036 2031 mfile.close()
2037 2032 self.shell.user_ns[mname] = Macro(mvalue)
2038 2033
2039 2034 def magic_ed(self,parameter_s=''):
2040 2035 """Alias to %edit."""
2041 2036 return self.magic_edit(parameter_s)
2042 2037
2043 2038 @testdec.skip_doctest
2044 2039 def magic_edit(self,parameter_s='',last_call=['','']):
2045 2040 """Bring up an editor and execute the resulting code.
2046 2041
2047 2042 Usage:
2048 2043 %edit [options] [args]
2049 2044
2050 2045 %edit runs IPython's editor hook. The default version of this hook is
2051 2046 set to call the __IPYTHON__.rc.editor command. This is read from your
2052 2047 environment variable $EDITOR. If this isn't found, it will default to
2053 2048 vi under Linux/Unix and to notepad under Windows. See the end of this
2054 2049 docstring for how to change the editor hook.
2055 2050
2056 2051 You can also set the value of this editor via the command line option
2057 2052 '-editor' or in your ipythonrc file. This is useful if you wish to use
2058 2053 specifically for IPython an editor different from your typical default
2059 2054 (and for Windows users who typically don't set environment variables).
2060 2055
2061 2056 This command allows you to conveniently edit multi-line code right in
2062 2057 your IPython session.
2063 2058
2064 2059 If called without arguments, %edit opens up an empty editor with a
2065 2060 temporary file and will execute the contents of this file when you
2066 2061 close it (don't forget to save it!).
2067 2062
2068 2063
2069 2064 Options:
2070 2065
2071 2066 -n <number>: open the editor at a specified line number. By default,
2072 2067 the IPython editor hook uses the unix syntax 'editor +N filename', but
2073 2068 you can configure this by providing your own modified hook if your
2074 2069 favorite editor supports line-number specifications with a different
2075 2070 syntax.
2076 2071
2077 2072 -p: this will call the editor with the same data as the previous time
2078 2073 it was used, regardless of how long ago (in your current session) it
2079 2074 was.
2080 2075
2081 2076 -r: use 'raw' input. This option only applies to input taken from the
2082 2077 user's history. By default, the 'processed' history is used, so that
2083 2078 magics are loaded in their transformed version to valid Python. If
2084 2079 this option is given, the raw input as typed as the command line is
2085 2080 used instead. When you exit the editor, it will be executed by
2086 2081 IPython's own processor.
2087 2082
2088 2083 -x: do not execute the edited code immediately upon exit. This is
2089 2084 mainly useful if you are editing programs which need to be called with
2090 2085 command line arguments, which you can then do using %run.
2091 2086
2092 2087
2093 2088 Arguments:
2094 2089
2095 2090 If arguments are given, the following possibilites exist:
2096 2091
2097 2092 - The arguments are numbers or pairs of colon-separated numbers (like
2098 2093 1 4:8 9). These are interpreted as lines of previous input to be
2099 2094 loaded into the editor. The syntax is the same of the %macro command.
2100 2095
2101 2096 - If the argument doesn't start with a number, it is evaluated as a
2102 2097 variable and its contents loaded into the editor. You can thus edit
2103 2098 any string which contains python code (including the result of
2104 2099 previous edits).
2105 2100
2106 2101 - If the argument is the name of an object (other than a string),
2107 2102 IPython will try to locate the file where it was defined and open the
2108 2103 editor at the point where it is defined. You can use `%edit function`
2109 2104 to load an editor exactly at the point where 'function' is defined,
2110 2105 edit it and have the file be executed automatically.
2111 2106
2112 2107 If the object is a macro (see %macro for details), this opens up your
2113 2108 specified editor with a temporary file containing the macro's data.
2114 2109 Upon exit, the macro is reloaded with the contents of the file.
2115 2110
2116 2111 Note: opening at an exact line is only supported under Unix, and some
2117 2112 editors (like kedit and gedit up to Gnome 2.8) do not understand the
2118 2113 '+NUMBER' parameter necessary for this feature. Good editors like
2119 2114 (X)Emacs, vi, jed, pico and joe all do.
2120 2115
2121 2116 - If the argument is not found as a variable, IPython will look for a
2122 2117 file with that name (adding .py if necessary) and load it into the
2123 2118 editor. It will execute its contents with execfile() when you exit,
2124 2119 loading any code in the file into your interactive namespace.
2125 2120
2126 2121 After executing your code, %edit will return as output the code you
2127 2122 typed in the editor (except when it was an existing file). This way
2128 2123 you can reload the code in further invocations of %edit as a variable,
2129 2124 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
2130 2125 the output.
2131 2126
2132 2127 Note that %edit is also available through the alias %ed.
2133 2128
2134 2129 This is an example of creating a simple function inside the editor and
2135 2130 then modifying it. First, start up the editor:
2136 2131
2137 2132 In [1]: ed
2138 2133 Editing... done. Executing edited code...
2139 2134 Out[1]: 'def foo():n print "foo() was defined in an editing session"n'
2140 2135
2141 2136 We can then call the function foo():
2142 2137
2143 2138 In [2]: foo()
2144 2139 foo() was defined in an editing session
2145 2140
2146 2141 Now we edit foo. IPython automatically loads the editor with the
2147 2142 (temporary) file where foo() was previously defined:
2148 2143
2149 2144 In [3]: ed foo
2150 2145 Editing... done. Executing edited code...
2151 2146
2152 2147 And if we call foo() again we get the modified version:
2153 2148
2154 2149 In [4]: foo()
2155 2150 foo() has now been changed!
2156 2151
2157 2152 Here is an example of how to edit a code snippet successive
2158 2153 times. First we call the editor:
2159 2154
2160 2155 In [5]: ed
2161 2156 Editing... done. Executing edited code...
2162 2157 hello
2163 2158 Out[5]: "print 'hello'n"
2164 2159
2165 2160 Now we call it again with the previous output (stored in _):
2166 2161
2167 2162 In [6]: ed _
2168 2163 Editing... done. Executing edited code...
2169 2164 hello world
2170 2165 Out[6]: "print 'hello world'n"
2171 2166
2172 2167 Now we call it with the output #8 (stored in _8, also as Out[8]):
2173 2168
2174 2169 In [7]: ed _8
2175 2170 Editing... done. Executing edited code...
2176 2171 hello again
2177 2172 Out[7]: "print 'hello again'n"
2178 2173
2179 2174
2180 2175 Changing the default editor hook:
2181 2176
2182 2177 If you wish to write your own editor hook, you can put it in a
2183 2178 configuration file which you load at startup time. The default hook
2184 2179 is defined in the IPython.core.hooks module, and you can use that as a
2185 2180 starting example for further modifications. That file also has
2186 2181 general instructions on how to set a new hook for use once you've
2187 2182 defined it."""
2188 2183
2189 2184 # FIXME: This function has become a convoluted mess. It needs a
2190 2185 # ground-up rewrite with clean, simple logic.
2191 2186
2192 2187 def make_filename(arg):
2193 2188 "Make a filename from the given args"
2194 2189 try:
2195 2190 filename = get_py_filename(arg)
2196 2191 except IOError:
2197 2192 if args.endswith('.py'):
2198 2193 filename = arg
2199 2194 else:
2200 2195 filename = None
2201 2196 return filename
2202 2197
2203 2198 # custom exceptions
2204 2199 class DataIsObject(Exception): pass
2205 2200
2206 2201 opts,args = self.parse_options(parameter_s,'prxn:')
2207 2202 # Set a few locals from the options for convenience:
2208 2203 opts_p = opts.has_key('p')
2209 2204 opts_r = opts.has_key('r')
2210 2205
2211 2206 # Default line number value
2212 2207 lineno = opts.get('n',None)
2213 2208
2214 2209 if opts_p:
2215 2210 args = '_%s' % last_call[0]
2216 2211 if not self.shell.user_ns.has_key(args):
2217 2212 args = last_call[1]
2218 2213
2219 2214 # use last_call to remember the state of the previous call, but don't
2220 2215 # let it be clobbered by successive '-p' calls.
2221 2216 try:
2222 2217 last_call[0] = self.shell.displayhook.prompt_count
2223 2218 if not opts_p:
2224 2219 last_call[1] = parameter_s
2225 2220 except:
2226 2221 pass
2227 2222
2228 2223 # by default this is done with temp files, except when the given
2229 2224 # arg is a filename
2230 2225 use_temp = 1
2231 2226
2232 2227 if re.match(r'\d',args):
2233 2228 # Mode where user specifies ranges of lines, like in %macro.
2234 2229 # This means that you can't edit files whose names begin with
2235 2230 # numbers this way. Tough.
2236 2231 ranges = args.split()
2237 2232 data = ''.join(self.extract_input_slices(ranges,opts_r))
2238 2233 elif args.endswith('.py'):
2239 2234 filename = make_filename(args)
2240 2235 data = ''
2241 2236 use_temp = 0
2242 2237 elif args:
2243 2238 try:
2244 2239 # Load the parameter given as a variable. If not a string,
2245 2240 # process it as an object instead (below)
2246 2241
2247 2242 #print '*** args',args,'type',type(args) # dbg
2248 2243 data = eval(args,self.shell.user_ns)
2249 2244 if not type(data) in StringTypes:
2250 2245 raise DataIsObject
2251 2246
2252 2247 except (NameError,SyntaxError):
2253 2248 # given argument is not a variable, try as a filename
2254 2249 filename = make_filename(args)
2255 2250 if filename is None:
2256 2251 warn("Argument given (%s) can't be found as a variable "
2257 2252 "or as a filename." % args)
2258 2253 return
2259 2254
2260 2255 data = ''
2261 2256 use_temp = 0
2262 2257 except DataIsObject:
2263 2258
2264 2259 # macros have a special edit function
2265 2260 if isinstance(data,Macro):
2266 2261 self._edit_macro(args,data)
2267 2262 return
2268 2263
2269 2264 # For objects, try to edit the file where they are defined
2270 2265 try:
2271 2266 filename = inspect.getabsfile(data)
2272 2267 if 'fakemodule' in filename.lower() and inspect.isclass(data):
2273 2268 # class created by %edit? Try to find source
2274 2269 # by looking for method definitions instead, the
2275 2270 # __module__ in those classes is FakeModule.
2276 2271 attrs = [getattr(data, aname) for aname in dir(data)]
2277 2272 for attr in attrs:
2278 2273 if not inspect.ismethod(attr):
2279 2274 continue
2280 2275 filename = inspect.getabsfile(attr)
2281 2276 if filename and 'fakemodule' not in filename.lower():
2282 2277 # change the attribute to be the edit target instead
2283 2278 data = attr
2284 2279 break
2285 2280
2286 2281 datafile = 1
2287 2282 except TypeError:
2288 2283 filename = make_filename(args)
2289 2284 datafile = 1
2290 2285 warn('Could not find file where `%s` is defined.\n'
2291 2286 'Opening a file named `%s`' % (args,filename))
2292 2287 # Now, make sure we can actually read the source (if it was in
2293 2288 # a temp file it's gone by now).
2294 2289 if datafile:
2295 2290 try:
2296 2291 if lineno is None:
2297 2292 lineno = inspect.getsourcelines(data)[1]
2298 2293 except IOError:
2299 2294 filename = make_filename(args)
2300 2295 if filename is None:
2301 2296 warn('The file `%s` where `%s` was defined cannot '
2302 2297 'be read.' % (filename,data))
2303 2298 return
2304 2299 use_temp = 0
2305 2300 else:
2306 2301 data = ''
2307 2302
2308 2303 if use_temp:
2309 2304 filename = self.shell.mktempfile(data)
2310 2305 print 'IPython will make a temporary file named:',filename
2311 2306
2312 2307 # do actual editing here
2313 2308 print 'Editing...',
2314 2309 sys.stdout.flush()
2315 2310 try:
2316 2311 # Quote filenames that may have spaces in them
2317 2312 if ' ' in filename:
2318 2313 filename = "%s" % filename
2319 2314 self.shell.hooks.editor(filename,lineno)
2320 2315 except TryNext:
2321 2316 warn('Could not open editor')
2322 2317 return
2323 2318
2324 2319 # XXX TODO: should this be generalized for all string vars?
2325 2320 # For now, this is special-cased to blocks created by cpaste
2326 2321 if args.strip() == 'pasted_block':
2327 2322 self.shell.user_ns['pasted_block'] = file_read(filename)
2328 2323
2329 2324 if opts.has_key('x'): # -x prevents actual execution
2330 2325 print
2331 2326 else:
2332 2327 print 'done. Executing edited code...'
2333 2328 if opts_r:
2334 self.shell.runlines(file_read(filename))
2329 self.shell.run_cell(file_read(filename))
2335 2330 else:
2336 2331 self.shell.safe_execfile(filename,self.shell.user_ns,
2337 2332 self.shell.user_ns)
2338 2333
2339 2334
2340 2335 if use_temp:
2341 2336 try:
2342 2337 return open(filename).read()
2343 2338 except IOError,msg:
2344 2339 if msg.filename == filename:
2345 2340 warn('File not found. Did you forget to save?')
2346 2341 return
2347 2342 else:
2348 2343 self.shell.showtraceback()
2349 2344
2350 2345 def magic_xmode(self,parameter_s = ''):
2351 2346 """Switch modes for the exception handlers.
2352 2347
2353 2348 Valid modes: Plain, Context and Verbose.
2354 2349
2355 2350 If called without arguments, acts as a toggle."""
2356 2351
2357 2352 def xmode_switch_err(name):
2358 2353 warn('Error changing %s exception modes.\n%s' %
2359 2354 (name,sys.exc_info()[1]))
2360 2355
2361 2356 shell = self.shell
2362 2357 new_mode = parameter_s.strip().capitalize()
2363 2358 try:
2364 2359 shell.InteractiveTB.set_mode(mode=new_mode)
2365 2360 print 'Exception reporting mode:',shell.InteractiveTB.mode
2366 2361 except:
2367 2362 xmode_switch_err('user')
2368 2363
2369 2364 def magic_colors(self,parameter_s = ''):
2370 2365 """Switch color scheme for prompts, info system and exception handlers.
2371 2366
2372 2367 Currently implemented schemes: NoColor, Linux, LightBG.
2373 2368
2374 2369 Color scheme names are not case-sensitive."""
2375 2370
2376 2371 def color_switch_err(name):
2377 2372 warn('Error changing %s color schemes.\n%s' %
2378 2373 (name,sys.exc_info()[1]))
2379 2374
2380 2375
2381 2376 new_scheme = parameter_s.strip()
2382 2377 if not new_scheme:
2383 2378 raise UsageError(
2384 2379 "%colors: you must specify a color scheme. See '%colors?'")
2385 2380 return
2386 2381 # local shortcut
2387 2382 shell = self.shell
2388 2383
2389 2384 import IPython.utils.rlineimpl as readline
2390 2385
2391 2386 if not readline.have_readline and sys.platform == "win32":
2392 2387 msg = """\
2393 2388 Proper color support under MS Windows requires the pyreadline library.
2394 2389 You can find it at:
2395 2390 http://ipython.scipy.org/moin/PyReadline/Intro
2396 2391 Gary's readline needs the ctypes module, from:
2397 2392 http://starship.python.net/crew/theller/ctypes
2398 2393 (Note that ctypes is already part of Python versions 2.5 and newer).
2399 2394
2400 2395 Defaulting color scheme to 'NoColor'"""
2401 2396 new_scheme = 'NoColor'
2402 2397 warn(msg)
2403 2398
2404 2399 # readline option is 0
2405 2400 if not shell.has_readline:
2406 2401 new_scheme = 'NoColor'
2407 2402
2408 2403 # Set prompt colors
2409 2404 try:
2410 2405 shell.displayhook.set_colors(new_scheme)
2411 2406 except:
2412 2407 color_switch_err('prompt')
2413 2408 else:
2414 2409 shell.colors = \
2415 2410 shell.displayhook.color_table.active_scheme_name
2416 2411 # Set exception colors
2417 2412 try:
2418 2413 shell.InteractiveTB.set_colors(scheme = new_scheme)
2419 2414 shell.SyntaxTB.set_colors(scheme = new_scheme)
2420 2415 except:
2421 2416 color_switch_err('exception')
2422 2417
2423 2418 # Set info (for 'object?') colors
2424 2419 if shell.color_info:
2425 2420 try:
2426 2421 shell.inspector.set_active_scheme(new_scheme)
2427 2422 except:
2428 2423 color_switch_err('object inspector')
2429 2424 else:
2430 2425 shell.inspector.set_active_scheme('NoColor')
2431 2426
2432 2427 def magic_Pprint(self, parameter_s=''):
2433 2428 """Toggle pretty printing on/off."""
2434 2429
2435 2430 self.shell.pprint = 1 - self.shell.pprint
2436 2431 print 'Pretty printing has been turned', \
2437 2432 ['OFF','ON'][self.shell.pprint]
2438 2433
2439 2434 def magic_Exit(self, parameter_s=''):
2440 2435 """Exit IPython."""
2441 2436
2442 2437 self.shell.ask_exit()
2443 2438
2444 2439 # Add aliases as magics so all common forms work: exit, quit, Exit, Quit.
2445 2440 magic_exit = magic_quit = magic_Quit = magic_Exit
2446 2441
2447 2442 #......................................................................
2448 2443 # Functions to implement unix shell-type things
2449 2444
2450 2445 @testdec.skip_doctest
2451 2446 def magic_alias(self, parameter_s = ''):
2452 2447 """Define an alias for a system command.
2453 2448
2454 2449 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
2455 2450
2456 2451 Then, typing 'alias_name params' will execute the system command 'cmd
2457 2452 params' (from your underlying operating system).
2458 2453
2459 2454 Aliases have lower precedence than magic functions and Python normal
2460 2455 variables, so if 'foo' is both a Python variable and an alias, the
2461 2456 alias can not be executed until 'del foo' removes the Python variable.
2462 2457
2463 2458 You can use the %l specifier in an alias definition to represent the
2464 2459 whole line when the alias is called. For example:
2465 2460
2466 2461 In [2]: alias bracket echo "Input in brackets: <%l>"
2467 2462 In [3]: bracket hello world
2468 2463 Input in brackets: <hello world>
2469 2464
2470 2465 You can also define aliases with parameters using %s specifiers (one
2471 2466 per parameter):
2472 2467
2473 2468 In [1]: alias parts echo first %s second %s
2474 2469 In [2]: %parts A B
2475 2470 first A second B
2476 2471 In [3]: %parts A
2477 2472 Incorrect number of arguments: 2 expected.
2478 2473 parts is an alias to: 'echo first %s second %s'
2479 2474
2480 2475 Note that %l and %s are mutually exclusive. You can only use one or
2481 2476 the other in your aliases.
2482 2477
2483 2478 Aliases expand Python variables just like system calls using ! or !!
2484 2479 do: all expressions prefixed with '$' get expanded. For details of
2485 2480 the semantic rules, see PEP-215:
2486 2481 http://www.python.org/peps/pep-0215.html. This is the library used by
2487 2482 IPython for variable expansion. If you want to access a true shell
2488 2483 variable, an extra $ is necessary to prevent its expansion by IPython:
2489 2484
2490 2485 In [6]: alias show echo
2491 2486 In [7]: PATH='A Python string'
2492 2487 In [8]: show $PATH
2493 2488 A Python string
2494 2489 In [9]: show $$PATH
2495 2490 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
2496 2491
2497 2492 You can use the alias facility to acess all of $PATH. See the %rehash
2498 2493 and %rehashx functions, which automatically create aliases for the
2499 2494 contents of your $PATH.
2500 2495
2501 2496 If called with no parameters, %alias prints the current alias table."""
2502 2497
2503 2498 par = parameter_s.strip()
2504 2499 if not par:
2505 2500 stored = self.db.get('stored_aliases', {} )
2506 2501 aliases = sorted(self.shell.alias_manager.aliases)
2507 2502 # for k, v in stored:
2508 2503 # atab.append(k, v[0])
2509 2504
2510 2505 print "Total number of aliases:", len(aliases)
2511 2506 sys.stdout.flush()
2512 2507 return aliases
2513 2508
2514 2509 # Now try to define a new one
2515 2510 try:
2516 2511 alias,cmd = par.split(None, 1)
2517 2512 except:
2518 2513 print oinspect.getdoc(self.magic_alias)
2519 2514 else:
2520 2515 self.shell.alias_manager.soft_define_alias(alias, cmd)
2521 2516 # end magic_alias
2522 2517
2523 2518 def magic_unalias(self, parameter_s = ''):
2524 2519 """Remove an alias"""
2525 2520
2526 2521 aname = parameter_s.strip()
2527 2522 self.shell.alias_manager.undefine_alias(aname)
2528 2523 stored = self.db.get('stored_aliases', {} )
2529 2524 if aname in stored:
2530 2525 print "Removing %stored alias",aname
2531 2526 del stored[aname]
2532 2527 self.db['stored_aliases'] = stored
2533 2528
2534 2529 def magic_rehashx(self, parameter_s = ''):
2535 2530 """Update the alias table with all executable files in $PATH.
2536 2531
2537 2532 This version explicitly checks that every entry in $PATH is a file
2538 2533 with execute access (os.X_OK), so it is much slower than %rehash.
2539 2534
2540 2535 Under Windows, it checks executability as a match agains a
2541 2536 '|'-separated string of extensions, stored in the IPython config
2542 2537 variable win_exec_ext. This defaults to 'exe|com|bat'.
2543 2538
2544 2539 This function also resets the root module cache of module completer,
2545 2540 used on slow filesystems.
2546 2541 """
2547 2542 from IPython.core.alias import InvalidAliasError
2548 2543
2549 2544 # for the benefit of module completer in ipy_completers.py
2550 2545 del self.db['rootmodules']
2551 2546
2552 2547 path = [os.path.abspath(os.path.expanduser(p)) for p in
2553 2548 os.environ.get('PATH','').split(os.pathsep)]
2554 2549 path = filter(os.path.isdir,path)
2555 2550
2556 2551 syscmdlist = []
2557 2552 # Now define isexec in a cross platform manner.
2558 2553 if os.name == 'posix':
2559 2554 isexec = lambda fname:os.path.isfile(fname) and \
2560 2555 os.access(fname,os.X_OK)
2561 2556 else:
2562 2557 try:
2563 2558 winext = os.environ['pathext'].replace(';','|').replace('.','')
2564 2559 except KeyError:
2565 2560 winext = 'exe|com|bat|py'
2566 2561 if 'py' not in winext:
2567 2562 winext += '|py'
2568 2563 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
2569 2564 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
2570 2565 savedir = os.getcwd()
2571 2566
2572 2567 # Now walk the paths looking for executables to alias.
2573 2568 try:
2574 2569 # write the whole loop for posix/Windows so we don't have an if in
2575 2570 # the innermost part
2576 2571 if os.name == 'posix':
2577 2572 for pdir in path:
2578 2573 os.chdir(pdir)
2579 2574 for ff in os.listdir(pdir):
2580 2575 if isexec(ff):
2581 2576 try:
2582 2577 # Removes dots from the name since ipython
2583 2578 # will assume names with dots to be python.
2584 2579 self.shell.alias_manager.define_alias(
2585 2580 ff.replace('.',''), ff)
2586 2581 except InvalidAliasError:
2587 2582 pass
2588 2583 else:
2589 2584 syscmdlist.append(ff)
2590 2585 else:
2591 2586 no_alias = self.shell.alias_manager.no_alias
2592 2587 for pdir in path:
2593 2588 os.chdir(pdir)
2594 2589 for ff in os.listdir(pdir):
2595 2590 base, ext = os.path.splitext(ff)
2596 2591 if isexec(ff) and base.lower() not in no_alias:
2597 2592 if ext.lower() == '.exe':
2598 2593 ff = base
2599 2594 try:
2600 2595 # Removes dots from the name since ipython
2601 2596 # will assume names with dots to be python.
2602 2597 self.shell.alias_manager.define_alias(
2603 2598 base.lower().replace('.',''), ff)
2604 2599 except InvalidAliasError:
2605 2600 pass
2606 2601 syscmdlist.append(ff)
2607 2602 db = self.db
2608 2603 db['syscmdlist'] = syscmdlist
2609 2604 finally:
2610 2605 os.chdir(savedir)
2611 2606
2612 2607 def magic_pwd(self, parameter_s = ''):
2613 2608 """Return the current working directory path."""
2614 2609 return os.getcwd()
2615 2610
2616 2611 def magic_cd(self, parameter_s=''):
2617 2612 """Change the current working directory.
2618 2613
2619 2614 This command automatically maintains an internal list of directories
2620 2615 you visit during your IPython session, in the variable _dh. The
2621 2616 command %dhist shows this history nicely formatted. You can also
2622 2617 do 'cd -<tab>' to see directory history conveniently.
2623 2618
2624 2619 Usage:
2625 2620
2626 2621 cd 'dir': changes to directory 'dir'.
2627 2622
2628 2623 cd -: changes to the last visited directory.
2629 2624
2630 2625 cd -<n>: changes to the n-th directory in the directory history.
2631 2626
2632 2627 cd --foo: change to directory that matches 'foo' in history
2633 2628
2634 2629 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
2635 2630 (note: cd <bookmark_name> is enough if there is no
2636 2631 directory <bookmark_name>, but a bookmark with the name exists.)
2637 2632 'cd -b <tab>' allows you to tab-complete bookmark names.
2638 2633
2639 2634 Options:
2640 2635
2641 2636 -q: quiet. Do not print the working directory after the cd command is
2642 2637 executed. By default IPython's cd command does print this directory,
2643 2638 since the default prompts do not display path information.
2644 2639
2645 2640 Note that !cd doesn't work for this purpose because the shell where
2646 2641 !command runs is immediately discarded after executing 'command'."""
2647 2642
2648 2643 parameter_s = parameter_s.strip()
2649 2644 #bkms = self.shell.persist.get("bookmarks",{})
2650 2645
2651 2646 oldcwd = os.getcwd()
2652 2647 numcd = re.match(r'(-)(\d+)$',parameter_s)
2653 2648 # jump in directory history by number
2654 2649 if numcd:
2655 2650 nn = int(numcd.group(2))
2656 2651 try:
2657 2652 ps = self.shell.user_ns['_dh'][nn]
2658 2653 except IndexError:
2659 2654 print 'The requested directory does not exist in history.'
2660 2655 return
2661 2656 else:
2662 2657 opts = {}
2663 2658 elif parameter_s.startswith('--'):
2664 2659 ps = None
2665 2660 fallback = None
2666 2661 pat = parameter_s[2:]
2667 2662 dh = self.shell.user_ns['_dh']
2668 2663 # first search only by basename (last component)
2669 2664 for ent in reversed(dh):
2670 2665 if pat in os.path.basename(ent) and os.path.isdir(ent):
2671 2666 ps = ent
2672 2667 break
2673 2668
2674 2669 if fallback is None and pat in ent and os.path.isdir(ent):
2675 2670 fallback = ent
2676 2671
2677 2672 # if we have no last part match, pick the first full path match
2678 2673 if ps is None:
2679 2674 ps = fallback
2680 2675
2681 2676 if ps is None:
2682 2677 print "No matching entry in directory history"
2683 2678 return
2684 2679 else:
2685 2680 opts = {}
2686 2681
2687 2682
2688 2683 else:
2689 2684 #turn all non-space-escaping backslashes to slashes,
2690 2685 # for c:\windows\directory\names\
2691 2686 parameter_s = re.sub(r'\\(?! )','/', parameter_s)
2692 2687 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
2693 2688 # jump to previous
2694 2689 if ps == '-':
2695 2690 try:
2696 2691 ps = self.shell.user_ns['_dh'][-2]
2697 2692 except IndexError:
2698 2693 raise UsageError('%cd -: No previous directory to change to.')
2699 2694 # jump to bookmark if needed
2700 2695 else:
2701 2696 if not os.path.isdir(ps) or opts.has_key('b'):
2702 2697 bkms = self.db.get('bookmarks', {})
2703 2698
2704 2699 if bkms.has_key(ps):
2705 2700 target = bkms[ps]
2706 2701 print '(bookmark:%s) -> %s' % (ps,target)
2707 2702 ps = target
2708 2703 else:
2709 2704 if opts.has_key('b'):
2710 2705 raise UsageError("Bookmark '%s' not found. "
2711 2706 "Use '%%bookmark -l' to see your bookmarks." % ps)
2712 2707
2713 2708 # at this point ps should point to the target dir
2714 2709 if ps:
2715 2710 try:
2716 2711 os.chdir(os.path.expanduser(ps))
2717 2712 if hasattr(self.shell, 'term_title') and self.shell.term_title:
2718 2713 set_term_title('IPython: ' + abbrev_cwd())
2719 2714 except OSError:
2720 2715 print sys.exc_info()[1]
2721 2716 else:
2722 2717 cwd = os.getcwd()
2723 2718 dhist = self.shell.user_ns['_dh']
2724 2719 if oldcwd != cwd:
2725 2720 dhist.append(cwd)
2726 2721 self.db['dhist'] = compress_dhist(dhist)[-100:]
2727 2722
2728 2723 else:
2729 2724 os.chdir(self.shell.home_dir)
2730 2725 if hasattr(self.shell, 'term_title') and self.shell.term_title:
2731 2726 set_term_title('IPython: ' + '~')
2732 2727 cwd = os.getcwd()
2733 2728 dhist = self.shell.user_ns['_dh']
2734 2729
2735 2730 if oldcwd != cwd:
2736 2731 dhist.append(cwd)
2737 2732 self.db['dhist'] = compress_dhist(dhist)[-100:]
2738 2733 if not 'q' in opts and self.shell.user_ns['_dh']:
2739 2734 print self.shell.user_ns['_dh'][-1]
2740 2735
2741 2736
2742 2737 def magic_env(self, parameter_s=''):
2743 2738 """List environment variables."""
2744 2739
2745 2740 return os.environ.data
2746 2741
2747 2742 def magic_pushd(self, parameter_s=''):
2748 2743 """Place the current dir on stack and change directory.
2749 2744
2750 2745 Usage:\\
2751 2746 %pushd ['dirname']
2752 2747 """
2753 2748
2754 2749 dir_s = self.shell.dir_stack
2755 2750 tgt = os.path.expanduser(parameter_s)
2756 2751 cwd = os.getcwd().replace(self.home_dir,'~')
2757 2752 if tgt:
2758 2753 self.magic_cd(parameter_s)
2759 2754 dir_s.insert(0,cwd)
2760 2755 return self.magic_dirs()
2761 2756
2762 2757 def magic_popd(self, parameter_s=''):
2763 2758 """Change to directory popped off the top of the stack.
2764 2759 """
2765 2760 if not self.shell.dir_stack:
2766 2761 raise UsageError("%popd on empty stack")
2767 2762 top = self.shell.dir_stack.pop(0)
2768 2763 self.magic_cd(top)
2769 2764 print "popd ->",top
2770 2765
2771 2766 def magic_dirs(self, parameter_s=''):
2772 2767 """Return the current directory stack."""
2773 2768
2774 2769 return self.shell.dir_stack
2775 2770
2776 2771 def magic_dhist(self, parameter_s=''):
2777 2772 """Print your history of visited directories.
2778 2773
2779 2774 %dhist -> print full history\\
2780 2775 %dhist n -> print last n entries only\\
2781 2776 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
2782 2777
2783 2778 This history is automatically maintained by the %cd command, and
2784 2779 always available as the global list variable _dh. You can use %cd -<n>
2785 2780 to go to directory number <n>.
2786 2781
2787 2782 Note that most of time, you should view directory history by entering
2788 2783 cd -<TAB>.
2789 2784
2790 2785 """
2791 2786
2792 2787 dh = self.shell.user_ns['_dh']
2793 2788 if parameter_s:
2794 2789 try:
2795 2790 args = map(int,parameter_s.split())
2796 2791 except:
2797 2792 self.arg_err(Magic.magic_dhist)
2798 2793 return
2799 2794 if len(args) == 1:
2800 2795 ini,fin = max(len(dh)-(args[0]),0),len(dh)
2801 2796 elif len(args) == 2:
2802 2797 ini,fin = args
2803 2798 else:
2804 2799 self.arg_err(Magic.magic_dhist)
2805 2800 return
2806 2801 else:
2807 2802 ini,fin = 0,len(dh)
2808 2803 nlprint(dh,
2809 2804 header = 'Directory history (kept in _dh)',
2810 2805 start=ini,stop=fin)
2811 2806
2812 2807 @testdec.skip_doctest
2813 2808 def magic_sc(self, parameter_s=''):
2814 2809 """Shell capture - execute a shell command and capture its output.
2815 2810
2816 2811 DEPRECATED. Suboptimal, retained for backwards compatibility.
2817 2812
2818 2813 You should use the form 'var = !command' instead. Example:
2819 2814
2820 2815 "%sc -l myfiles = ls ~" should now be written as
2821 2816
2822 2817 "myfiles = !ls ~"
2823 2818
2824 2819 myfiles.s, myfiles.l and myfiles.n still apply as documented
2825 2820 below.
2826 2821
2827 2822 --
2828 2823 %sc [options] varname=command
2829 2824
2830 2825 IPython will run the given command using commands.getoutput(), and
2831 2826 will then update the user's interactive namespace with a variable
2832 2827 called varname, containing the value of the call. Your command can
2833 2828 contain shell wildcards, pipes, etc.
2834 2829
2835 2830 The '=' sign in the syntax is mandatory, and the variable name you
2836 2831 supply must follow Python's standard conventions for valid names.
2837 2832
2838 2833 (A special format without variable name exists for internal use)
2839 2834
2840 2835 Options:
2841 2836
2842 2837 -l: list output. Split the output on newlines into a list before
2843 2838 assigning it to the given variable. By default the output is stored
2844 2839 as a single string.
2845 2840
2846 2841 -v: verbose. Print the contents of the variable.
2847 2842
2848 2843 In most cases you should not need to split as a list, because the
2849 2844 returned value is a special type of string which can automatically
2850 2845 provide its contents either as a list (split on newlines) or as a
2851 2846 space-separated string. These are convenient, respectively, either
2852 2847 for sequential processing or to be passed to a shell command.
2853 2848
2854 2849 For example:
2855 2850
2856 2851 # all-random
2857 2852
2858 2853 # Capture into variable a
2859 2854 In [1]: sc a=ls *py
2860 2855
2861 2856 # a is a string with embedded newlines
2862 2857 In [2]: a
2863 2858 Out[2]: 'setup.py\\nwin32_manual_post_install.py'
2864 2859
2865 2860 # which can be seen as a list:
2866 2861 In [3]: a.l
2867 2862 Out[3]: ['setup.py', 'win32_manual_post_install.py']
2868 2863
2869 2864 # or as a whitespace-separated string:
2870 2865 In [4]: a.s
2871 2866 Out[4]: 'setup.py win32_manual_post_install.py'
2872 2867
2873 2868 # a.s is useful to pass as a single command line:
2874 2869 In [5]: !wc -l $a.s
2875 2870 146 setup.py
2876 2871 130 win32_manual_post_install.py
2877 2872 276 total
2878 2873
2879 2874 # while the list form is useful to loop over:
2880 2875 In [6]: for f in a.l:
2881 2876 ...: !wc -l $f
2882 2877 ...:
2883 2878 146 setup.py
2884 2879 130 win32_manual_post_install.py
2885 2880
2886 2881 Similiarly, the lists returned by the -l option are also special, in
2887 2882 the sense that you can equally invoke the .s attribute on them to
2888 2883 automatically get a whitespace-separated string from their contents:
2889 2884
2890 2885 In [7]: sc -l b=ls *py
2891 2886
2892 2887 In [8]: b
2893 2888 Out[8]: ['setup.py', 'win32_manual_post_install.py']
2894 2889
2895 2890 In [9]: b.s
2896 2891 Out[9]: 'setup.py win32_manual_post_install.py'
2897 2892
2898 2893 In summary, both the lists and strings used for ouptut capture have
2899 2894 the following special attributes:
2900 2895
2901 2896 .l (or .list) : value as list.
2902 2897 .n (or .nlstr): value as newline-separated string.
2903 2898 .s (or .spstr): value as space-separated string.
2904 2899 """
2905 2900
2906 2901 opts,args = self.parse_options(parameter_s,'lv')
2907 2902 # Try to get a variable name and command to run
2908 2903 try:
2909 2904 # the variable name must be obtained from the parse_options
2910 2905 # output, which uses shlex.split to strip options out.
2911 2906 var,_ = args.split('=',1)
2912 2907 var = var.strip()
2913 2908 # But the the command has to be extracted from the original input
2914 2909 # parameter_s, not on what parse_options returns, to avoid the
2915 2910 # quote stripping which shlex.split performs on it.
2916 2911 _,cmd = parameter_s.split('=',1)
2917 2912 except ValueError:
2918 2913 var,cmd = '',''
2919 2914 # If all looks ok, proceed
2920 2915 split = 'l' in opts
2921 2916 out = self.shell.getoutput(cmd, split=split)
2922 2917 if opts.has_key('v'):
2923 2918 print '%s ==\n%s' % (var,pformat(out))
2924 2919 if var:
2925 2920 self.shell.user_ns.update({var:out})
2926 2921 else:
2927 2922 return out
2928 2923
2929 2924 def magic_sx(self, parameter_s=''):
2930 2925 """Shell execute - run a shell command and capture its output.
2931 2926
2932 2927 %sx command
2933 2928
2934 2929 IPython will run the given command using commands.getoutput(), and
2935 2930 return the result formatted as a list (split on '\\n'). Since the
2936 2931 output is _returned_, it will be stored in ipython's regular output
2937 2932 cache Out[N] and in the '_N' automatic variables.
2938 2933
2939 2934 Notes:
2940 2935
2941 2936 1) If an input line begins with '!!', then %sx is automatically
2942 2937 invoked. That is, while:
2943 2938 !ls
2944 2939 causes ipython to simply issue system('ls'), typing
2945 2940 !!ls
2946 2941 is a shorthand equivalent to:
2947 2942 %sx ls
2948 2943
2949 2944 2) %sx differs from %sc in that %sx automatically splits into a list,
2950 2945 like '%sc -l'. The reason for this is to make it as easy as possible
2951 2946 to process line-oriented shell output via further python commands.
2952 2947 %sc is meant to provide much finer control, but requires more
2953 2948 typing.
2954 2949
2955 2950 3) Just like %sc -l, this is a list with special attributes:
2956 2951
2957 2952 .l (or .list) : value as list.
2958 2953 .n (or .nlstr): value as newline-separated string.
2959 2954 .s (or .spstr): value as whitespace-separated string.
2960 2955
2961 2956 This is very useful when trying to use such lists as arguments to
2962 2957 system commands."""
2963 2958
2964 2959 if parameter_s:
2965 2960 return self.shell.getoutput(parameter_s)
2966 2961
2967 2962 def magic_r(self, parameter_s=''):
2968 2963 """Repeat previous input.
2969 2964
2970 2965 Note: Consider using the more powerfull %rep instead!
2971 2966
2972 2967 If given an argument, repeats the previous command which starts with
2973 2968 the same string, otherwise it just repeats the previous input.
2974 2969
2975 2970 Shell escaped commands (with ! as first character) are not recognized
2976 2971 by this system, only pure python code and magic commands.
2977 2972 """
2978 2973
2979 2974 start = parameter_s.strip()
2980 2975 esc_magic = ESC_MAGIC
2981 2976 # Identify magic commands even if automagic is on (which means
2982 2977 # the in-memory version is different from that typed by the user).
2983 2978 if self.shell.automagic:
2984 2979 start_magic = esc_magic+start
2985 2980 else:
2986 2981 start_magic = start
2987 2982 # Look through the input history in reverse
2988 2983 for n in range(len(self.shell.input_hist)-2,0,-1):
2989 2984 input = self.shell.input_hist[n]
2990 2985 # skip plain 'r' lines so we don't recurse to infinity
2991 2986 if input != '_ip.magic("r")\n' and \
2992 2987 (input.startswith(start) or input.startswith(start_magic)):
2993 2988 #print 'match',`input` # dbg
2994 2989 print 'Executing:',input,
2995 self.shell.runlines(input)
2990 self.shell.run_cell(input)
2996 2991 return
2997 2992 print 'No previous input matching `%s` found.' % start
2998 2993
2999 2994
3000 2995 def magic_bookmark(self, parameter_s=''):
3001 2996 """Manage IPython's bookmark system.
3002 2997
3003 2998 %bookmark <name> - set bookmark to current dir
3004 2999 %bookmark <name> <dir> - set bookmark to <dir>
3005 3000 %bookmark -l - list all bookmarks
3006 3001 %bookmark -d <name> - remove bookmark
3007 3002 %bookmark -r - remove all bookmarks
3008 3003
3009 3004 You can later on access a bookmarked folder with:
3010 3005 %cd -b <name>
3011 3006 or simply '%cd <name>' if there is no directory called <name> AND
3012 3007 there is such a bookmark defined.
3013 3008
3014 3009 Your bookmarks persist through IPython sessions, but they are
3015 3010 associated with each profile."""
3016 3011
3017 3012 opts,args = self.parse_options(parameter_s,'drl',mode='list')
3018 3013 if len(args) > 2:
3019 3014 raise UsageError("%bookmark: too many arguments")
3020 3015
3021 3016 bkms = self.db.get('bookmarks',{})
3022 3017
3023 3018 if opts.has_key('d'):
3024 3019 try:
3025 3020 todel = args[0]
3026 3021 except IndexError:
3027 3022 raise UsageError(
3028 3023 "%bookmark -d: must provide a bookmark to delete")
3029 3024 else:
3030 3025 try:
3031 3026 del bkms[todel]
3032 3027 except KeyError:
3033 3028 raise UsageError(
3034 3029 "%%bookmark -d: Can't delete bookmark '%s'" % todel)
3035 3030
3036 3031 elif opts.has_key('r'):
3037 3032 bkms = {}
3038 3033 elif opts.has_key('l'):
3039 3034 bks = bkms.keys()
3040 3035 bks.sort()
3041 3036 if bks:
3042 3037 size = max(map(len,bks))
3043 3038 else:
3044 3039 size = 0
3045 3040 fmt = '%-'+str(size)+'s -> %s'
3046 3041 print 'Current bookmarks:'
3047 3042 for bk in bks:
3048 3043 print fmt % (bk,bkms[bk])
3049 3044 else:
3050 3045 if not args:
3051 3046 raise UsageError("%bookmark: You must specify the bookmark name")
3052 3047 elif len(args)==1:
3053 3048 bkms[args[0]] = os.getcwd()
3054 3049 elif len(args)==2:
3055 3050 bkms[args[0]] = args[1]
3056 3051 self.db['bookmarks'] = bkms
3057 3052
3058 3053 def magic_pycat(self, parameter_s=''):
3059 3054 """Show a syntax-highlighted file through a pager.
3060 3055
3061 3056 This magic is similar to the cat utility, but it will assume the file
3062 3057 to be Python source and will show it with syntax highlighting. """
3063 3058
3064 3059 try:
3065 3060 filename = get_py_filename(parameter_s)
3066 3061 cont = file_read(filename)
3067 3062 except IOError:
3068 3063 try:
3069 3064 cont = eval(parameter_s,self.user_ns)
3070 3065 except NameError:
3071 3066 cont = None
3072 3067 if cont is None:
3073 3068 print "Error: no such file or variable"
3074 3069 return
3075 3070
3076 3071 page.page(self.shell.pycolorize(cont))
3077 3072
3078 3073 def _rerun_pasted(self):
3079 3074 """ Rerun a previously pasted command.
3080 3075 """
3081 3076 b = self.user_ns.get('pasted_block', None)
3082 3077 if b is None:
3083 3078 raise UsageError('No previous pasted block available')
3084 3079 print "Re-executing '%s...' (%d chars)"% (b.split('\n',1)[0], len(b))
3085 3080 exec b in self.user_ns
3086 3081
3087 3082 def _get_pasted_lines(self, sentinel):
3088 3083 """ Yield pasted lines until the user enters the given sentinel value.
3089 3084 """
3090 3085 from IPython.core import interactiveshell
3091 3086 print "Pasting code; enter '%s' alone on the line to stop." % sentinel
3092 3087 while True:
3093 3088 l = interactiveshell.raw_input_original(':')
3094 3089 if l == sentinel:
3095 3090 return
3096 3091 else:
3097 3092 yield l
3098 3093
3099 3094 def _strip_pasted_lines_for_code(self, raw_lines):
3100 3095 """ Strip non-code parts of a sequence of lines to return a block of
3101 3096 code.
3102 3097 """
3103 3098 # Regular expressions that declare text we strip from the input:
3104 3099 strip_re = [r'^\s*In \[\d+\]:', # IPython input prompt
3105 3100 r'^\s*(\s?>)+', # Python input prompt
3106 3101 r'^\s*\.{3,}', # Continuation prompts
3107 3102 r'^\++',
3108 3103 ]
3109 3104
3110 3105 strip_from_start = map(re.compile,strip_re)
3111 3106
3112 3107 lines = []
3113 3108 for l in raw_lines:
3114 3109 for pat in strip_from_start:
3115 3110 l = pat.sub('',l)
3116 3111 lines.append(l)
3117 3112
3118 3113 block = "\n".join(lines) + '\n'
3119 3114 #print "block:\n",block
3120 3115 return block
3121 3116
3122 3117 def _execute_block(self, block, par):
3123 3118 """ Execute a block, or store it in a variable, per the user's request.
3124 3119 """
3125 3120 if not par:
3126 3121 b = textwrap.dedent(block)
3127 3122 self.user_ns['pasted_block'] = b
3128 3123 exec b in self.user_ns
3129 3124 else:
3130 3125 self.user_ns[par] = SList(block.splitlines())
3131 3126 print "Block assigned to '%s'" % par
3132 3127
3133 3128 def magic_quickref(self,arg):
3134 3129 """ Show a quick reference sheet """
3135 3130 import IPython.core.usage
3136 3131 qr = IPython.core.usage.quick_reference + self.magic_magic('-brief')
3137 3132
3138 3133 page.page(qr)
3139 3134
3140 3135 def magic_doctest_mode(self,parameter_s=''):
3141 3136 """Toggle doctest mode on and off.
3142 3137
3143 3138 This mode is intended to make IPython behave as much as possible like a
3144 3139 plain Python shell, from the perspective of how its prompts, exceptions
3145 3140 and output look. This makes it easy to copy and paste parts of a
3146 3141 session into doctests. It does so by:
3147 3142
3148 3143 - Changing the prompts to the classic ``>>>`` ones.
3149 3144 - Changing the exception reporting mode to 'Plain'.
3150 3145 - Disabling pretty-printing of output.
3151 3146
3152 3147 Note that IPython also supports the pasting of code snippets that have
3153 3148 leading '>>>' and '...' prompts in them. This means that you can paste
3154 3149 doctests from files or docstrings (even if they have leading
3155 3150 whitespace), and the code will execute correctly. You can then use
3156 3151 '%history -t' to see the translated history; this will give you the
3157 3152 input after removal of all the leading prompts and whitespace, which
3158 3153 can be pasted back into an editor.
3159 3154
3160 3155 With these features, you can switch into this mode easily whenever you
3161 3156 need to do testing and changes to doctests, without having to leave
3162 3157 your existing IPython session.
3163 3158 """
3164 3159
3165 3160 from IPython.utils.ipstruct import Struct
3166 3161
3167 3162 # Shorthands
3168 3163 shell = self.shell
3169 3164 oc = shell.displayhook
3170 3165 meta = shell.meta
3171 3166 # dstore is a data store kept in the instance metadata bag to track any
3172 3167 # changes we make, so we can undo them later.
3173 3168 dstore = meta.setdefault('doctest_mode',Struct())
3174 3169 save_dstore = dstore.setdefault
3175 3170
3176 3171 # save a few values we'll need to recover later
3177 3172 mode = save_dstore('mode',False)
3178 3173 save_dstore('rc_pprint',shell.pprint)
3179 3174 save_dstore('xmode',shell.InteractiveTB.mode)
3180 3175 save_dstore('rc_separate_out',shell.separate_out)
3181 3176 save_dstore('rc_separate_out2',shell.separate_out2)
3182 3177 save_dstore('rc_prompts_pad_left',shell.prompts_pad_left)
3183 3178 save_dstore('rc_separate_in',shell.separate_in)
3184 3179
3185 3180 if mode == False:
3186 3181 # turn on
3187 3182 oc.prompt1.p_template = '>>> '
3188 3183 oc.prompt2.p_template = '... '
3189 3184 oc.prompt_out.p_template = ''
3190 3185
3191 3186 # Prompt separators like plain python
3192 3187 oc.input_sep = oc.prompt1.sep = ''
3193 3188 oc.output_sep = ''
3194 3189 oc.output_sep2 = ''
3195 3190
3196 3191 oc.prompt1.pad_left = oc.prompt2.pad_left = \
3197 3192 oc.prompt_out.pad_left = False
3198 3193
3199 3194 shell.pprint = False
3200 3195
3201 3196 shell.magic_xmode('Plain')
3202 3197 else:
3203 3198 # turn off
3204 3199 oc.prompt1.p_template = shell.prompt_in1
3205 3200 oc.prompt2.p_template = shell.prompt_in2
3206 3201 oc.prompt_out.p_template = shell.prompt_out
3207 3202
3208 3203 oc.input_sep = oc.prompt1.sep = dstore.rc_separate_in
3209 3204
3210 3205 oc.output_sep = dstore.rc_separate_out
3211 3206 oc.output_sep2 = dstore.rc_separate_out2
3212 3207
3213 3208 oc.prompt1.pad_left = oc.prompt2.pad_left = \
3214 3209 oc.prompt_out.pad_left = dstore.rc_prompts_pad_left
3215 3210
3216 3211 shell.pprint = dstore.rc_pprint
3217 3212
3218 3213 shell.magic_xmode(dstore.xmode)
3219 3214
3220 3215 # Store new mode and inform
3221 3216 dstore.mode = bool(1-int(mode))
3222 3217 mode_label = ['OFF','ON'][dstore.mode]
3223 3218 print 'Doctest mode is:', mode_label
3224 3219
3225 3220 def magic_gui(self, parameter_s=''):
3226 3221 """Enable or disable IPython GUI event loop integration.
3227 3222
3228 3223 %gui [GUINAME]
3229 3224
3230 3225 This magic replaces IPython's threaded shells that were activated
3231 3226 using the (pylab/wthread/etc.) command line flags. GUI toolkits
3232 3227 can now be enabled, disabled and swtiched at runtime and keyboard
3233 3228 interrupts should work without any problems. The following toolkits
3234 3229 are supported: wxPython, PyQt4, PyGTK, and Tk::
3235 3230
3236 3231 %gui wx # enable wxPython event loop integration
3237 3232 %gui qt4|qt # enable PyQt4 event loop integration
3238 3233 %gui gtk # enable PyGTK event loop integration
3239 3234 %gui tk # enable Tk event loop integration
3240 3235 %gui # disable all event loop integration
3241 3236
3242 3237 WARNING: after any of these has been called you can simply create
3243 3238 an application object, but DO NOT start the event loop yourself, as
3244 3239 we have already handled that.
3245 3240 """
3246 3241 from IPython.lib.inputhook import enable_gui
3247 3242 opts, arg = self.parse_options(parameter_s='')
3248 3243 if arg=='': arg = None
3249 3244 return enable_gui(arg)
3250 3245
3251 3246 def magic_load_ext(self, module_str):
3252 3247 """Load an IPython extension by its module name."""
3253 3248 return self.extension_manager.load_extension(module_str)
3254 3249
3255 3250 def magic_unload_ext(self, module_str):
3256 3251 """Unload an IPython extension by its module name."""
3257 3252 self.extension_manager.unload_extension(module_str)
3258 3253
3259 3254 def magic_reload_ext(self, module_str):
3260 3255 """Reload an IPython extension by its module name."""
3261 3256 self.extension_manager.reload_extension(module_str)
3262 3257
3263 3258 @testdec.skip_doctest
3264 3259 def magic_install_profiles(self, s):
3265 3260 """Install the default IPython profiles into the .ipython dir.
3266 3261
3267 3262 If the default profiles have already been installed, they will not
3268 3263 be overwritten. You can force overwriting them by using the ``-o``
3269 3264 option::
3270 3265
3271 3266 In [1]: %install_profiles -o
3272 3267 """
3273 3268 if '-o' in s:
3274 3269 overwrite = True
3275 3270 else:
3276 3271 overwrite = False
3277 3272 from IPython.config import profile
3278 3273 profile_dir = os.path.split(profile.__file__)[0]
3279 3274 ipython_dir = self.ipython_dir
3280 3275 files = os.listdir(profile_dir)
3281 3276
3282 3277 to_install = []
3283 3278 for f in files:
3284 3279 if f.startswith('ipython_config'):
3285 3280 src = os.path.join(profile_dir, f)
3286 3281 dst = os.path.join(ipython_dir, f)
3287 3282 if (not os.path.isfile(dst)) or overwrite:
3288 3283 to_install.append((f, src, dst))
3289 3284 if len(to_install)>0:
3290 3285 print "Installing profiles to: ", ipython_dir
3291 3286 for (f, src, dst) in to_install:
3292 3287 shutil.copy(src, dst)
3293 3288 print " %s" % f
3294 3289
3295 3290 def magic_install_default_config(self, s):
3296 3291 """Install IPython's default config file into the .ipython dir.
3297 3292
3298 3293 If the default config file (:file:`ipython_config.py`) is already
3299 3294 installed, it will not be overwritten. You can force overwriting
3300 3295 by using the ``-o`` option::
3301 3296
3302 3297 In [1]: %install_default_config
3303 3298 """
3304 3299 if '-o' in s:
3305 3300 overwrite = True
3306 3301 else:
3307 3302 overwrite = False
3308 3303 from IPython.config import default
3309 3304 config_dir = os.path.split(default.__file__)[0]
3310 3305 ipython_dir = self.ipython_dir
3311 3306 default_config_file_name = 'ipython_config.py'
3312 3307 src = os.path.join(config_dir, default_config_file_name)
3313 3308 dst = os.path.join(ipython_dir, default_config_file_name)
3314 3309 if (not os.path.isfile(dst)) or overwrite:
3315 3310 shutil.copy(src, dst)
3316 3311 print "Installing default config file: %s" % dst
3317 3312
3318 3313 # Pylab support: simple wrappers that activate pylab, load gui input
3319 3314 # handling and modify slightly %run
3320 3315
3321 3316 @testdec.skip_doctest
3322 3317 def _pylab_magic_run(self, parameter_s=''):
3323 3318 Magic.magic_run(self, parameter_s,
3324 3319 runner=mpl_runner(self.shell.safe_execfile))
3325 3320
3326 3321 _pylab_magic_run.__doc__ = magic_run.__doc__
3327 3322
3328 3323 @testdec.skip_doctest
3329 3324 def magic_pylab(self, s):
3330 3325 """Load numpy and matplotlib to work interactively.
3331 3326
3332 3327 %pylab [GUINAME]
3333 3328
3334 3329 This function lets you activate pylab (matplotlib, numpy and
3335 3330 interactive support) at any point during an IPython session.
3336 3331
3337 3332 It will import at the top level numpy as np, pyplot as plt, matplotlib,
3338 3333 pylab and mlab, as well as all names from numpy and pylab.
3339 3334
3340 3335 Parameters
3341 3336 ----------
3342 3337 guiname : optional
3343 3338 One of the valid arguments to the %gui magic ('qt', 'wx', 'gtk' or
3344 3339 'tk'). If given, the corresponding Matplotlib backend is used,
3345 3340 otherwise matplotlib's default (which you can override in your
3346 3341 matplotlib config file) is used.
3347 3342
3348 3343 Examples
3349 3344 --------
3350 3345 In this case, where the MPL default is TkAgg:
3351 3346 In [2]: %pylab
3352 3347
3353 3348 Welcome to pylab, a matplotlib-based Python environment.
3354 3349 Backend in use: TkAgg
3355 3350 For more information, type 'help(pylab)'.
3356 3351
3357 3352 But you can explicitly request a different backend:
3358 3353 In [3]: %pylab qt
3359 3354
3360 3355 Welcome to pylab, a matplotlib-based Python environment.
3361 3356 Backend in use: Qt4Agg
3362 3357 For more information, type 'help(pylab)'.
3363 3358 """
3364 3359 self.shell.enable_pylab(s)
3365 3360
3366 3361 def magic_tb(self, s):
3367 3362 """Print the last traceback with the currently active exception mode.
3368 3363
3369 3364 See %xmode for changing exception reporting modes."""
3370 3365 self.shell.showtraceback()
3371 3366
3372 3367 # end Magic
@@ -1,1014 +1,1000 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3 """
4 4 Prefiltering components.
5 5
6 6 Prefilters transform user input before it is exec'd by Python. These
7 7 transforms are used to implement additional syntax such as !ls and %magic.
8 8
9 9 Authors:
10 10
11 11 * Brian Granger
12 12 * Fernando Perez
13 13 * Dan Milstein
14 14 * Ville Vainio
15 15 """
16 16
17 17 #-----------------------------------------------------------------------------
18 18 # Copyright (C) 2008-2009 The IPython Development Team
19 19 #
20 20 # Distributed under the terms of the BSD License. The full license is in
21 21 # the file COPYING, distributed as part of this software.
22 22 #-----------------------------------------------------------------------------
23 23
24 24 #-----------------------------------------------------------------------------
25 25 # Imports
26 26 #-----------------------------------------------------------------------------
27 27
28 28 import __builtin__
29 29 import codeop
30 30 import re
31 31
32 32 from IPython.core.alias import AliasManager
33 33 from IPython.core.autocall import IPyAutocall
34 34 from IPython.config.configurable import Configurable
35 35 from IPython.core.splitinput import split_user_input
36 36 from IPython.core import page
37 37
38 38 from IPython.utils.traitlets import List, Int, Any, Str, CBool, Bool, Instance
39 39 import IPython.utils.io
40 40 from IPython.utils.text import make_quoted_expr
41 41 from IPython.utils.autoattr import auto_attr
42 42
43 43 #-----------------------------------------------------------------------------
44 44 # Global utilities, errors and constants
45 45 #-----------------------------------------------------------------------------
46 46
47 47 # Warning, these cannot be changed unless various regular expressions
48 48 # are updated in a number of places. Not great, but at least we told you.
49 49 ESC_SHELL = '!'
50 50 ESC_SH_CAP = '!!'
51 51 ESC_HELP = '?'
52 52 ESC_MAGIC = '%'
53 53 ESC_QUOTE = ','
54 54 ESC_QUOTE2 = ';'
55 55 ESC_PAREN = '/'
56 56
57 57
58 58 class PrefilterError(Exception):
59 59 pass
60 60
61 61
62 62 # RegExp to identify potential function names
63 63 re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$')
64 64
65 65 # RegExp to exclude strings with this start from autocalling. In
66 66 # particular, all binary operators should be excluded, so that if foo is
67 67 # callable, foo OP bar doesn't become foo(OP bar), which is invalid. The
68 68 # characters '!=()' don't need to be checked for, as the checkPythonChars
69 69 # routine explicitely does so, to catch direct calls and rebindings of
70 70 # existing names.
71 71
72 72 # Warning: the '-' HAS TO BE AT THE END of the first group, otherwise
73 73 # it affects the rest of the group in square brackets.
74 74 re_exclude_auto = re.compile(r'^[,&^\|\*/\+-]'
75 75 r'|^is |^not |^in |^and |^or ')
76 76
77 77 # try to catch also methods for stuff in lists/tuples/dicts: off
78 78 # (experimental). For this to work, the line_split regexp would need
79 79 # to be modified so it wouldn't break things at '['. That line is
80 80 # nasty enough that I shouldn't change it until I can test it _well_.
81 81 #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$')
82 82
83 83
84 84 # Handler Check Utilities
85 85 def is_shadowed(identifier, ip):
86 86 """Is the given identifier defined in one of the namespaces which shadow
87 87 the alias and magic namespaces? Note that an identifier is different
88 88 than ifun, because it can not contain a '.' character."""
89 89 # This is much safer than calling ofind, which can change state
90 90 return (identifier in ip.user_ns \
91 91 or identifier in ip.internal_ns \
92 92 or identifier in ip.ns_table['builtin'])
93 93
94 94
95 95 #-----------------------------------------------------------------------------
96 96 # The LineInfo class used throughout
97 97 #-----------------------------------------------------------------------------
98 98
99 99
100 100 class LineInfo(object):
101 101 """A single line of input and associated info.
102 102
103 103 Includes the following as properties:
104 104
105 105 line
106 106 The original, raw line
107 107
108 108 continue_prompt
109 109 Is this line a continuation in a sequence of multiline input?
110 110
111 111 pre
112 112 The initial esc character or whitespace.
113 113
114 114 pre_char
115 115 The escape character(s) in pre or the empty string if there isn't one.
116 116 Note that '!!' is a possible value for pre_char. Otherwise it will
117 117 always be a single character.
118 118
119 119 pre_whitespace
120 120 The leading whitespace from pre if it exists. If there is a pre_char,
121 121 this is just ''.
122 122
123 123 ifun
124 124 The 'function part', which is basically the maximal initial sequence
125 125 of valid python identifiers and the '.' character. This is what is
126 126 checked for alias and magic transformations, used for auto-calling,
127 127 etc.
128 128
129 129 the_rest
130 130 Everything else on the line.
131 131 """
132 132 def __init__(self, line, continue_prompt):
133 133 self.line = line
134 134 self.continue_prompt = continue_prompt
135 135 self.pre, self.ifun, self.the_rest = split_user_input(line)
136 136
137 137 self.pre_char = self.pre.strip()
138 138 if self.pre_char:
139 139 self.pre_whitespace = '' # No whitespace allowd before esc chars
140 140 else:
141 141 self.pre_whitespace = self.pre
142 142
143 143 self._oinfo = None
144 144
145 145 def ofind(self, ip):
146 146 """Do a full, attribute-walking lookup of the ifun in the various
147 147 namespaces for the given IPython InteractiveShell instance.
148 148
149 149 Return a dict with keys: found,obj,ospace,ismagic
150 150
151 151 Note: can cause state changes because of calling getattr, but should
152 152 only be run if autocall is on and if the line hasn't matched any
153 153 other, less dangerous handlers.
154 154
155 155 Does cache the results of the call, so can be called multiple times
156 156 without worrying about *further* damaging state.
157 157 """
158 158 if not self._oinfo:
159 159 # ip.shell._ofind is actually on the Magic class!
160 160 self._oinfo = ip.shell._ofind(self.ifun)
161 161 return self._oinfo
162 162
163 163 def __str__(self):
164 164 return "Lineinfo [%s|%s|%s]" %(self.pre, self.ifun, self.the_rest)
165 165
166 166
167 167 #-----------------------------------------------------------------------------
168 168 # Main Prefilter manager
169 169 #-----------------------------------------------------------------------------
170 170
171 171
172 172 class PrefilterManager(Configurable):
173 173 """Main prefilter component.
174 174
175 175 The IPython prefilter is run on all user input before it is run. The
176 176 prefilter consumes lines of input and produces transformed lines of
177 177 input.
178 178
179 179 The iplementation consists of two phases:
180 180
181 181 1. Transformers
182 182 2. Checkers and handlers
183 183
184 184 Over time, we plan on deprecating the checkers and handlers and doing
185 185 everything in the transformers.
186 186
187 187 The transformers are instances of :class:`PrefilterTransformer` and have
188 188 a single method :meth:`transform` that takes a line and returns a
189 189 transformed line. The transformation can be accomplished using any
190 190 tool, but our current ones use regular expressions for speed. We also
191 191 ship :mod:`pyparsing` in :mod:`IPython.external` for use in transformers.
192 192
193 193 After all the transformers have been run, the line is fed to the checkers,
194 194 which are instances of :class:`PrefilterChecker`. The line is passed to
195 195 the :meth:`check` method, which either returns `None` or a
196 196 :class:`PrefilterHandler` instance. If `None` is returned, the other
197 197 checkers are tried. If an :class:`PrefilterHandler` instance is returned,
198 198 the line is passed to the :meth:`handle` method of the returned
199 199 handler and no further checkers are tried.
200 200
201 201 Both transformers and checkers have a `priority` attribute, that determines
202 202 the order in which they are called. Smaller priorities are tried first.
203 203
204 204 Both transformers and checkers also have `enabled` attribute, which is
205 205 a boolean that determines if the instance is used.
206 206
207 207 Users or developers can change the priority or enabled attribute of
208 208 transformers or checkers, but they must call the :meth:`sort_checkers`
209 209 or :meth:`sort_transformers` method after changing the priority.
210 210 """
211 211
212 212 multi_line_specials = CBool(True, config=True)
213 213 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
214 214
215 215 def __init__(self, shell=None, config=None):
216 216 super(PrefilterManager, self).__init__(shell=shell, config=config)
217 217 self.shell = shell
218 218 self.init_transformers()
219 219 self.init_handlers()
220 220 self.init_checkers()
221 221
222 222 #-------------------------------------------------------------------------
223 223 # API for managing transformers
224 224 #-------------------------------------------------------------------------
225 225
226 226 def init_transformers(self):
227 227 """Create the default transformers."""
228 228 self._transformers = []
229 229 for transformer_cls in _default_transformers:
230 230 transformer_cls(
231 231 shell=self.shell, prefilter_manager=self, config=self.config
232 232 )
233 233
234 234 def sort_transformers(self):
235 235 """Sort the transformers by priority.
236 236
237 237 This must be called after the priority of a transformer is changed.
238 238 The :meth:`register_transformer` method calls this automatically.
239 239 """
240 240 self._transformers.sort(cmp=lambda x,y: x.priority-y.priority)
241 241
242 242 @property
243 243 def transformers(self):
244 244 """Return a list of checkers, sorted by priority."""
245 245 return self._transformers
246 246
247 247 def register_transformer(self, transformer):
248 248 """Register a transformer instance."""
249 249 if transformer not in self._transformers:
250 250 self._transformers.append(transformer)
251 251 self.sort_transformers()
252 252
253 253 def unregister_transformer(self, transformer):
254 254 """Unregister a transformer instance."""
255 255 if transformer in self._transformers:
256 256 self._transformers.remove(transformer)
257 257
258 258 #-------------------------------------------------------------------------
259 259 # API for managing checkers
260 260 #-------------------------------------------------------------------------
261 261
262 262 def init_checkers(self):
263 263 """Create the default checkers."""
264 264 self._checkers = []
265 265 for checker in _default_checkers:
266 266 checker(
267 267 shell=self.shell, prefilter_manager=self, config=self.config
268 268 )
269 269
270 270 def sort_checkers(self):
271 271 """Sort the checkers by priority.
272 272
273 273 This must be called after the priority of a checker is changed.
274 274 The :meth:`register_checker` method calls this automatically.
275 275 """
276 276 self._checkers.sort(cmp=lambda x,y: x.priority-y.priority)
277 277
278 278 @property
279 279 def checkers(self):
280 280 """Return a list of checkers, sorted by priority."""
281 281 return self._checkers
282 282
283 283 def register_checker(self, checker):
284 284 """Register a checker instance."""
285 285 if checker not in self._checkers:
286 286 self._checkers.append(checker)
287 287 self.sort_checkers()
288 288
289 289 def unregister_checker(self, checker):
290 290 """Unregister a checker instance."""
291 291 if checker in self._checkers:
292 292 self._checkers.remove(checker)
293 293
294 294 #-------------------------------------------------------------------------
295 295 # API for managing checkers
296 296 #-------------------------------------------------------------------------
297 297
298 298 def init_handlers(self):
299 299 """Create the default handlers."""
300 300 self._handlers = {}
301 301 self._esc_handlers = {}
302 302 for handler in _default_handlers:
303 303 handler(
304 304 shell=self.shell, prefilter_manager=self, config=self.config
305 305 )
306 306
307 307 @property
308 308 def handlers(self):
309 309 """Return a dict of all the handlers."""
310 310 return self._handlers
311 311
312 312 def register_handler(self, name, handler, esc_strings):
313 313 """Register a handler instance by name with esc_strings."""
314 314 self._handlers[name] = handler
315 315 for esc_str in esc_strings:
316 316 self._esc_handlers[esc_str] = handler
317 317
318 318 def unregister_handler(self, name, handler, esc_strings):
319 319 """Unregister a handler instance by name with esc_strings."""
320 320 try:
321 321 del self._handlers[name]
322 322 except KeyError:
323 323 pass
324 324 for esc_str in esc_strings:
325 325 h = self._esc_handlers.get(esc_str)
326 326 if h is handler:
327 327 del self._esc_handlers[esc_str]
328 328
329 329 def get_handler_by_name(self, name):
330 330 """Get a handler by its name."""
331 331 return self._handlers.get(name)
332 332
333 333 def get_handler_by_esc(self, esc_str):
334 334 """Get a handler by its escape string."""
335 335 return self._esc_handlers.get(esc_str)
336 336
337 337 #-------------------------------------------------------------------------
338 338 # Main prefiltering API
339 339 #-------------------------------------------------------------------------
340 340
341 341 def prefilter_line_info(self, line_info):
342 342 """Prefilter a line that has been converted to a LineInfo object.
343 343
344 344 This implements the checker/handler part of the prefilter pipe.
345 345 """
346 346 # print "prefilter_line_info: ", line_info
347 347 handler = self.find_handler(line_info)
348 348 return handler.handle(line_info)
349 349
350 350 def find_handler(self, line_info):
351 351 """Find a handler for the line_info by trying checkers."""
352 352 for checker in self.checkers:
353 353 if checker.enabled:
354 354 handler = checker.check(line_info)
355 355 if handler:
356 356 return handler
357 357 return self.get_handler_by_name('normal')
358 358
359 359 def transform_line(self, line, continue_prompt):
360 360 """Calls the enabled transformers in order of increasing priority."""
361 361 for transformer in self.transformers:
362 362 if transformer.enabled:
363 363 line = transformer.transform(line, continue_prompt)
364 364 return line
365 365
366 366 def prefilter_line(self, line, continue_prompt=False):
367 367 """Prefilter a single input line as text.
368 368
369 369 This method prefilters a single line of text by calling the
370 370 transformers and then the checkers/handlers.
371 371 """
372 372
373 373 # print "prefilter_line: ", line, continue_prompt
374 374 # All handlers *must* return a value, even if it's blank ('').
375 375
376 # Lines are NOT logged here. Handlers should process the line as
377 # needed, update the cache AND log it (so that the input cache array
378 # stays synced).
379
380 376 # save the line away in case we crash, so the post-mortem handler can
381 377 # record it
382 378 self.shell._last_input_line = line
383 379
384 380 if not line:
385 381 # Return immediately on purely empty lines, so that if the user
386 382 # previously typed some whitespace that started a continuation
387 383 # prompt, he can break out of that loop with just an empty line.
388 384 # This is how the default python prompt works.
389 385
390 386 # Only return if the accumulated input buffer was just whitespace!
391 387 if ''.join(self.shell.buffer).isspace():
392 388 self.shell.buffer[:] = []
393 389 return ''
394 390
395 391 # At this point, we invoke our transformers.
396 392 if not continue_prompt or (continue_prompt and self.multi_line_specials):
397 393 line = self.transform_line(line, continue_prompt)
398 394
399 395 # Now we compute line_info for the checkers and handlers
400 396 line_info = LineInfo(line, continue_prompt)
401 397
402 398 # the input history needs to track even empty lines
403 399 stripped = line.strip()
404 400
405 401 normal_handler = self.get_handler_by_name('normal')
406 402 if not stripped:
407 403 if not continue_prompt:
408 404 self.shell.displayhook.prompt_count -= 1
409 405
410 406 return normal_handler.handle(line_info)
411 407
412 408 # special handlers are only allowed for single line statements
413 409 if continue_prompt and not self.multi_line_specials:
414 410 return normal_handler.handle(line_info)
415 411
416 412 prefiltered = self.prefilter_line_info(line_info)
417 413 # print "prefiltered line: %r" % prefiltered
418 414 return prefiltered
419 415
420 416 def prefilter_lines(self, lines, continue_prompt=False):
421 417 """Prefilter multiple input lines of text.
422 418
423 419 This is the main entry point for prefiltering multiple lines of
424 420 input. This simply calls :meth:`prefilter_line` for each line of
425 421 input.
426 422
427 423 This covers cases where there are multiple lines in the user entry,
428 424 which is the case when the user goes back to a multiline history
429 425 entry and presses enter.
430 426 """
431 427 llines = lines.rstrip('\n').split('\n')
432 428 # We can get multiple lines in one shot, where multiline input 'blends'
433 429 # into one line, in cases like recalling from the readline history
434 430 # buffer. We need to make sure that in such cases, we correctly
435 431 # communicate downstream which line is first and which are continuation
436 432 # ones.
437 433 if len(llines) > 1:
438 434 out = '\n'.join([self.prefilter_line(line, lnum>0)
439 435 for lnum, line in enumerate(llines) ])
440 436 else:
441 437 out = self.prefilter_line(llines[0], continue_prompt)
442 438
443 439 return out
444 440
445 441 #-----------------------------------------------------------------------------
446 442 # Prefilter transformers
447 443 #-----------------------------------------------------------------------------
448 444
449 445
450 446 class PrefilterTransformer(Configurable):
451 447 """Transform a line of user input."""
452 448
453 449 priority = Int(100, config=True)
454 450 # Transformers don't currently use shell or prefilter_manager, but as we
455 451 # move away from checkers and handlers, they will need them.
456 452 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
457 453 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
458 454 enabled = Bool(True, config=True)
459 455
460 456 def __init__(self, shell=None, prefilter_manager=None, config=None):
461 457 super(PrefilterTransformer, self).__init__(
462 458 shell=shell, prefilter_manager=prefilter_manager, config=config
463 459 )
464 460 self.prefilter_manager.register_transformer(self)
465 461
466 462 def transform(self, line, continue_prompt):
467 463 """Transform a line, returning the new one."""
468 464 return None
469 465
470 466 def __repr__(self):
471 467 return "<%s(priority=%r, enabled=%r)>" % (
472 468 self.__class__.__name__, self.priority, self.enabled)
473 469
474 470
475 471 _assign_system_re = re.compile(r'(?P<lhs>(\s*)([\w\.]+)((\s*,\s*[\w\.]+)*))'
476 472 r'\s*=\s*!(?P<cmd>.*)')
477 473
478 474
479 475 class AssignSystemTransformer(PrefilterTransformer):
480 476 """Handle the `files = !ls` syntax."""
481 477
482 478 priority = Int(100, config=True)
483 479
484 480 def transform(self, line, continue_prompt):
485 481 m = _assign_system_re.match(line)
486 482 if m is not None:
487 483 cmd = m.group('cmd')
488 484 lhs = m.group('lhs')
489 485 expr = make_quoted_expr("sc =%s" % cmd)
490 486 new_line = '%s = get_ipython().magic(%s)' % (lhs, expr)
491 487 return new_line
492 488 return line
493 489
494 490
495 491 _assign_magic_re = re.compile(r'(?P<lhs>(\s*)([\w\.]+)((\s*,\s*[\w\.]+)*))'
496 492 r'\s*=\s*%(?P<cmd>.*)')
497 493
498 494 class AssignMagicTransformer(PrefilterTransformer):
499 495 """Handle the `a = %who` syntax."""
500 496
501 497 priority = Int(200, config=True)
502 498
503 499 def transform(self, line, continue_prompt):
504 500 m = _assign_magic_re.match(line)
505 501 if m is not None:
506 502 cmd = m.group('cmd')
507 503 lhs = m.group('lhs')
508 504 expr = make_quoted_expr(cmd)
509 505 new_line = '%s = get_ipython().magic(%s)' % (lhs, expr)
510 506 return new_line
511 507 return line
512 508
513 509
514 510 _classic_prompt_re = re.compile(r'(^[ \t]*>>> |^[ \t]*\.\.\. )')
515 511
516 512 class PyPromptTransformer(PrefilterTransformer):
517 513 """Handle inputs that start with '>>> ' syntax."""
518 514
519 515 priority = Int(50, config=True)
520 516
521 517 def transform(self, line, continue_prompt):
522 518
523 519 if not line or line.isspace() or line.strip() == '...':
524 520 # This allows us to recognize multiple input prompts separated by
525 521 # blank lines and pasted in a single chunk, very common when
526 522 # pasting doctests or long tutorial passages.
527 523 return ''
528 524 m = _classic_prompt_re.match(line)
529 525 if m:
530 526 return line[len(m.group(0)):]
531 527 else:
532 528 return line
533 529
534 530
535 531 _ipy_prompt_re = re.compile(r'(^[ \t]*In \[\d+\]: |^[ \t]*\ \ \ \.\.\.+: )')
536 532
537 533 class IPyPromptTransformer(PrefilterTransformer):
538 534 """Handle inputs that start classic IPython prompt syntax."""
539 535
540 536 priority = Int(50, config=True)
541 537
542 538 def transform(self, line, continue_prompt):
543 539
544 540 if not line or line.isspace() or line.strip() == '...':
545 541 # This allows us to recognize multiple input prompts separated by
546 542 # blank lines and pasted in a single chunk, very common when
547 543 # pasting doctests or long tutorial passages.
548 544 return ''
549 545 m = _ipy_prompt_re.match(line)
550 546 if m:
551 547 return line[len(m.group(0)):]
552 548 else:
553 549 return line
554 550
555 551 #-----------------------------------------------------------------------------
556 552 # Prefilter checkers
557 553 #-----------------------------------------------------------------------------
558 554
559 555
560 556 class PrefilterChecker(Configurable):
561 557 """Inspect an input line and return a handler for that line."""
562 558
563 559 priority = Int(100, config=True)
564 560 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
565 561 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
566 562 enabled = Bool(True, config=True)
567 563
568 564 def __init__(self, shell=None, prefilter_manager=None, config=None):
569 565 super(PrefilterChecker, self).__init__(
570 566 shell=shell, prefilter_manager=prefilter_manager, config=config
571 567 )
572 568 self.prefilter_manager.register_checker(self)
573 569
574 570 def check(self, line_info):
575 571 """Inspect line_info and return a handler instance or None."""
576 572 return None
577 573
578 574 def __repr__(self):
579 575 return "<%s(priority=%r, enabled=%r)>" % (
580 576 self.__class__.__name__, self.priority, self.enabled)
581 577
582 578
583 579 class EmacsChecker(PrefilterChecker):
584 580
585 581 priority = Int(100, config=True)
586 582 enabled = Bool(False, config=True)
587 583
588 584 def check(self, line_info):
589 585 "Emacs ipython-mode tags certain input lines."
590 586 if line_info.line.endswith('# PYTHON-MODE'):
591 587 return self.prefilter_manager.get_handler_by_name('emacs')
592 588 else:
593 589 return None
594 590
595 591
596 592 class ShellEscapeChecker(PrefilterChecker):
597 593
598 594 priority = Int(200, config=True)
599 595
600 596 def check(self, line_info):
601 597 if line_info.line.lstrip().startswith(ESC_SHELL):
602 598 return self.prefilter_manager.get_handler_by_name('shell')
603 599
604 600
605 601 class IPyAutocallChecker(PrefilterChecker):
606 602
607 603 priority = Int(300, config=True)
608 604
609 605 def check(self, line_info):
610 606 "Instances of IPyAutocall in user_ns get autocalled immediately"
611 607 obj = self.shell.user_ns.get(line_info.ifun, None)
612 608 if isinstance(obj, IPyAutocall):
613 609 obj.set_ip(self.shell)
614 610 return self.prefilter_manager.get_handler_by_name('auto')
615 611 else:
616 612 return None
617 613
618 614
619 615 class MultiLineMagicChecker(PrefilterChecker):
620 616
621 617 priority = Int(400, config=True)
622 618
623 619 def check(self, line_info):
624 620 "Allow ! and !! in multi-line statements if multi_line_specials is on"
625 621 # Note that this one of the only places we check the first character of
626 622 # ifun and *not* the pre_char. Also note that the below test matches
627 623 # both ! and !!.
628 624 if line_info.continue_prompt \
629 625 and self.prefilter_manager.multi_line_specials:
630 626 if line_info.ifun.startswith(ESC_MAGIC):
631 627 return self.prefilter_manager.get_handler_by_name('magic')
632 628 else:
633 629 return None
634 630
635 631
636 632 class EscCharsChecker(PrefilterChecker):
637 633
638 634 priority = Int(500, config=True)
639 635
640 636 def check(self, line_info):
641 637 """Check for escape character and return either a handler to handle it,
642 638 or None if there is no escape char."""
643 639 if line_info.line[-1] == ESC_HELP \
644 640 and line_info.pre_char != ESC_SHELL \
645 641 and line_info.pre_char != ESC_SH_CAP:
646 642 # the ? can be at the end, but *not* for either kind of shell escape,
647 643 # because a ? can be a vaild final char in a shell cmd
648 644 return self.prefilter_manager.get_handler_by_name('help')
649 645 else:
650 646 # This returns None like it should if no handler exists
651 647 return self.prefilter_manager.get_handler_by_esc(line_info.pre_char)
652 648
653 649
654 650 class AssignmentChecker(PrefilterChecker):
655 651
656 652 priority = Int(600, config=True)
657 653
658 654 def check(self, line_info):
659 655 """Check to see if user is assigning to a var for the first time, in
660 656 which case we want to avoid any sort of automagic / autocall games.
661 657
662 658 This allows users to assign to either alias or magic names true python
663 659 variables (the magic/alias systems always take second seat to true
664 660 python code). E.g. ls='hi', or ls,that=1,2"""
665 661 if line_info.the_rest:
666 662 if line_info.the_rest[0] in '=,':
667 663 return self.prefilter_manager.get_handler_by_name('normal')
668 664 else:
669 665 return None
670 666
671 667
672 668 class AutoMagicChecker(PrefilterChecker):
673 669
674 670 priority = Int(700, config=True)
675 671
676 672 def check(self, line_info):
677 673 """If the ifun is magic, and automagic is on, run it. Note: normal,
678 674 non-auto magic would already have been triggered via '%' in
679 675 check_esc_chars. This just checks for automagic. Also, before
680 676 triggering the magic handler, make sure that there is nothing in the
681 677 user namespace which could shadow it."""
682 678 if not self.shell.automagic or not hasattr(self.shell,'magic_'+line_info.ifun):
683 679 return None
684 680
685 681 # We have a likely magic method. Make sure we should actually call it.
686 682 if line_info.continue_prompt and not self.prefilter_manager.multi_line_specials:
687 683 return None
688 684
689 685 head = line_info.ifun.split('.',1)[0]
690 686 if is_shadowed(head, self.shell):
691 687 return None
692 688
693 689 return self.prefilter_manager.get_handler_by_name('magic')
694 690
695 691
696 692 class AliasChecker(PrefilterChecker):
697 693
698 694 priority = Int(800, config=True)
699 695
700 696 def check(self, line_info):
701 697 "Check if the initital identifier on the line is an alias."
702 698 # Note: aliases can not contain '.'
703 699 head = line_info.ifun.split('.',1)[0]
704 700 if line_info.ifun not in self.shell.alias_manager \
705 701 or head not in self.shell.alias_manager \
706 702 or is_shadowed(head, self.shell):
707 703 return None
708 704
709 705 return self.prefilter_manager.get_handler_by_name('alias')
710 706
711 707
712 708 class PythonOpsChecker(PrefilterChecker):
713 709
714 710 priority = Int(900, config=True)
715 711
716 712 def check(self, line_info):
717 713 """If the 'rest' of the line begins with a function call or pretty much
718 714 any python operator, we should simply execute the line (regardless of
719 715 whether or not there's a possible autocall expansion). This avoids
720 716 spurious (and very confusing) geattr() accesses."""
721 717 if line_info.the_rest and line_info.the_rest[0] in '!=()<>,+*/%^&|':
722 718 return self.prefilter_manager.get_handler_by_name('normal')
723 719 else:
724 720 return None
725 721
726 722
727 723 class AutocallChecker(PrefilterChecker):
728 724
729 725 priority = Int(1000, config=True)
730 726
731 727 def check(self, line_info):
732 728 "Check if the initial word/function is callable and autocall is on."
733 729 if not self.shell.autocall:
734 730 return None
735 731
736 732 oinfo = line_info.ofind(self.shell) # This can mutate state via getattr
737 733 if not oinfo['found']:
738 734 return None
739 735
740 736 if callable(oinfo['obj']) \
741 737 and (not re_exclude_auto.match(line_info.the_rest)) \
742 738 and re_fun_name.match(line_info.ifun):
743 739 return self.prefilter_manager.get_handler_by_name('auto')
744 740 else:
745 741 return None
746 742
747 743
748 744 #-----------------------------------------------------------------------------
749 745 # Prefilter handlers
750 746 #-----------------------------------------------------------------------------
751 747
752 748
753 749 class PrefilterHandler(Configurable):
754 750
755 751 handler_name = Str('normal')
756 752 esc_strings = List([])
757 753 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
758 754 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
759 755
760 756 def __init__(self, shell=None, prefilter_manager=None, config=None):
761 757 super(PrefilterHandler, self).__init__(
762 758 shell=shell, prefilter_manager=prefilter_manager, config=config
763 759 )
764 760 self.prefilter_manager.register_handler(
765 761 self.handler_name,
766 762 self,
767 763 self.esc_strings
768 764 )
769 765
770 766 def handle(self, line_info):
771 767 # print "normal: ", line_info
772 768 """Handle normal input lines. Use as a template for handlers."""
773 769
774 770 # With autoindent on, we need some way to exit the input loop, and I
775 771 # don't want to force the user to have to backspace all the way to
776 772 # clear the line. The rule will be in this case, that either two
777 773 # lines of pure whitespace in a row, or a line of pure whitespace but
778 774 # of a size different to the indent level, will exit the input loop.
779 775 line = line_info.line
780 776 continue_prompt = line_info.continue_prompt
781 777
782 778 if (continue_prompt and
783 779 self.shell.autoindent and
784 780 line.isspace() and
785 781
786 782 (0 < abs(len(line) - self.shell.indent_current_nsp) <= 2
787 783 or
788 784 not self.shell.buffer
789 785 or
790 786 (self.shell.buffer[-1]).isspace()
791 787 )
792 788 ):
793 789 line = ''
794 790
795 self.shell.log(line, line, continue_prompt)
796 791 return line
797 792
798 793 def __str__(self):
799 794 return "<%s(name=%s)>" % (self.__class__.__name__, self.handler_name)
800 795
801 796
802 797 class AliasHandler(PrefilterHandler):
803 798
804 799 handler_name = Str('alias')
805 800
806 801 def handle(self, line_info):
807 802 """Handle alias input lines. """
808 803 transformed = self.shell.alias_manager.expand_aliases(line_info.ifun,line_info.the_rest)
809 804 # pre is needed, because it carries the leading whitespace. Otherwise
810 805 # aliases won't work in indented sections.
811 806 line_out = '%sget_ipython().system(%s)' % (line_info.pre_whitespace,
812 807 make_quoted_expr(transformed))
813 808
814 self.shell.log(line_info.line, line_out, line_info.continue_prompt)
815 809 return line_out
816 810
817 811
818 812 class ShellEscapeHandler(PrefilterHandler):
819 813
820 814 handler_name = Str('shell')
821 815 esc_strings = List([ESC_SHELL, ESC_SH_CAP])
822 816
823 817 def handle(self, line_info):
824 818 """Execute the line in a shell, empty return value"""
825 819 magic_handler = self.prefilter_manager.get_handler_by_name('magic')
826 820
827 821 line = line_info.line
828 822 if line.lstrip().startswith(ESC_SH_CAP):
829 823 # rewrite LineInfo's line, ifun and the_rest to properly hold the
830 824 # call to %sx and the actual command to be executed, so
831 825 # handle_magic can work correctly. Note that this works even if
832 826 # the line is indented, so it handles multi_line_specials
833 827 # properly.
834 828 new_rest = line.lstrip()[2:]
835 829 line_info.line = '%ssx %s' % (ESC_MAGIC, new_rest)
836 830 line_info.ifun = 'sx'
837 831 line_info.the_rest = new_rest
838 832 return magic_handler.handle(line_info)
839 833 else:
840 834 cmd = line.lstrip().lstrip(ESC_SHELL)
841 835 line_out = '%sget_ipython().system(%s)' % (line_info.pre_whitespace,
842 836 make_quoted_expr(cmd))
843 # update cache/log and return
844 self.shell.log(line, line_out, line_info.continue_prompt)
845 837 return line_out
846 838
847 839
848 840 class MagicHandler(PrefilterHandler):
849 841
850 842 handler_name = Str('magic')
851 843 esc_strings = List([ESC_MAGIC])
852 844
853 845 def handle(self, line_info):
854 846 """Execute magic functions."""
855 847 ifun = line_info.ifun
856 848 the_rest = line_info.the_rest
857 849 cmd = '%sget_ipython().magic(%s)' % (line_info.pre_whitespace,
858 850 make_quoted_expr(ifun + " " + the_rest))
859 self.shell.log(line_info.line, cmd, line_info.continue_prompt)
860 851 return cmd
861 852
862 853
863 854 class AutoHandler(PrefilterHandler):
864 855
865 856 handler_name = Str('auto')
866 857 esc_strings = List([ESC_PAREN, ESC_QUOTE, ESC_QUOTE2])
867 858
868 859 def handle(self, line_info):
869 860 """Handle lines which can be auto-executed, quoting if requested."""
870 861 line = line_info.line
871 862 ifun = line_info.ifun
872 863 the_rest = line_info.the_rest
873 864 pre = line_info.pre
874 865 continue_prompt = line_info.continue_prompt
875 866 obj = line_info.ofind(self)['obj']
876 867 #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun,the_rest) # dbg
877 868
878 869 # This should only be active for single-line input!
879 870 if continue_prompt:
880 self.shell.log(line,line,continue_prompt)
881 871 return line
882 872
883 873 force_auto = isinstance(obj, IPyAutocall)
884 874 auto_rewrite = True
885 875
886 876 if pre == ESC_QUOTE:
887 877 # Auto-quote splitting on whitespace
888 878 newcmd = '%s("%s")' % (ifun,'", "'.join(the_rest.split()) )
889 879 elif pre == ESC_QUOTE2:
890 880 # Auto-quote whole string
891 881 newcmd = '%s("%s")' % (ifun,the_rest)
892 882 elif pre == ESC_PAREN:
893 883 newcmd = '%s(%s)' % (ifun,",".join(the_rest.split()))
894 884 else:
895 885 # Auto-paren.
896 886 # We only apply it to argument-less calls if the autocall
897 887 # parameter is set to 2. We only need to check that autocall is <
898 888 # 2, since this function isn't called unless it's at least 1.
899 889 if not the_rest and (self.shell.autocall < 2) and not force_auto:
900 890 newcmd = '%s %s' % (ifun,the_rest)
901 891 auto_rewrite = False
902 892 else:
903 893 if not force_auto and the_rest.startswith('['):
904 894 if hasattr(obj,'__getitem__'):
905 895 # Don't autocall in this case: item access for an object
906 896 # which is BOTH callable and implements __getitem__.
907 897 newcmd = '%s %s' % (ifun,the_rest)
908 898 auto_rewrite = False
909 899 else:
910 900 # if the object doesn't support [] access, go ahead and
911 901 # autocall
912 902 newcmd = '%s(%s)' % (ifun.rstrip(),the_rest)
913 903 elif the_rest.endswith(';'):
914 904 newcmd = '%s(%s);' % (ifun.rstrip(),the_rest[:-1])
915 905 else:
916 906 newcmd = '%s(%s)' % (ifun.rstrip(), the_rest)
917 907
918 908 if auto_rewrite:
919 909 self.shell.auto_rewrite_input(newcmd)
920 910
921 # log what is now valid Python, not the actual user input (without the
922 # final newline)
923 self.shell.log(line,newcmd,continue_prompt)
924 911 return newcmd
925 912
926 913
927 914 class HelpHandler(PrefilterHandler):
928 915
929 916 handler_name = Str('help')
930 917 esc_strings = List([ESC_HELP])
931 918
932 919 def handle(self, line_info):
933 920 """Try to get some help for the object.
934 921
935 922 obj? or ?obj -> basic information.
936 923 obj?? or ??obj -> more details.
937 924 """
938 925 normal_handler = self.prefilter_manager.get_handler_by_name('normal')
939 926 line = line_info.line
940 927 # We need to make sure that we don't process lines which would be
941 928 # otherwise valid python, such as "x=1 # what?"
942 929 try:
943 930 codeop.compile_command(line)
944 931 except SyntaxError:
945 932 # We should only handle as help stuff which is NOT valid syntax
946 933 if line[0]==ESC_HELP:
947 934 line = line[1:]
948 935 elif line[-1]==ESC_HELP:
949 936 line = line[:-1]
950 self.shell.log(line, '#?'+line, line_info.continue_prompt)
951 937 if line:
952 938 #print 'line:<%r>' % line # dbg
953 939 self.shell.magic_pinfo(line)
954 940 else:
955 941 self.shell.show_usage()
956 942 return '' # Empty string is needed here!
957 943 except:
958 944 raise
959 945 # Pass any other exceptions through to the normal handler
960 946 return normal_handler.handle(line_info)
961 947 else:
962 948 # If the code compiles ok, we should handle it normally
963 949 return normal_handler.handle(line_info)
964 950
965 951
966 952 class EmacsHandler(PrefilterHandler):
967 953
968 954 handler_name = Str('emacs')
969 955 esc_strings = List([])
970 956
971 957 def handle(self, line_info):
972 958 """Handle input lines marked by python-mode."""
973 959
974 960 # Currently, nothing is done. Later more functionality can be added
975 961 # here if needed.
976 962
977 963 # The input cache shouldn't be updated
978 964 return line_info.line
979 965
980 966
981 967 #-----------------------------------------------------------------------------
982 968 # Defaults
983 969 #-----------------------------------------------------------------------------
984 970
985 971
986 972 _default_transformers = [
987 973 AssignSystemTransformer,
988 974 AssignMagicTransformer,
989 975 PyPromptTransformer,
990 976 IPyPromptTransformer,
991 977 ]
992 978
993 979 _default_checkers = [
994 980 EmacsChecker,
995 981 ShellEscapeChecker,
996 982 IPyAutocallChecker,
997 983 MultiLineMagicChecker,
998 984 EscCharsChecker,
999 985 AssignmentChecker,
1000 986 AutoMagicChecker,
1001 987 AliasChecker,
1002 988 PythonOpsChecker,
1003 989 AutocallChecker
1004 990 ]
1005 991
1006 992 _default_handlers = [
1007 993 PrefilterHandler,
1008 994 AliasHandler,
1009 995 ShellEscapeHandler,
1010 996 MagicHandler,
1011 997 AutoHandler,
1012 998 HelpHandler,
1013 999 EmacsHandler
1014 1000 ]
@@ -1,665 +1,665 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3 """
4 4 The :class:`~IPython.core.application.Application` object for the command
5 5 line :command:`ipython` program.
6 6
7 7 Authors
8 8 -------
9 9
10 10 * Brian Granger
11 11 * Fernando Perez
12 12 """
13 13
14 14 #-----------------------------------------------------------------------------
15 15 # Copyright (C) 2008-2010 The IPython Development Team
16 16 #
17 17 # Distributed under the terms of the BSD License. The full license is in
18 18 # the file COPYING, distributed as part of this software.
19 19 #-----------------------------------------------------------------------------
20 20
21 21 #-----------------------------------------------------------------------------
22 22 # Imports
23 23 #-----------------------------------------------------------------------------
24 24
25 25 from __future__ import absolute_import
26 26
27 27 import logging
28 28 import os
29 29 import sys
30 30
31 31 from IPython.core import release
32 32 from IPython.core.crashhandler import CrashHandler
33 33 from IPython.core.application import Application, BaseAppConfigLoader
34 34 from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell
35 35 from IPython.config.loader import (
36 36 Config,
37 37 PyFileConfigLoader
38 38 )
39 39 from IPython.lib import inputhook
40 40 from IPython.utils.path import filefind, get_ipython_dir
41 41 from IPython.core import usage
42 42
43 43 #-----------------------------------------------------------------------------
44 44 # Globals, utilities and helpers
45 45 #-----------------------------------------------------------------------------
46 46
47 47 #: The default config file name for this application.
48 48 default_config_file_name = u'ipython_config.py'
49 49
50 50
51 51 class IPAppConfigLoader(BaseAppConfigLoader):
52 52
53 53 def _add_arguments(self):
54 54 super(IPAppConfigLoader, self)._add_arguments()
55 55 paa = self.parser.add_argument
56 56 paa('-p',
57 57 '--profile', dest='Global.profile', type=unicode,
58 58 help=
59 59 """The string name of the ipython profile to be used. Assume that your
60 60 config file is ipython_config-<name>.py (looks in current dir first,
61 61 then in IPYTHON_DIR). This is a quick way to keep and load multiple
62 62 config files for different tasks, especially if include your basic one
63 63 in your more specialized ones. You can keep a basic
64 64 IPYTHON_DIR/ipython_config.py file and then have other 'profiles' which
65 65 include this one and load extra things for particular tasks.""",
66 66 metavar='Global.profile')
67 67 paa('--config-file',
68 68 dest='Global.config_file', type=unicode,
69 69 help=
70 70 """Set the config file name to override default. Normally IPython
71 71 loads ipython_config.py (from current directory) or
72 72 IPYTHON_DIR/ipython_config.py. If the loading of your config file
73 73 fails, IPython starts with a bare bones configuration (no modules
74 74 loaded at all).""",
75 75 metavar='Global.config_file')
76 76 paa('--autocall',
77 77 dest='InteractiveShell.autocall', type=int,
78 78 help=
79 79 """Make IPython automatically call any callable object even if you
80 80 didn't type explicit parentheses. For example, 'str 43' becomes
81 81 'str(43)' automatically. The value can be '0' to disable the feature,
82 82 '1' for 'smart' autocall, where it is not applied if there are no more
83 83 arguments on the line, and '2' for 'full' autocall, where all callable
84 84 objects are automatically called (even if no arguments are present).
85 85 The default is '1'.""",
86 86 metavar='InteractiveShell.autocall')
87 87 paa('--autoindent',
88 88 action='store_true', dest='InteractiveShell.autoindent',
89 89 help='Turn on autoindenting.')
90 90 paa('--no-autoindent',
91 91 action='store_false', dest='InteractiveShell.autoindent',
92 92 help='Turn off autoindenting.')
93 93 paa('--automagic',
94 94 action='store_true', dest='InteractiveShell.automagic',
95 95 help=
96 96 """Turn on the auto calling of magic commands. Type %%magic at the
97 97 IPython prompt for more information.""")
98 98 paa('--no-automagic',
99 99 action='store_false', dest='InteractiveShell.automagic',
100 100 help='Turn off the auto calling of magic commands.')
101 101 paa('--autoedit-syntax',
102 102 action='store_true', dest='TerminalInteractiveShell.autoedit_syntax',
103 103 help='Turn on auto editing of files with syntax errors.')
104 104 paa('--no-autoedit-syntax',
105 105 action='store_false', dest='TerminalInteractiveShell.autoedit_syntax',
106 106 help='Turn off auto editing of files with syntax errors.')
107 107 paa('--banner',
108 108 action='store_true', dest='Global.display_banner',
109 109 help='Display a banner upon starting IPython.')
110 110 paa('--no-banner',
111 111 action='store_false', dest='Global.display_banner',
112 112 help="Don't display a banner upon starting IPython.")
113 113 paa('--cache-size',
114 114 type=int, dest='InteractiveShell.cache_size',
115 115 help=
116 116 """Set the size of the output cache. The default is 1000, you can
117 117 change it permanently in your config file. Setting it to 0 completely
118 118 disables the caching system, and the minimum value accepted is 20 (if
119 119 you provide a value less than 20, it is reset to 0 and a warning is
120 120 issued). This limit is defined because otherwise you'll spend more
121 121 time re-flushing a too small cache than working""",
122 122 metavar='InteractiveShell.cache_size')
123 123 paa('--classic',
124 124 action='store_true', dest='Global.classic',
125 125 help="Gives IPython a similar feel to the classic Python prompt.")
126 126 paa('--colors',
127 127 type=str, dest='InteractiveShell.colors',
128 128 help="Set the color scheme (NoColor, Linux, and LightBG).",
129 129 metavar='InteractiveShell.colors')
130 130 paa('--color-info',
131 131 action='store_true', dest='InteractiveShell.color_info',
132 132 help=
133 133 """IPython can display information about objects via a set of func-
134 134 tions, and optionally can use colors for this, syntax highlighting
135 135 source code and various other elements. However, because this
136 136 information is passed through a pager (like 'less') and many pagers get
137 137 confused with color codes, this option is off by default. You can test
138 138 it and turn it on permanently in your ipython_config.py file if it
139 139 works for you. Test it and turn it on permanently if it works with
140 140 your system. The magic function %%color_info allows you to toggle this
141 141 inter- actively for testing.""")
142 142 paa('--no-color-info',
143 143 action='store_false', dest='InteractiveShell.color_info',
144 144 help="Disable using colors for info related things.")
145 145 paa('--confirm-exit',
146 146 action='store_true', dest='TerminalInteractiveShell.confirm_exit',
147 147 help=
148 148 """Set to confirm when you try to exit IPython with an EOF (Control-D
149 149 in Unix, Control-Z/Enter in Windows). By typing 'exit', 'quit' or
150 150 '%%Exit', you can force a direct exit without any confirmation.""")
151 151 paa('--no-confirm-exit',
152 152 action='store_false', dest='TerminalInteractiveShell.confirm_exit',
153 153 help="Don't prompt the user when exiting.")
154 154 paa('--deep-reload',
155 155 action='store_true', dest='InteractiveShell.deep_reload',
156 156 help=
157 157 """Enable deep (recursive) reloading by default. IPython can use the
158 158 deep_reload module which reloads changes in modules recursively (it
159 159 replaces the reload() function, so you don't need to change anything to
160 160 use it). deep_reload() forces a full reload of modules whose code may
161 161 have changed, which the default reload() function does not. When
162 162 deep_reload is off, IPython will use the normal reload(), but
163 163 deep_reload will still be available as dreload(). This fea- ture is off
164 164 by default [which means that you have both normal reload() and
165 165 dreload()].""")
166 166 paa('--no-deep-reload',
167 167 action='store_false', dest='InteractiveShell.deep_reload',
168 168 help="Disable deep (recursive) reloading by default.")
169 169 paa('--editor',
170 170 type=str, dest='TerminalInteractiveShell.editor',
171 171 help="Set the editor used by IPython (default to $EDITOR/vi/notepad).",
172 172 metavar='TerminalInteractiveShell.editor')
173 173 paa('--log','-l',
174 174 action='store_true', dest='InteractiveShell.logstart',
175 175 help="Start logging to the default log file (./ipython_log.py).")
176 176 paa('--logfile','-lf',
177 177 type=unicode, dest='InteractiveShell.logfile',
178 178 help="Start logging to logfile with this name.",
179 179 metavar='InteractiveShell.logfile')
180 180 paa('--log-append','-la',
181 181 type=unicode, dest='InteractiveShell.logappend',
182 182 help="Start logging to the given file in append mode.",
183 183 metavar='InteractiveShell.logfile')
184 184 paa('--pdb',
185 185 action='store_true', dest='InteractiveShell.pdb',
186 186 help="Enable auto calling the pdb debugger after every exception.")
187 187 paa('--no-pdb',
188 188 action='store_false', dest='InteractiveShell.pdb',
189 189 help="Disable auto calling the pdb debugger after every exception.")
190 190 paa('--pprint',
191 191 action='store_true', dest='InteractiveShell.pprint',
192 192 help="Enable auto pretty printing of results.")
193 193 paa('--no-pprint',
194 194 action='store_false', dest='InteractiveShell.pprint',
195 195 help="Disable auto auto pretty printing of results.")
196 196 paa('--prompt-in1','-pi1',
197 197 type=str, dest='InteractiveShell.prompt_in1',
198 198 help=
199 199 """Set the main input prompt ('In [\#]: '). Note that if you are using
200 200 numbered prompts, the number is represented with a '\#' in the string.
201 201 Don't forget to quote strings with spaces embedded in them. Most
202 202 bash-like escapes can be used to customize IPython's prompts, as well
203 203 as a few additional ones which are IPython-spe- cific. All valid
204 204 prompt escapes are described in detail in the Customization section of
205 205 the IPython manual.""",
206 206 metavar='InteractiveShell.prompt_in1')
207 207 paa('--prompt-in2','-pi2',
208 208 type=str, dest='InteractiveShell.prompt_in2',
209 209 help=
210 210 """Set the secondary input prompt (' .\D.: '). Similar to the previous
211 211 option, but used for the continuation prompts. The special sequence
212 212 '\D' is similar to '\#', but with all digits replaced by dots (so you
213 213 can have your continuation prompt aligned with your input prompt).
214 214 Default: ' .\D.: ' (note three spaces at the start for alignment with
215 215 'In [\#]')""",
216 216 metavar='InteractiveShell.prompt_in2')
217 217 paa('--prompt-out','-po',
218 218 type=str, dest='InteractiveShell.prompt_out',
219 219 help="Set the output prompt ('Out[\#]:')",
220 220 metavar='InteractiveShell.prompt_out')
221 221 paa('--quick',
222 222 action='store_true', dest='Global.quick',
223 223 help="Enable quick startup with no config files.")
224 224 paa('--readline',
225 225 action='store_true', dest='InteractiveShell.readline_use',
226 226 help="Enable readline for command line usage.")
227 227 paa('--no-readline',
228 228 action='store_false', dest='InteractiveShell.readline_use',
229 229 help="Disable readline for command line usage.")
230 230 paa('--screen-length','-sl',
231 231 type=int, dest='TerminalInteractiveShell.screen_length',
232 232 help=
233 233 """Number of lines of your screen, used to control printing of very
234 234 long strings. Strings longer than this number of lines will be sent
235 235 through a pager instead of directly printed. The default value for
236 236 this is 0, which means IPython will auto-detect your screen size every
237 237 time it needs to print certain potentially long strings (this doesn't
238 238 change the behavior of the 'print' keyword, it's only triggered
239 239 internally). If for some reason this isn't working well (it needs
240 240 curses support), specify it yourself. Otherwise don't change the
241 241 default.""",
242 242 metavar='TerminalInteractiveShell.screen_length')
243 243 paa('--separate-in','-si',
244 244 type=str, dest='InteractiveShell.separate_in',
245 245 help="Separator before input prompts. Default '\\n'.",
246 246 metavar='InteractiveShell.separate_in')
247 247 paa('--separate-out','-so',
248 248 type=str, dest='InteractiveShell.separate_out',
249 249 help="Separator before output prompts. Default 0 (nothing).",
250 250 metavar='InteractiveShell.separate_out')
251 251 paa('--separate-out2','-so2',
252 252 type=str, dest='InteractiveShell.separate_out2',
253 253 help="Separator after output prompts. Default 0 (nonight).",
254 254 metavar='InteractiveShell.separate_out2')
255 255 paa('--no-sep',
256 256 action='store_true', dest='Global.nosep',
257 257 help="Eliminate all spacing between prompts.")
258 258 paa('--term-title',
259 259 action='store_true', dest='TerminalInteractiveShell.term_title',
260 260 help="Enable auto setting the terminal title.")
261 261 paa('--no-term-title',
262 262 action='store_false', dest='TerminalInteractiveShell.term_title',
263 263 help="Disable auto setting the terminal title.")
264 264 paa('--xmode',
265 265 type=str, dest='InteractiveShell.xmode',
266 266 help=
267 267 """Exception reporting mode ('Plain','Context','Verbose'). Plain:
268 268 similar to python's normal traceback printing. Context: prints 5 lines
269 269 of context source code around each line in the traceback. Verbose:
270 270 similar to Context, but additionally prints the variables currently
271 271 visible where the exception happened (shortening their strings if too
272 272 long). This can potentially be very slow, if you happen to have a huge
273 273 data structure whose string representation is complex to compute.
274 274 Your computer may appear to freeze for a while with cpu usage at 100%%.
275 275 If this occurs, you can cancel the traceback with Ctrl-C (maybe hitting
276 276 it more than once).
277 277 """,
278 278 metavar='InteractiveShell.xmode')
279 279 paa('--ext',
280 280 type=str, dest='Global.extra_extension',
281 281 help="The dotted module name of an IPython extension to load.",
282 282 metavar='Global.extra_extension')
283 283 paa('-c',
284 284 type=str, dest='Global.code_to_run',
285 285 help="Execute the given command string.",
286 286 metavar='Global.code_to_run')
287 287 paa('-i',
288 288 action='store_true', dest='Global.force_interact',
289 289 help=
290 290 "If running code from the command line, become interactive afterwards.")
291 291
292 292 # Options to start with GUI control enabled from the beginning
293 293 paa('--gui',
294 294 type=str, dest='Global.gui',
295 295 help="Enable GUI event loop integration ('qt', 'wx', 'gtk').",
296 296 metavar='gui-mode')
297 297 paa('--pylab','-pylab',
298 298 type=str, dest='Global.pylab',
299 299 nargs='?', const='auto', metavar='gui-mode',
300 300 help="Pre-load matplotlib and numpy for interactive use. "+
301 301 "If no value is given, the gui backend is matplotlib's, else use "+
302 302 "one of: ['tk', 'qt', 'wx', 'gtk'].")
303 303
304 304 # Legacy GUI options. Leave them in for backwards compatibility, but the
305 305 # 'thread' names are really a misnomer now.
306 306 paa('--wthread', '-wthread',
307 307 action='store_true', dest='Global.wthread',
308 308 help=
309 309 """Enable wxPython event loop integration. (DEPRECATED, use --gui wx)""")
310 310 paa('--q4thread', '--qthread', '-q4thread', '-qthread',
311 311 action='store_true', dest='Global.q4thread',
312 312 help=
313 313 """Enable Qt4 event loop integration. Qt3 is no longer supported.
314 314 (DEPRECATED, use --gui qt)""")
315 315 paa('--gthread', '-gthread',
316 316 action='store_true', dest='Global.gthread',
317 317 help=
318 318 """Enable GTK event loop integration. (DEPRECATED, use --gui gtk)""")
319 319
320 320
321 321 #-----------------------------------------------------------------------------
322 322 # Crash handler for this application
323 323 #-----------------------------------------------------------------------------
324 324
325 325
326 326 _message_template = """\
327 327 Oops, $self.app_name crashed. We do our best to make it stable, but...
328 328
329 329 A crash report was automatically generated with the following information:
330 330 - A verbatim copy of the crash traceback.
331 331 - A copy of your input history during this session.
332 332 - Data on your current $self.app_name configuration.
333 333
334 334 It was left in the file named:
335 335 \t'$self.crash_report_fname'
336 336 If you can email this file to the developers, the information in it will help
337 337 them in understanding and correcting the problem.
338 338
339 339 You can mail it to: $self.contact_name at $self.contact_email
340 340 with the subject '$self.app_name Crash Report'.
341 341
342 342 If you want to do it now, the following command will work (under Unix):
343 343 mail -s '$self.app_name Crash Report' $self.contact_email < $self.crash_report_fname
344 344
345 345 To ensure accurate tracking of this issue, please file a report about it at:
346 346 $self.bug_tracker
347 347 """
348 348
349 349 class IPAppCrashHandler(CrashHandler):
350 350 """sys.excepthook for IPython itself, leaves a detailed report on disk."""
351 351
352 352 message_template = _message_template
353 353
354 354 def __init__(self, app):
355 355 contact_name = release.authors['Fernando'][0]
356 356 contact_email = release.authors['Fernando'][1]
357 357 bug_tracker = 'https://bugs.launchpad.net/ipython/+filebug'
358 358 super(IPAppCrashHandler,self).__init__(
359 359 app, contact_name, contact_email, bug_tracker
360 360 )
361 361
362 362 def make_report(self,traceback):
363 363 """Return a string containing a crash report."""
364 364
365 365 sec_sep = self.section_sep
366 366 # Start with parent report
367 367 report = [super(IPAppCrashHandler, self).make_report(traceback)]
368 368 # Add interactive-specific info we may have
369 369 rpt_add = report.append
370 370 try:
371 371 rpt_add(sec_sep+"History of session input:")
372 372 for line in self.app.shell.user_ns['_ih']:
373 373 rpt_add(line)
374 374 rpt_add('\n*** Last line of input (may not be in above history):\n')
375 375 rpt_add(self.app.shell._last_input_line+'\n')
376 376 except:
377 377 pass
378 378
379 379 return ''.join(report)
380 380
381 381
382 382 #-----------------------------------------------------------------------------
383 383 # Main classes and functions
384 384 #-----------------------------------------------------------------------------
385 385
386 386 class IPythonApp(Application):
387 387 name = u'ipython'
388 388 #: argparse formats better the 'usage' than the 'description' field
389 389 description = None
390 390 usage = usage.cl_usage
391 391 command_line_loader = IPAppConfigLoader
392 392 default_config_file_name = default_config_file_name
393 393 crash_handler_class = IPAppCrashHandler
394 394
395 395 def create_default_config(self):
396 396 super(IPythonApp, self).create_default_config()
397 397 # Eliminate multiple lookups
398 398 Global = self.default_config.Global
399 399
400 400 # Set all default values
401 401 Global.display_banner = True
402 402
403 403 # If the -c flag is given or a file is given to run at the cmd line
404 404 # like "ipython foo.py", normally we exit without starting the main
405 405 # loop. The force_interact config variable allows a user to override
406 406 # this and interact. It is also set by the -i cmd line flag, just
407 407 # like Python.
408 408 Global.force_interact = False
409 409
410 410 # By default always interact by starting the IPython mainloop.
411 411 Global.interact = True
412 412
413 413 # No GUI integration by default
414 414 Global.gui = False
415 415 # Pylab off by default
416 416 Global.pylab = False
417 417
418 418 # Deprecated versions of gui support that used threading, we support
419 419 # them just for bacwards compatibility as an alternate spelling for
420 420 # '--gui X'
421 421 Global.qthread = False
422 422 Global.q4thread = False
423 423 Global.wthread = False
424 424 Global.gthread = False
425 425
426 426 def load_file_config(self):
427 427 if hasattr(self.command_line_config.Global, 'quick'):
428 428 if self.command_line_config.Global.quick:
429 429 self.file_config = Config()
430 430 return
431 431 super(IPythonApp, self).load_file_config()
432 432
433 433 def post_load_file_config(self):
434 434 if hasattr(self.command_line_config.Global, 'extra_extension'):
435 435 if not hasattr(self.file_config.Global, 'extensions'):
436 436 self.file_config.Global.extensions = []
437 437 self.file_config.Global.extensions.append(
438 438 self.command_line_config.Global.extra_extension)
439 439 del self.command_line_config.Global.extra_extension
440 440
441 441 def pre_construct(self):
442 442 config = self.master_config
443 443
444 444 if hasattr(config.Global, 'classic'):
445 445 if config.Global.classic:
446 446 config.InteractiveShell.cache_size = 0
447 447 config.InteractiveShell.pprint = 0
448 448 config.InteractiveShell.prompt_in1 = '>>> '
449 449 config.InteractiveShell.prompt_in2 = '... '
450 450 config.InteractiveShell.prompt_out = ''
451 451 config.InteractiveShell.separate_in = \
452 452 config.InteractiveShell.separate_out = \
453 453 config.InteractiveShell.separate_out2 = ''
454 454 config.InteractiveShell.colors = 'NoColor'
455 455 config.InteractiveShell.xmode = 'Plain'
456 456
457 457 if hasattr(config.Global, 'nosep'):
458 458 if config.Global.nosep:
459 459 config.InteractiveShell.separate_in = \
460 460 config.InteractiveShell.separate_out = \
461 461 config.InteractiveShell.separate_out2 = ''
462 462
463 463 # if there is code of files to run from the cmd line, don't interact
464 464 # unless the -i flag (Global.force_interact) is true.
465 465 code_to_run = config.Global.get('code_to_run','')
466 466 file_to_run = False
467 467 if self.extra_args and self.extra_args[0]:
468 468 file_to_run = True
469 469 if file_to_run or code_to_run:
470 470 if not config.Global.force_interact:
471 471 config.Global.interact = False
472 472
473 473 def construct(self):
474 474 # I am a little hesitant to put these into InteractiveShell itself.
475 475 # But that might be the place for them
476 476 sys.path.insert(0, '')
477 477
478 478 # Create an InteractiveShell instance.
479 479 self.shell = TerminalInteractiveShell.instance(config=self.master_config)
480 480
481 481 def post_construct(self):
482 482 """Do actions after construct, but before starting the app."""
483 483 config = self.master_config
484 484
485 485 # shell.display_banner should always be False for the terminal
486 486 # based app, because we call shell.show_banner() by hand below
487 487 # so the banner shows *before* all extension loading stuff.
488 488 self.shell.display_banner = False
489 489 if config.Global.display_banner and \
490 490 config.Global.interact:
491 491 self.shell.show_banner()
492 492
493 493 # Make sure there is a space below the banner.
494 494 if self.log_level <= logging.INFO: print
495 495
496 496 # Now a variety of things that happen after the banner is printed.
497 497 self._enable_gui_pylab()
498 498 self._load_extensions()
499 499 self._run_exec_lines()
500 500 self._run_exec_files()
501 501 self._run_cmd_line_code()
502 502
503 503 def _enable_gui_pylab(self):
504 504 """Enable GUI event loop integration, taking pylab into account."""
505 505 Global = self.master_config.Global
506 506
507 507 # Select which gui to use
508 508 if Global.gui:
509 509 gui = Global.gui
510 510 # The following are deprecated, but there's likely to be a lot of use
511 511 # of this form out there, so we might as well support it for now. But
512 512 # the --gui option above takes precedence.
513 513 elif Global.wthread:
514 514 gui = inputhook.GUI_WX
515 515 elif Global.qthread:
516 516 gui = inputhook.GUI_QT
517 517 elif Global.gthread:
518 518 gui = inputhook.GUI_GTK
519 519 else:
520 520 gui = None
521 521
522 522 # Using --pylab will also require gui activation, though which toolkit
523 523 # to use may be chosen automatically based on mpl configuration.
524 524 if Global.pylab:
525 525 activate = self.shell.enable_pylab
526 526 if Global.pylab == 'auto':
527 527 gui = None
528 528 else:
529 529 gui = Global.pylab
530 530 else:
531 531 # Enable only GUI integration, no pylab
532 532 activate = inputhook.enable_gui
533 533
534 534 if gui or Global.pylab:
535 535 try:
536 536 self.log.info("Enabling GUI event loop integration, "
537 537 "toolkit=%s, pylab=%s" % (gui, Global.pylab) )
538 538 activate(gui)
539 539 except:
540 540 self.log.warn("Error in enabling GUI event loop integration:")
541 541 self.shell.showtraceback()
542 542
543 543 def _load_extensions(self):
544 544 """Load all IPython extensions in Global.extensions.
545 545
546 546 This uses the :meth:`ExtensionManager.load_extensions` to load all
547 547 the extensions listed in ``self.master_config.Global.extensions``.
548 548 """
549 549 try:
550 550 if hasattr(self.master_config.Global, 'extensions'):
551 551 self.log.debug("Loading IPython extensions...")
552 552 extensions = self.master_config.Global.extensions
553 553 for ext in extensions:
554 554 try:
555 555 self.log.info("Loading IPython extension: %s" % ext)
556 556 self.shell.extension_manager.load_extension(ext)
557 557 except:
558 558 self.log.warn("Error in loading extension: %s" % ext)
559 559 self.shell.showtraceback()
560 560 except:
561 561 self.log.warn("Unknown error in loading extensions:")
562 562 self.shell.showtraceback()
563 563
564 564 def _run_exec_lines(self):
565 565 """Run lines of code in Global.exec_lines in the user's namespace."""
566 566 try:
567 567 if hasattr(self.master_config.Global, 'exec_lines'):
568 568 self.log.debug("Running code from Global.exec_lines...")
569 569 exec_lines = self.master_config.Global.exec_lines
570 570 for line in exec_lines:
571 571 try:
572 572 self.log.info("Running code in user namespace: %s" %
573 573 line)
574 self.shell.runlines(line)
574 self.shell.run_cell(line)
575 575 except:
576 576 self.log.warn("Error in executing line in user "
577 577 "namespace: %s" % line)
578 578 self.shell.showtraceback()
579 579 except:
580 580 self.log.warn("Unknown error in handling Global.exec_lines:")
581 581 self.shell.showtraceback()
582 582
583 583 def _exec_file(self, fname):
584 584 full_filename = filefind(fname, [u'.', self.ipython_dir])
585 585 if os.path.isfile(full_filename):
586 586 if full_filename.endswith(u'.py'):
587 587 self.log.info("Running file in user namespace: %s" %
588 588 full_filename)
589 589 # Ensure that __file__ is always defined to match Python behavior
590 590 self.shell.user_ns['__file__'] = fname
591 591 try:
592 592 self.shell.safe_execfile(full_filename, self.shell.user_ns)
593 593 finally:
594 594 del self.shell.user_ns['__file__']
595 595 elif full_filename.endswith('.ipy'):
596 596 self.log.info("Running file in user namespace: %s" %
597 597 full_filename)
598 598 self.shell.safe_execfile_ipy(full_filename)
599 599 else:
600 600 self.log.warn("File does not have a .py or .ipy extension: <%s>"
601 601 % full_filename)
602 602 def _run_exec_files(self):
603 603 try:
604 604 if hasattr(self.master_config.Global, 'exec_files'):
605 605 self.log.debug("Running files in Global.exec_files...")
606 606 exec_files = self.master_config.Global.exec_files
607 607 for fname in exec_files:
608 608 self._exec_file(fname)
609 609 except:
610 610 self.log.warn("Unknown error in handling Global.exec_files:")
611 611 self.shell.showtraceback()
612 612
613 613 def _run_cmd_line_code(self):
614 614 if hasattr(self.master_config.Global, 'code_to_run'):
615 615 line = self.master_config.Global.code_to_run
616 616 try:
617 617 self.log.info("Running code given at command line (-c): %s" %
618 618 line)
619 self.shell.runlines(line)
619 self.shell.run_cell(line)
620 620 except:
621 621 self.log.warn("Error in executing line in user namespace: %s" %
622 622 line)
623 623 self.shell.showtraceback()
624 624 return
625 625 # Like Python itself, ignore the second if the first of these is present
626 626 try:
627 627 fname = self.extra_args[0]
628 628 except:
629 629 pass
630 630 else:
631 631 try:
632 632 self._exec_file(fname)
633 633 except:
634 634 self.log.warn("Error in executing file in user namespace: %s" %
635 635 fname)
636 636 self.shell.showtraceback()
637 637
638 638 def start_app(self):
639 639 if self.master_config.Global.interact:
640 640 self.log.debug("Starting IPython's mainloop...")
641 641 self.shell.mainloop()
642 642 else:
643 643 self.log.debug("IPython not interactive, start_app is no-op...")
644 644
645 645
646 646 def load_default_config(ipython_dir=None):
647 647 """Load the default config file from the default ipython_dir.
648 648
649 649 This is useful for embedded shells.
650 650 """
651 651 if ipython_dir is None:
652 652 ipython_dir = get_ipython_dir()
653 653 cl = PyFileConfigLoader(default_config_file_name, ipython_dir)
654 654 config = cl.load_config()
655 655 return config
656 656
657 657
658 658 def launch_new_instance():
659 659 """Create and run a full blown IPython instance"""
660 660 app = IPythonApp()
661 661 app.start()
662 662
663 663
664 664 if __name__ == '__main__':
665 665 launch_new_instance()
@@ -1,575 +1,575 b''
1 1 """Module for interactive demos using IPython.
2 2
3 3 This module implements a few classes for running Python scripts interactively
4 4 in IPython for demonstrations. With very simple markup (a few tags in
5 5 comments), you can control points where the script stops executing and returns
6 6 control to IPython.
7 7
8 8
9 9 Provided classes
10 10 ================
11 11
12 12 The classes are (see their docstrings for further details):
13 13
14 14 - Demo: pure python demos
15 15
16 16 - IPythonDemo: demos with input to be processed by IPython as if it had been
17 17 typed interactively (so magics work, as well as any other special syntax you
18 18 may have added via input prefilters).
19 19
20 20 - LineDemo: single-line version of the Demo class. These demos are executed
21 21 one line at a time, and require no markup.
22 22
23 23 - IPythonLineDemo: IPython version of the LineDemo class (the demo is
24 24 executed a line at a time, but processed via IPython).
25 25
26 26 - ClearMixin: mixin to make Demo classes with less visual clutter. It
27 27 declares an empty marquee and a pre_cmd that clears the screen before each
28 28 block (see Subclassing below).
29 29
30 30 - ClearDemo, ClearIPDemo: mixin-enabled versions of the Demo and IPythonDemo
31 31 classes.
32 32
33 33
34 34 Subclassing
35 35 ===========
36 36
37 37 The classes here all include a few methods meant to make customization by
38 38 subclassing more convenient. Their docstrings below have some more details:
39 39
40 40 - marquee(): generates a marquee to provide visible on-screen markers at each
41 41 block start and end.
42 42
43 43 - pre_cmd(): run right before the execution of each block.
44 44
45 45 - post_cmd(): run right after the execution of each block. If the block
46 46 raises an exception, this is NOT called.
47 47
48 48
49 49 Operation
50 50 =========
51 51
52 52 The file is run in its own empty namespace (though you can pass it a string of
53 53 arguments as if in a command line environment, and it will see those as
54 54 sys.argv). But at each stop, the global IPython namespace is updated with the
55 55 current internal demo namespace, so you can work interactively with the data
56 56 accumulated so far.
57 57
58 58 By default, each block of code is printed (with syntax highlighting) before
59 59 executing it and you have to confirm execution. This is intended to show the
60 60 code to an audience first so you can discuss it, and only proceed with
61 61 execution once you agree. There are a few tags which allow you to modify this
62 62 behavior.
63 63
64 64 The supported tags are:
65 65
66 66 # <demo> stop
67 67
68 68 Defines block boundaries, the points where IPython stops execution of the
69 69 file and returns to the interactive prompt.
70 70
71 71 You can optionally mark the stop tag with extra dashes before and after the
72 72 word 'stop', to help visually distinguish the blocks in a text editor:
73 73
74 74 # <demo> --- stop ---
75 75
76 76
77 77 # <demo> silent
78 78
79 79 Make a block execute silently (and hence automatically). Typically used in
80 80 cases where you have some boilerplate or initialization code which you need
81 81 executed but do not want to be seen in the demo.
82 82
83 83 # <demo> auto
84 84
85 85 Make a block execute automatically, but still being printed. Useful for
86 86 simple code which does not warrant discussion, since it avoids the extra
87 87 manual confirmation.
88 88
89 89 # <demo> auto_all
90 90
91 91 This tag can _only_ be in the first block, and if given it overrides the
92 92 individual auto tags to make the whole demo fully automatic (no block asks
93 93 for confirmation). It can also be given at creation time (or the attribute
94 94 set later) to override what's in the file.
95 95
96 96 While _any_ python file can be run as a Demo instance, if there are no stop
97 97 tags the whole file will run in a single block (no different that calling
98 98 first %pycat and then %run). The minimal markup to make this useful is to
99 99 place a set of stop tags; the other tags are only there to let you fine-tune
100 100 the execution.
101 101
102 102 This is probably best explained with the simple example file below. You can
103 103 copy this into a file named ex_demo.py, and try running it via:
104 104
105 105 from IPython.demo import Demo
106 106 d = Demo('ex_demo.py')
107 107 d() <--- Call the d object (omit the parens if you have autocall set to 2).
108 108
109 109 Each time you call the demo object, it runs the next block. The demo object
110 110 has a few useful methods for navigation, like again(), edit(), jump(), seek()
111 111 and back(). It can be reset for a new run via reset() or reloaded from disk
112 112 (in case you've edited the source) via reload(). See their docstrings below.
113 113
114 114 Note: To make this simpler to explore, a file called "demo-exercizer.py" has
115 115 been added to the "docs/examples/core" directory. Just cd to this directory in
116 116 an IPython session, and type::
117 117
118 118 %run demo-exercizer.py
119 119
120 120 and then follow the directions.
121 121
122 122 Example
123 123 =======
124 124
125 125 The following is a very simple example of a valid demo file.
126 126
127 127 #################### EXAMPLE DEMO <ex_demo.py> ###############################
128 128 '''A simple interactive demo to illustrate the use of IPython's Demo class.'''
129 129
130 130 print 'Hello, welcome to an interactive IPython demo.'
131 131
132 132 # The mark below defines a block boundary, which is a point where IPython will
133 133 # stop execution and return to the interactive prompt. The dashes are actually
134 134 # optional and used only as a visual aid to clearly separate blocks while
135 135 # editing the demo code.
136 136 # <demo> stop
137 137
138 138 x = 1
139 139 y = 2
140 140
141 141 # <demo> stop
142 142
143 143 # the mark below makes this block as silent
144 144 # <demo> silent
145 145
146 146 print 'This is a silent block, which gets executed but not printed.'
147 147
148 148 # <demo> stop
149 149 # <demo> auto
150 150 print 'This is an automatic block.'
151 151 print 'It is executed without asking for confirmation, but printed.'
152 152 z = x+y
153 153
154 154 print 'z=',x
155 155
156 156 # <demo> stop
157 157 # This is just another normal block.
158 158 print 'z is now:', z
159 159
160 160 print 'bye!'
161 161 ################### END EXAMPLE DEMO <ex_demo.py> ############################
162 162 """
163 163
164 164 #*****************************************************************************
165 165 # Copyright (C) 2005-2006 Fernando Perez. <Fernando.Perez@colorado.edu>
166 166 #
167 167 # Distributed under the terms of the BSD License. The full license is in
168 168 # the file COPYING, distributed as part of this software.
169 169 #
170 170 #*****************************************************************************
171 171
172 172 import exceptions
173 173 import os
174 174 import re
175 175 import shlex
176 176 import sys
177 177
178 178 from IPython.utils.PyColorize import Parser
179 179 from IPython.utils.io import file_read, file_readlines
180 180 import IPython.utils.io
181 181 from IPython.utils.text import marquee
182 182
183 183 __all__ = ['Demo','IPythonDemo','LineDemo','IPythonLineDemo','DemoError']
184 184
185 185 class DemoError(exceptions.Exception): pass
186 186
187 187 def re_mark(mark):
188 188 return re.compile(r'^\s*#\s+<demo>\s+%s\s*$' % mark,re.MULTILINE)
189 189
190 190 class Demo(object):
191 191
192 192 re_stop = re_mark('-*\s?stop\s?-*')
193 193 re_silent = re_mark('silent')
194 194 re_auto = re_mark('auto')
195 195 re_auto_all = re_mark('auto_all')
196 196
197 197 def __init__(self,src,title='',arg_str='',auto_all=None):
198 198 """Make a new demo object. To run the demo, simply call the object.
199 199
200 200 See the module docstring for full details and an example (you can use
201 201 IPython.Demo? in IPython to see it).
202 202
203 203 Inputs:
204 204
205 205 - src is either a file, or file-like object, or a
206 206 string that can be resolved to a filename.
207 207
208 208 Optional inputs:
209 209
210 210 - title: a string to use as the demo name. Of most use when the demo
211 211 you are making comes from an object that has no filename, or if you
212 212 want an alternate denotation distinct from the filename.
213 213
214 214 - arg_str(''): a string of arguments, internally converted to a list
215 215 just like sys.argv, so the demo script can see a similar
216 216 environment.
217 217
218 218 - auto_all(None): global flag to run all blocks automatically without
219 219 confirmation. This attribute overrides the block-level tags and
220 220 applies to the whole demo. It is an attribute of the object, and
221 221 can be changed at runtime simply by reassigning it to a boolean
222 222 value.
223 223 """
224 224 if hasattr(src, "read"):
225 225 # It seems to be a file or a file-like object
226 226 self.fname = "from a file-like object"
227 227 if title == '':
228 228 self.title = "from a file-like object"
229 229 else:
230 230 self.title = title
231 231 else:
232 232 # Assume it's a string or something that can be converted to one
233 233 self.fname = src
234 234 if title == '':
235 235 (filepath, filename) = os.path.split(src)
236 236 self.title = filename
237 237 else:
238 238 self.title = title
239 239 self.sys_argv = [src] + shlex.split(arg_str)
240 240 self.auto_all = auto_all
241 241 self.src = src
242 242
243 243 # get a few things from ipython. While it's a bit ugly design-wise,
244 244 # it ensures that things like color scheme and the like are always in
245 245 # sync with the ipython mode being used. This class is only meant to
246 246 # be used inside ipython anyways, so it's OK.
247 247 ip = get_ipython() # this is in builtins whenever IPython is running
248 248 self.ip_ns = ip.user_ns
249 249 self.ip_colorize = ip.pycolorize
250 250 self.ip_showtb = ip.showtraceback
251 self.ip_runlines = ip.runlines
251 self.ip_run_cell = ip.run_cell
252 252 self.shell = ip
253 253
254 254 # load user data and initialize data structures
255 255 self.reload()
256 256
257 257 def fload(self):
258 258 """Load file object."""
259 259 # read data and parse into blocks
260 260 if hasattr(self, 'fobj') and self.fobj is not None:
261 261 self.fobj.close()
262 262 if hasattr(self.src, "read"):
263 263 # It seems to be a file or a file-like object
264 264 self.fobj = self.src
265 265 else:
266 266 # Assume it's a string or something that can be converted to one
267 267 self.fobj = open(self.fname)
268 268
269 269 def reload(self):
270 270 """Reload source from disk and initialize state."""
271 271 self.fload()
272 272
273 273 self.src = self.fobj.read()
274 274 src_b = [b.strip() for b in self.re_stop.split(self.src) if b]
275 275 self._silent = [bool(self.re_silent.findall(b)) for b in src_b]
276 276 self._auto = [bool(self.re_auto.findall(b)) for b in src_b]
277 277
278 278 # if auto_all is not given (def. None), we read it from the file
279 279 if self.auto_all is None:
280 280 self.auto_all = bool(self.re_auto_all.findall(src_b[0]))
281 281 else:
282 282 self.auto_all = bool(self.auto_all)
283 283
284 284 # Clean the sources from all markup so it doesn't get displayed when
285 285 # running the demo
286 286 src_blocks = []
287 287 auto_strip = lambda s: self.re_auto.sub('',s)
288 288 for i,b in enumerate(src_b):
289 289 if self._auto[i]:
290 290 src_blocks.append(auto_strip(b))
291 291 else:
292 292 src_blocks.append(b)
293 293 # remove the auto_all marker
294 294 src_blocks[0] = self.re_auto_all.sub('',src_blocks[0])
295 295
296 296 self.nblocks = len(src_blocks)
297 297 self.src_blocks = src_blocks
298 298
299 299 # also build syntax-highlighted source
300 300 self.src_blocks_colored = map(self.ip_colorize,self.src_blocks)
301 301
302 302 # ensure clean namespace and seek offset
303 303 self.reset()
304 304
305 305 def reset(self):
306 306 """Reset the namespace and seek pointer to restart the demo"""
307 307 self.user_ns = {}
308 308 self.finished = False
309 309 self.block_index = 0
310 310
311 311 def _validate_index(self,index):
312 312 if index<0 or index>=self.nblocks:
313 313 raise ValueError('invalid block index %s' % index)
314 314
315 315 def _get_index(self,index):
316 316 """Get the current block index, validating and checking status.
317 317
318 318 Returns None if the demo is finished"""
319 319
320 320 if index is None:
321 321 if self.finished:
322 322 print >>IPython.utils.io.Term.cout, 'Demo finished. Use <demo_name>.reset() if you want to rerun it.'
323 323 return None
324 324 index = self.block_index
325 325 else:
326 326 self._validate_index(index)
327 327 return index
328 328
329 329 def seek(self,index):
330 330 """Move the current seek pointer to the given block.
331 331
332 332 You can use negative indices to seek from the end, with identical
333 333 semantics to those of Python lists."""
334 334 if index<0:
335 335 index = self.nblocks + index
336 336 self._validate_index(index)
337 337 self.block_index = index
338 338 self.finished = False
339 339
340 340 def back(self,num=1):
341 341 """Move the seek pointer back num blocks (default is 1)."""
342 342 self.seek(self.block_index-num)
343 343
344 344 def jump(self,num=1):
345 345 """Jump a given number of blocks relative to the current one.
346 346
347 347 The offset can be positive or negative, defaults to 1."""
348 348 self.seek(self.block_index+num)
349 349
350 350 def again(self):
351 351 """Move the seek pointer back one block and re-execute."""
352 352 self.back(1)
353 353 self()
354 354
355 355 def edit(self,index=None):
356 356 """Edit a block.
357 357
358 358 If no number is given, use the last block executed.
359 359
360 360 This edits the in-memory copy of the demo, it does NOT modify the
361 361 original source file. If you want to do that, simply open the file in
362 362 an editor and use reload() when you make changes to the file. This
363 363 method is meant to let you change a block during a demonstration for
364 364 explanatory purposes, without damaging your original script."""
365 365
366 366 index = self._get_index(index)
367 367 if index is None:
368 368 return
369 369 # decrease the index by one (unless we're at the very beginning), so
370 370 # that the default demo.edit() call opens up the sblock we've last run
371 371 if index>0:
372 372 index -= 1
373 373
374 374 filename = self.shell.mktempfile(self.src_blocks[index])
375 375 self.shell.hooks.editor(filename,1)
376 376 new_block = file_read(filename)
377 377 # update the source and colored block
378 378 self.src_blocks[index] = new_block
379 379 self.src_blocks_colored[index] = self.ip_colorize(new_block)
380 380 self.block_index = index
381 381 # call to run with the newly edited index
382 382 self()
383 383
384 384 def show(self,index=None):
385 385 """Show a single block on screen"""
386 386
387 387 index = self._get_index(index)
388 388 if index is None:
389 389 return
390 390
391 391 print >>IPython.utils.io.Term.cout, self.marquee('<%s> block # %s (%s remaining)' %
392 392 (self.title,index,self.nblocks-index-1))
393 393 print >>IPython.utils.io.Term.cout,(self.src_blocks_colored[index])
394 394 sys.stdout.flush()
395 395
396 396 def show_all(self):
397 397 """Show entire demo on screen, block by block"""
398 398
399 399 fname = self.title
400 400 title = self.title
401 401 nblocks = self.nblocks
402 402 silent = self._silent
403 403 marquee = self.marquee
404 404 for index,block in enumerate(self.src_blocks_colored):
405 405 if silent[index]:
406 406 print >>IPython.utils.io.Term.cout, marquee('<%s> SILENT block # %s (%s remaining)' %
407 407 (title,index,nblocks-index-1))
408 408 else:
409 409 print >>IPython.utils.io.Term.cout, marquee('<%s> block # %s (%s remaining)' %
410 410 (title,index,nblocks-index-1))
411 411 print >>IPython.utils.io.Term.cout, block,
412 412 sys.stdout.flush()
413 413
414 def runlines(self,source):
414 def run_cell(self,source):
415 415 """Execute a string with one or more lines of code"""
416 416
417 417 exec source in self.user_ns
418 418
419 419 def __call__(self,index=None):
420 420 """run a block of the demo.
421 421
422 422 If index is given, it should be an integer >=1 and <= nblocks. This
423 423 means that the calling convention is one off from typical Python
424 424 lists. The reason for the inconsistency is that the demo always
425 425 prints 'Block n/N, and N is the total, so it would be very odd to use
426 426 zero-indexing here."""
427 427
428 428 index = self._get_index(index)
429 429 if index is None:
430 430 return
431 431 try:
432 432 marquee = self.marquee
433 433 next_block = self.src_blocks[index]
434 434 self.block_index += 1
435 435 if self._silent[index]:
436 436 print >>IPython.utils.io.Term.cout, marquee('Executing silent block # %s (%s remaining)' %
437 437 (index,self.nblocks-index-1))
438 438 else:
439 439 self.pre_cmd()
440 440 self.show(index)
441 441 if self.auto_all or self._auto[index]:
442 442 print >>IPython.utils.io.Term.cout, marquee('output:')
443 443 else:
444 444 print >>IPython.utils.io.Term.cout, marquee('Press <q> to quit, <Enter> to execute...'),
445 445 ans = raw_input().strip()
446 446 if ans:
447 447 print >>IPython.utils.io.Term.cout, marquee('Block NOT executed')
448 448 return
449 449 try:
450 450 save_argv = sys.argv
451 451 sys.argv = self.sys_argv
452 self.runlines(next_block)
452 self.run_cell(next_block)
453 453 self.post_cmd()
454 454 finally:
455 455 sys.argv = save_argv
456 456
457 457 except:
458 458 self.ip_showtb(filename=self.fname)
459 459 else:
460 460 self.ip_ns.update(self.user_ns)
461 461
462 462 if self.block_index == self.nblocks:
463 463 mq1 = self.marquee('END OF DEMO')
464 464 if mq1:
465 465 # avoid spurious print >>IPython.utils.io.Term.cout,s if empty marquees are used
466 466 print >>IPython.utils.io.Term.cout
467 467 print >>IPython.utils.io.Term.cout, mq1
468 468 print >>IPython.utils.io.Term.cout, self.marquee('Use <demo_name>.reset() if you want to rerun it.')
469 469 self.finished = True
470 470
471 471 # These methods are meant to be overridden by subclasses who may wish to
472 472 # customize the behavior of of their demos.
473 473 def marquee(self,txt='',width=78,mark='*'):
474 474 """Return the input string centered in a 'marquee'."""
475 475 return marquee(txt,width,mark)
476 476
477 477 def pre_cmd(self):
478 478 """Method called before executing each block."""
479 479 pass
480 480
481 481 def post_cmd(self):
482 482 """Method called after executing each block."""
483 483 pass
484 484
485 485
486 486 class IPythonDemo(Demo):
487 487 """Class for interactive demos with IPython's input processing applied.
488 488
489 489 This subclasses Demo, but instead of executing each block by the Python
490 490 interpreter (via exec), it actually calls IPython on it, so that any input
491 491 filters which may be in place are applied to the input block.
492 492
493 493 If you have an interactive environment which exposes special input
494 494 processing, you can use this class instead to write demo scripts which
495 495 operate exactly as if you had typed them interactively. The default Demo
496 496 class requires the input to be valid, pure Python code.
497 497 """
498 498
499 def runlines(self,source):
499 def run_cell(self,source):
500 500 """Execute a string with one or more lines of code"""
501 501
502 self.shell.runlines(source)
502 self.shell.run_cell(source)
503 503
504 504 class LineDemo(Demo):
505 505 """Demo where each line is executed as a separate block.
506 506
507 507 The input script should be valid Python code.
508 508
509 509 This class doesn't require any markup at all, and it's meant for simple
510 510 scripts (with no nesting or any kind of indentation) which consist of
511 511 multiple lines of input to be executed, one at a time, as if they had been
512 512 typed in the interactive prompt.
513 513
514 514 Note: the input can not have *any* indentation, which means that only
515 515 single-lines of input are accepted, not even function definitions are
516 516 valid."""
517 517
518 518 def reload(self):
519 519 """Reload source from disk and initialize state."""
520 520 # read data and parse into blocks
521 521 self.fload()
522 522 lines = self.fobj.readlines()
523 523 src_b = [l for l in lines if l.strip()]
524 524 nblocks = len(src_b)
525 525 self.src = ''.join(lines)
526 526 self._silent = [False]*nblocks
527 527 self._auto = [True]*nblocks
528 528 self.auto_all = True
529 529 self.nblocks = nblocks
530 530 self.src_blocks = src_b
531 531
532 532 # also build syntax-highlighted source
533 533 self.src_blocks_colored = map(self.ip_colorize,self.src_blocks)
534 534
535 535 # ensure clean namespace and seek offset
536 536 self.reset()
537 537
538 538
539 539 class IPythonLineDemo(IPythonDemo,LineDemo):
540 540 """Variant of the LineDemo class whose input is processed by IPython."""
541 541 pass
542 542
543 543
544 544 class ClearMixin(object):
545 545 """Use this mixin to make Demo classes with less visual clutter.
546 546
547 547 Demos using this mixin will clear the screen before every block and use
548 548 blank marquees.
549 549
550 550 Note that in order for the methods defined here to actually override those
551 551 of the classes it's mixed with, it must go /first/ in the inheritance
552 552 tree. For example:
553 553
554 554 class ClearIPDemo(ClearMixin,IPythonDemo): pass
555 555
556 556 will provide an IPythonDemo class with the mixin's features.
557 557 """
558 558
559 559 def marquee(self,txt='',width=78,mark='*'):
560 560 """Blank marquee that returns '' no matter what the input."""
561 561 return ''
562 562
563 563 def pre_cmd(self):
564 564 """Method called before executing each block.
565 565
566 566 This one simply clears the screen."""
567 567 from IPython.utils.terminal import term_clear
568 568 term_clear()
569 569
570 570 class ClearDemo(ClearMixin,Demo):
571 571 pass
572 572
573 573
574 574 class ClearIPDemo(ClearMixin,IPythonDemo):
575 575 pass
@@ -1,627 +1,621 b''
1 1 #!/usr/bin/env python
2 2 """A simple interactive kernel that talks to a frontend over 0MQ.
3 3
4 4 Things to do:
5 5
6 6 * Implement `set_parent` logic. Right before doing exec, the Kernel should
7 7 call set_parent on all the PUB objects with the message about to be executed.
8 8 * Implement random port and security key logic.
9 9 * Implement control messages.
10 10 * Implement event loop and poll version.
11 11 """
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16 from __future__ import print_function
17 17
18 18 # Standard library imports.
19 19 import __builtin__
20 20 import atexit
21 21 import sys
22 22 import time
23 23 import traceback
24 24
25 25 # System library imports.
26 26 import zmq
27 27
28 28 # Local imports.
29 29 from IPython.config.configurable import Configurable
30 30 from IPython.utils import io
31 31 from IPython.utils.jsonutil import json_clean
32 32 from IPython.lib import pylabtools
33 33 from IPython.utils.traitlets import Instance, Float
34 34 from entry_point import (base_launch_kernel, make_argument_parser, make_kernel,
35 35 start_kernel)
36 36 from iostream import OutStream
37 37 from session import Session, Message
38 38 from zmqshell import ZMQInteractiveShell
39 39
40 40 #-----------------------------------------------------------------------------
41 41 # Main kernel class
42 42 #-----------------------------------------------------------------------------
43 43
44 44 class Kernel(Configurable):
45 45
46 46 #---------------------------------------------------------------------------
47 47 # Kernel interface
48 48 #---------------------------------------------------------------------------
49 49
50 50 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
51 51 session = Instance(Session)
52 52 reply_socket = Instance('zmq.Socket')
53 53 pub_socket = Instance('zmq.Socket')
54 54 req_socket = Instance('zmq.Socket')
55 55
56 56 # Private interface
57 57
58 58 # Time to sleep after flushing the stdout/err buffers in each execute
59 59 # cycle. While this introduces a hard limit on the minimal latency of the
60 60 # execute cycle, it helps prevent output synchronization problems for
61 61 # clients.
62 62 # Units are in seconds. The minimum zmq latency on local host is probably
63 63 # ~150 microseconds, set this to 500us for now. We may need to increase it
64 64 # a little if it's not enough after more interactive testing.
65 65 _execute_sleep = Float(0.0005, config=True)
66 66
67 67 # Frequency of the kernel's event loop.
68 68 # Units are in seconds, kernel subclasses for GUI toolkits may need to
69 69 # adapt to milliseconds.
70 70 _poll_interval = Float(0.05, config=True)
71 71
72 72 # If the shutdown was requested over the network, we leave here the
73 73 # necessary reply message so it can be sent by our registered atexit
74 74 # handler. This ensures that the reply is only sent to clients truly at
75 75 # the end of our shutdown process (which happens after the underlying
76 76 # IPython shell's own shutdown).
77 77 _shutdown_message = None
78 78
79 79 # This is a dict of port number that the kernel is listening on. It is set
80 80 # by record_ports and used by connect_request.
81 81 _recorded_ports = None
82 82
83 83 def __init__(self, **kwargs):
84 84 super(Kernel, self).__init__(**kwargs)
85 85
86 86 # Before we even start up the shell, register *first* our exit handlers
87 87 # so they come before the shell's
88 88 atexit.register(self._at_shutdown)
89 89
90 90 # Initialize the InteractiveShell subclass
91 91 self.shell = ZMQInteractiveShell.instance()
92 92 self.shell.displayhook.session = self.session
93 93 self.shell.displayhook.pub_socket = self.pub_socket
94 94
95 95 # TMP - hack while developing
96 96 self.shell._reply_content = None
97 97
98 98 # Build dict of handlers for message types
99 99 msg_types = [ 'execute_request', 'complete_request',
100 100 'object_info_request', 'history_request',
101 101 'connect_request', 'shutdown_request']
102 102 self.handlers = {}
103 103 for msg_type in msg_types:
104 104 self.handlers[msg_type] = getattr(self, msg_type)
105 105
106 106 def do_one_iteration(self):
107 107 """Do one iteration of the kernel's evaluation loop.
108 108 """
109 109 try:
110 110 ident = self.reply_socket.recv(zmq.NOBLOCK)
111 111 except zmq.ZMQError, e:
112 112 if e.errno == zmq.EAGAIN:
113 113 return
114 114 else:
115 115 raise
116 116 # FIXME: Bug in pyzmq/zmq?
117 117 # assert self.reply_socket.rcvmore(), "Missing message part."
118 118 msg = self.reply_socket.recv_json()
119 119
120 120 # Print some info about this message and leave a '--->' marker, so it's
121 121 # easier to trace visually the message chain when debugging. Each
122 122 # handler prints its message at the end.
123 123 # Eventually we'll move these from stdout to a logger.
124 124 io.raw_print('\n*** MESSAGE TYPE:', msg['msg_type'], '***')
125 125 io.raw_print(' Content: ', msg['content'],
126 126 '\n --->\n ', sep='', end='')
127 127
128 128 # Find and call actual handler for message
129 129 handler = self.handlers.get(msg['msg_type'], None)
130 130 if handler is None:
131 131 io.raw_print_err("UNKNOWN MESSAGE TYPE:", msg)
132 132 else:
133 133 handler(ident, msg)
134 134
135 135 # Check whether we should exit, in case the incoming message set the
136 136 # exit flag on
137 137 if self.shell.exit_now:
138 138 io.raw_print('\nExiting IPython kernel...')
139 139 # We do a normal, clean exit, which allows any actions registered
140 140 # via atexit (such as history saving) to take place.
141 141 sys.exit(0)
142 142
143 143
144 144 def start(self):
145 145 """ Start the kernel main loop.
146 146 """
147 147 while True:
148 148 time.sleep(self._poll_interval)
149 149 self.do_one_iteration()
150 150
151 151 def record_ports(self, xrep_port, pub_port, req_port, hb_port):
152 152 """Record the ports that this kernel is using.
153 153
154 154 The creator of the Kernel instance must call this methods if they
155 155 want the :meth:`connect_request` method to return the port numbers.
156 156 """
157 157 self._recorded_ports = {
158 158 'xrep_port' : xrep_port,
159 159 'pub_port' : pub_port,
160 160 'req_port' : req_port,
161 161 'hb_port' : hb_port
162 162 }
163 163
164 164 #---------------------------------------------------------------------------
165 165 # Kernel request handlers
166 166 #---------------------------------------------------------------------------
167 167
168 168 def _publish_pyin(self, code, parent):
169 169 """Publish the code request on the pyin stream."""
170 170
171 171 pyin_msg = self.session.msg(u'pyin',{u'code':code}, parent=parent)
172 172 self.pub_socket.send_json(pyin_msg)
173 173
174 174 def execute_request(self, ident, parent):
175 175
176 176 status_msg = self.session.msg(
177 177 u'status',
178 178 {u'execution_state':u'busy'},
179 179 parent=parent
180 180 )
181 181 self.pub_socket.send_json(status_msg)
182 182
183 183 try:
184 184 content = parent[u'content']
185 185 code = content[u'code']
186 186 silent = content[u'silent']
187 187 except:
188 188 io.raw_print_err("Got bad msg: ")
189 189 io.raw_print_err(Message(parent))
190 190 return
191 191
192 192 shell = self.shell # we'll need this a lot here
193 193
194 194 # Replace raw_input. Note that is not sufficient to replace
195 195 # raw_input in the user namespace.
196 196 raw_input = lambda prompt='': self._raw_input(prompt, ident, parent)
197 197 __builtin__.raw_input = raw_input
198 198
199 199 # Set the parent message of the display hook and out streams.
200 200 shell.displayhook.set_parent(parent)
201 201 sys.stdout.set_parent(parent)
202 202 sys.stderr.set_parent(parent)
203 203
204 204 # Re-broadcast our input for the benefit of listening clients, and
205 205 # start computing output
206 206 if not silent:
207 207 self._publish_pyin(code, parent)
208 208
209 209 reply_content = {}
210 210 try:
211 211 if silent:
212 212 # runcode uses 'exec' mode, so no displayhook will fire, and it
213 213 # doesn't call logging or history manipulations. Print
214 214 # statements in that code will obviously still execute.
215 215 shell.runcode(code)
216 216 else:
217 # FIXME: runlines calls the exception handler itself.
217 # FIXME: the shell calls the exception handler itself.
218 218 shell._reply_content = None
219
220 # For now leave this here until we're sure we can stop using it
221 #shell.runlines(code)
222
223 # Experimental: cell mode! Test more before turning into
224 # default and removing the hacks around runlines.
225 219 shell.run_cell(code)
226 220 except:
227 221 status = u'error'
228 222 # FIXME: this code right now isn't being used yet by default,
229 223 # because the runlines() call above directly fires off exception
230 224 # reporting. This code, therefore, is only active in the scenario
231 225 # where runlines itself has an unhandled exception. We need to
232 226 # uniformize this, for all exception construction to come from a
233 227 # single location in the codbase.
234 228 etype, evalue, tb = sys.exc_info()
235 229 tb_list = traceback.format_exception(etype, evalue, tb)
236 230 reply_content.update(shell._showtraceback(etype, evalue, tb_list))
237 231 else:
238 232 status = u'ok'
239 233
240 234 reply_content[u'status'] = status
241 235
242 236 # Return the execution counter so clients can display prompts
243 237 reply_content['execution_count'] = shell.execution_count -1
244 238
245 239 # FIXME - fish exception info out of shell, possibly left there by
246 240 # runlines. We'll need to clean up this logic later.
247 241 if shell._reply_content is not None:
248 242 reply_content.update(shell._reply_content)
249 243
250 244 # At this point, we can tell whether the main code execution succeeded
251 245 # or not. If it did, we proceed to evaluate user_variables/expressions
252 246 if reply_content['status'] == 'ok':
253 247 reply_content[u'user_variables'] = \
254 248 shell.user_variables(content[u'user_variables'])
255 249 reply_content[u'user_expressions'] = \
256 250 shell.user_expressions(content[u'user_expressions'])
257 251 else:
258 252 # If there was an error, don't even try to compute variables or
259 253 # expressions
260 254 reply_content[u'user_variables'] = {}
261 255 reply_content[u'user_expressions'] = {}
262 256
263 257 # Payloads should be retrieved regardless of outcome, so we can both
264 258 # recover partial output (that could have been generated early in a
265 259 # block, before an error) and clear the payload system always.
266 260 reply_content[u'payload'] = shell.payload_manager.read_payload()
267 261 # Be agressive about clearing the payload because we don't want
268 262 # it to sit in memory until the next execute_request comes in.
269 263 shell.payload_manager.clear_payload()
270 264
271 265 # Send the reply.
272 266 reply_msg = self.session.msg(u'execute_reply', reply_content, parent)
273 267 io.raw_print(reply_msg)
274 268
275 269 # Flush output before sending the reply.
276 270 sys.stdout.flush()
277 271 sys.stderr.flush()
278 272 # FIXME: on rare occasions, the flush doesn't seem to make it to the
279 273 # clients... This seems to mitigate the problem, but we definitely need
280 274 # to better understand what's going on.
281 275 if self._execute_sleep:
282 276 time.sleep(self._execute_sleep)
283 277
284 278 self.reply_socket.send(ident, zmq.SNDMORE)
285 279 self.reply_socket.send_json(reply_msg)
286 280 if reply_msg['content']['status'] == u'error':
287 281 self._abort_queue()
288 282
289 283 status_msg = self.session.msg(
290 284 u'status',
291 285 {u'execution_state':u'idle'},
292 286 parent=parent
293 287 )
294 288 self.pub_socket.send_json(status_msg)
295 289
296 290 def complete_request(self, ident, parent):
297 291 txt, matches = self._complete(parent)
298 292 matches = {'matches' : matches,
299 293 'matched_text' : txt,
300 294 'status' : 'ok'}
301 295 completion_msg = self.session.send(self.reply_socket, 'complete_reply',
302 296 matches, parent, ident)
303 297 io.raw_print(completion_msg)
304 298
305 299 def object_info_request(self, ident, parent):
306 300 object_info = self.shell.object_inspect(parent['content']['oname'])
307 301 # Before we send this object over, we scrub it for JSON usage
308 302 oinfo = json_clean(object_info)
309 303 msg = self.session.send(self.reply_socket, 'object_info_reply',
310 304 oinfo, parent, ident)
311 305 io.raw_print(msg)
312 306
313 307 def history_request(self, ident, parent):
314 308 output = parent['content']['output']
315 309 index = parent['content']['index']
316 310 raw = parent['content']['raw']
317 311 hist = self.shell.get_history(index=index, raw=raw, output=output)
318 312 content = {'history' : hist}
319 313 msg = self.session.send(self.reply_socket, 'history_reply',
320 314 content, parent, ident)
321 315 io.raw_print(msg)
322 316
323 317 def connect_request(self, ident, parent):
324 318 if self._recorded_ports is not None:
325 319 content = self._recorded_ports.copy()
326 320 else:
327 321 content = {}
328 322 msg = self.session.send(self.reply_socket, 'connect_reply',
329 323 content, parent, ident)
330 324 io.raw_print(msg)
331 325
332 326 def shutdown_request(self, ident, parent):
333 327 self.shell.exit_now = True
334 328 self._shutdown_message = self.session.msg(u'shutdown_reply', {}, parent)
335 329 sys.exit(0)
336 330
337 331 #---------------------------------------------------------------------------
338 332 # Protected interface
339 333 #---------------------------------------------------------------------------
340 334
341 335 def _abort_queue(self):
342 336 while True:
343 337 try:
344 338 ident = self.reply_socket.recv(zmq.NOBLOCK)
345 339 except zmq.ZMQError, e:
346 340 if e.errno == zmq.EAGAIN:
347 341 break
348 342 else:
349 343 assert self.reply_socket.rcvmore(), \
350 344 "Unexpected missing message part."
351 345 msg = self.reply_socket.recv_json()
352 346 io.raw_print("Aborting:\n", Message(msg))
353 347 msg_type = msg['msg_type']
354 348 reply_type = msg_type.split('_')[0] + '_reply'
355 349 reply_msg = self.session.msg(reply_type, {'status' : 'aborted'}, msg)
356 350 io.raw_print(reply_msg)
357 351 self.reply_socket.send(ident,zmq.SNDMORE)
358 352 self.reply_socket.send_json(reply_msg)
359 353 # We need to wait a bit for requests to come in. This can probably
360 354 # be set shorter for true asynchronous clients.
361 355 time.sleep(0.1)
362 356
363 357 def _raw_input(self, prompt, ident, parent):
364 358 # Flush output before making the request.
365 359 sys.stderr.flush()
366 360 sys.stdout.flush()
367 361
368 362 # Send the input request.
369 363 content = dict(prompt=prompt)
370 364 msg = self.session.msg(u'input_request', content, parent)
371 365 self.req_socket.send_json(msg)
372 366
373 367 # Await a response.
374 368 reply = self.req_socket.recv_json()
375 369 try:
376 370 value = reply['content']['value']
377 371 except:
378 372 io.raw_print_err("Got bad raw_input reply: ")
379 373 io.raw_print_err(Message(parent))
380 374 value = ''
381 375 return value
382 376
383 377 def _complete(self, msg):
384 378 c = msg['content']
385 379 try:
386 380 cpos = int(c['cursor_pos'])
387 381 except:
388 382 # If we don't get something that we can convert to an integer, at
389 383 # least attempt the completion guessing the cursor is at the end of
390 384 # the text, if there's any, and otherwise of the line
391 385 cpos = len(c['text'])
392 386 if cpos==0:
393 387 cpos = len(c['line'])
394 388 return self.shell.complete(c['text'], c['line'], cpos)
395 389
396 390 def _object_info(self, context):
397 391 symbol, leftover = self._symbol_from_context(context)
398 392 if symbol is not None and not leftover:
399 393 doc = getattr(symbol, '__doc__', '')
400 394 else:
401 395 doc = ''
402 396 object_info = dict(docstring = doc)
403 397 return object_info
404 398
405 399 def _symbol_from_context(self, context):
406 400 if not context:
407 401 return None, context
408 402
409 403 base_symbol_string = context[0]
410 404 symbol = self.shell.user_ns.get(base_symbol_string, None)
411 405 if symbol is None:
412 406 symbol = __builtin__.__dict__.get(base_symbol_string, None)
413 407 if symbol is None:
414 408 return None, context
415 409
416 410 context = context[1:]
417 411 for i, name in enumerate(context):
418 412 new_symbol = getattr(symbol, name, None)
419 413 if new_symbol is None:
420 414 return symbol, context[i:]
421 415 else:
422 416 symbol = new_symbol
423 417
424 418 return symbol, []
425 419
426 420 def _at_shutdown(self):
427 421 """Actions taken at shutdown by the kernel, called by python's atexit.
428 422 """
429 423 # io.rprint("Kernel at_shutdown") # dbg
430 424 if self._shutdown_message is not None:
431 425 self.reply_socket.send_json(self._shutdown_message)
432 426 io.raw_print(self._shutdown_message)
433 427 # A very short sleep to give zmq time to flush its message buffers
434 428 # before Python truly shuts down.
435 429 time.sleep(0.01)
436 430
437 431
438 432 class QtKernel(Kernel):
439 433 """A Kernel subclass with Qt support."""
440 434
441 435 def start(self):
442 436 """Start a kernel with QtPy4 event loop integration."""
443 437
444 438 from PyQt4 import QtCore
445 439 from IPython.lib.guisupport import get_app_qt4, start_event_loop_qt4
446 440
447 441 self.app = get_app_qt4([" "])
448 442 self.app.setQuitOnLastWindowClosed(False)
449 443 self.timer = QtCore.QTimer()
450 444 self.timer.timeout.connect(self.do_one_iteration)
451 445 # Units for the timer are in milliseconds
452 446 self.timer.start(1000*self._poll_interval)
453 447 start_event_loop_qt4(self.app)
454 448
455 449
456 450 class WxKernel(Kernel):
457 451 """A Kernel subclass with Wx support."""
458 452
459 453 def start(self):
460 454 """Start a kernel with wx event loop support."""
461 455
462 456 import wx
463 457 from IPython.lib.guisupport import start_event_loop_wx
464 458
465 459 doi = self.do_one_iteration
466 460 # Wx uses milliseconds
467 461 poll_interval = int(1000*self._poll_interval)
468 462
469 463 # We have to put the wx.Timer in a wx.Frame for it to fire properly.
470 464 # We make the Frame hidden when we create it in the main app below.
471 465 class TimerFrame(wx.Frame):
472 466 def __init__(self, func):
473 467 wx.Frame.__init__(self, None, -1)
474 468 self.timer = wx.Timer(self)
475 469 # Units for the timer are in milliseconds
476 470 self.timer.Start(poll_interval)
477 471 self.Bind(wx.EVT_TIMER, self.on_timer)
478 472 self.func = func
479 473
480 474 def on_timer(self, event):
481 475 self.func()
482 476
483 477 # We need a custom wx.App to create our Frame subclass that has the
484 478 # wx.Timer to drive the ZMQ event loop.
485 479 class IPWxApp(wx.App):
486 480 def OnInit(self):
487 481 self.frame = TimerFrame(doi)
488 482 self.frame.Show(False)
489 483 return True
490 484
491 485 # The redirect=False here makes sure that wx doesn't replace
492 486 # sys.stdout/stderr with its own classes.
493 487 self.app = IPWxApp(redirect=False)
494 488 start_event_loop_wx(self.app)
495 489
496 490
497 491 class TkKernel(Kernel):
498 492 """A Kernel subclass with Tk support."""
499 493
500 494 def start(self):
501 495 """Start a Tk enabled event loop."""
502 496
503 497 import Tkinter
504 498 doi = self.do_one_iteration
505 499 # Tk uses milliseconds
506 500 poll_interval = int(1000*self._poll_interval)
507 501 # For Tkinter, we create a Tk object and call its withdraw method.
508 502 class Timer(object):
509 503 def __init__(self, func):
510 504 self.app = Tkinter.Tk()
511 505 self.app.withdraw()
512 506 self.func = func
513 507
514 508 def on_timer(self):
515 509 self.func()
516 510 self.app.after(poll_interval, self.on_timer)
517 511
518 512 def start(self):
519 513 self.on_timer() # Call it once to get things going.
520 514 self.app.mainloop()
521 515
522 516 self.timer = Timer(doi)
523 517 self.timer.start()
524 518
525 519
526 520 class GTKKernel(Kernel):
527 521 """A Kernel subclass with GTK support."""
528 522
529 523 def start(self):
530 524 """Start the kernel, coordinating with the GTK event loop"""
531 525 from .gui.gtkembed import GTKEmbed
532 526
533 527 gtk_kernel = GTKEmbed(self)
534 528 gtk_kernel.start()
535 529
536 530
537 531 #-----------------------------------------------------------------------------
538 532 # Kernel main and launch functions
539 533 #-----------------------------------------------------------------------------
540 534
541 535 def launch_kernel(xrep_port=0, pub_port=0, req_port=0, hb_port=0,
542 536 independent=False, pylab=False):
543 537 """Launches a localhost kernel, binding to the specified ports.
544 538
545 539 Parameters
546 540 ----------
547 541 xrep_port : int, optional
548 542 The port to use for XREP channel.
549 543
550 544 pub_port : int, optional
551 545 The port to use for the SUB channel.
552 546
553 547 req_port : int, optional
554 548 The port to use for the REQ (raw input) channel.
555 549
556 550 hb_port : int, optional
557 551 The port to use for the hearbeat REP channel.
558 552
559 553 independent : bool, optional (default False)
560 554 If set, the kernel process is guaranteed to survive if this process
561 555 dies. If not set, an effort is made to ensure that the kernel is killed
562 556 when this process dies. Note that in this case it is still good practice
563 557 to kill kernels manually before exiting.
564 558
565 559 pylab : bool or string, optional (default False)
566 560 If not False, the kernel will be launched with pylab enabled. If a
567 561 string is passed, matplotlib will use the specified backend. Otherwise,
568 562 matplotlib's default backend will be used.
569 563
570 564 Returns
571 565 -------
572 566 A tuple of form:
573 567 (kernel_process, xrep_port, pub_port, req_port)
574 568 where kernel_process is a Popen object and the ports are integers.
575 569 """
576 570 extra_arguments = []
577 571 if pylab:
578 572 extra_arguments.append('--pylab')
579 573 if isinstance(pylab, basestring):
580 574 extra_arguments.append(pylab)
581 575 return base_launch_kernel('from IPython.zmq.ipkernel import main; main()',
582 576 xrep_port, pub_port, req_port, hb_port,
583 577 independent, extra_arguments)
584 578
585 579
586 580 def main():
587 581 """ The IPython kernel main entry point.
588 582 """
589 583 parser = make_argument_parser()
590 584 parser.add_argument('--pylab', type=str, metavar='GUI', nargs='?',
591 585 const='auto', help = \
592 586 "Pre-load matplotlib and numpy for interactive use. If GUI is not \
593 587 given, the GUI backend is matplotlib's, otherwise use one of: \
594 588 ['tk', 'gtk', 'qt', 'wx', 'inline'].")
595 589 namespace = parser.parse_args()
596 590
597 591 kernel_class = Kernel
598 592
599 593 kernel_classes = {
600 594 'qt' : QtKernel,
601 595 'qt4': QtKernel,
602 596 'inline': Kernel,
603 597 'wx' : WxKernel,
604 598 'tk' : TkKernel,
605 599 'gtk': GTKKernel,
606 600 }
607 601 if namespace.pylab:
608 602 if namespace.pylab == 'auto':
609 603 gui, backend = pylabtools.find_gui_and_backend()
610 604 else:
611 605 gui, backend = pylabtools.find_gui_and_backend(namespace.pylab)
612 606 kernel_class = kernel_classes.get(gui)
613 607 if kernel_class is None:
614 608 raise ValueError('GUI is not supported: %r' % gui)
615 609 pylabtools.activate_matplotlib(backend)
616 610
617 611 kernel = make_kernel(namespace, kernel_class, OutStream)
618 612
619 613 if namespace.pylab:
620 614 pylabtools.import_pylab(kernel.shell.user_ns, backend,
621 615 shell=kernel.shell)
622 616
623 617 start_kernel(namespace, kernel)
624 618
625 619
626 620 if __name__ == '__main__':
627 621 main()
General Comments 0
You need to be logged in to leave comments. Login now