Show More
@@ -53,7 +53,7 import IPython.utils.io | |||
|
53 | 53 | __all__ = ['editor', 'fix_error_editor', 'synchronize_with_editor', |
|
54 | 54 | 'input_prefilter', 'shutdown_hook', 'late_startup_hook', |
|
55 | 55 | 'generate_prompt', 'show_in_pager','pre_prompt_hook', |
|
56 | 'pre_runcode_hook', 'clipboard_get'] | |
|
56 | 'pre_run_code_hook', 'clipboard_get'] | |
|
57 | 57 | |
|
58 | 58 | def editor(self,filename, linenum=None): |
|
59 | 59 | """Open the default editor at the given filename and linenumber. |
@@ -234,7 +234,7 def pre_prompt_hook(self): | |||
|
234 | 234 | return None |
|
235 | 235 | |
|
236 | 236 | |
|
237 | def pre_runcode_hook(self): | |
|
237 | def pre_run_code_hook(self): | |
|
238 | 238 | """ Executed before running the (prefiltered) code in IPython """ |
|
239 | 239 | return None |
|
240 | 240 |
@@ -1211,14 +1211,20 class InteractiveShell(Configurable, Magic): | |||
|
1211 | 1211 | def init_history(self): |
|
1212 | 1212 | self.history_manager = HistoryManager(shell=self) |
|
1213 | 1213 | |
|
1214 | def savehist(self): | |
|
1214 | def save_hist(self): | |
|
1215 | 1215 | """Save input history to a file (via readline library).""" |
|
1216 | 1216 | self.history_manager.save_hist() |
|
1217 | ||
|
1218 | # For backwards compatibility | |
|
1219 | savehist = save_hist | |
|
1217 | 1220 | |
|
1218 | def reloadhist(self): | |
|
1221 | def reload_hist(self): | |
|
1219 | 1222 | """Reload the input history from disk file.""" |
|
1220 | 1223 | self.history_manager.reload_hist() |
|
1221 | 1224 | |
|
1225 | # For backwards compatibility | |
|
1226 | reloadhist = reload_hist | |
|
1227 | ||
|
1222 | 1228 | def history_saving_wrapper(self, func): |
|
1223 | 1229 | """ Wrap func for readline history saving |
|
1224 | 1230 | |
@@ -1231,7 +1237,7 class InteractiveShell(Configurable, Magic): | |||
|
1231 | 1237 | return func |
|
1232 | 1238 | |
|
1233 | 1239 | def wrapper(): |
|
1234 | self.savehist() | |
|
1240 | self.save_hist() | |
|
1235 | 1241 | try: |
|
1236 | 1242 | func() |
|
1237 | 1243 | finally: |
@@ -1269,7 +1275,7 class InteractiveShell(Configurable, Magic): | |||
|
1269 | 1275 | |
|
1270 | 1276 | Set a custom exception handler, which will be called if any of the |
|
1271 | 1277 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
1272 | runcode() method. | |
|
1278 | run_code() method. | |
|
1273 | 1279 | |
|
1274 | 1280 | Inputs: |
|
1275 | 1281 | |
@@ -1464,8 +1470,8 class InteractiveShell(Configurable, Magic): | |||
|
1464 | 1470 | self.has_readline = False |
|
1465 | 1471 | self.readline = None |
|
1466 | 1472 | # Set a number of methods that depend on readline to be no-op |
|
1467 | self.savehist = no_op | |
|
1468 | self.reloadhist = no_op | |
|
1473 | self.save_hist = no_op | |
|
1474 | self.reload_hist = no_op | |
|
1469 | 1475 | self.set_readline_completer = no_op |
|
1470 | 1476 | self.set_custom_completer = no_op |
|
1471 | 1477 | self.set_completer_frame = no_op |
@@ -1527,7 +1533,7 class InteractiveShell(Configurable, Magic): | |||
|
1527 | 1533 | |
|
1528 | 1534 | # If we have readline, we want our history saved upon ipython |
|
1529 | 1535 | # exiting. |
|
1530 | atexit.register(self.savehist) | |
|
1536 | atexit.register(self.save_hist) | |
|
1531 | 1537 | |
|
1532 | 1538 | # Configure auto-indent for all platforms |
|
1533 | 1539 | self.set_autoindent(self.autoindent) |
@@ -2075,8 +2081,8 class InteractiveShell(Configurable, Magic): | |||
|
2075 | 2081 | # the main object). |
|
2076 | 2082 | # - do any logging of input |
|
2077 | 2083 | # - update histories (raw/translated) |
|
2078 | # - then, call plain runsource (for single blocks, so displayhook is | |
|
2079 | # triggered) or runcode (for multiline blocks in exec mode). | |
|
2084 | # - then, call plain run_source (for single blocks, so displayhook is | |
|
2085 | # triggered) or run_code (for multiline blocks in exec mode). | |
|
2080 | 2086 | # |
|
2081 | 2087 | # Once this is done, we'll be able to stop using runlines and we'll |
|
2082 | 2088 | # also have a much cleaner separation of logging, input history and |
@@ -2125,13 +2131,13 class InteractiveShell(Configurable, Magic): | |||
|
2125 | 2131 | |
|
2126 | 2132 | # In multi-block input, if the last block is a simple (one-two |
|
2127 | 2133 | # lines) expression, run it in single mode so it produces output. |
|
2128 | # Otherwise just feed the whole thing to runcode. This seems like | |
|
2134 | # Otherwise just feed the whole thing to run_code. This seems like | |
|
2129 | 2135 | # a reasonable usability design. |
|
2130 | 2136 | last = blocks[-1] |
|
2131 | 2137 | last_nlines = len(last.splitlines()) |
|
2132 | 2138 | |
|
2133 | # Note: below, whenever we call runcode, we must sync history | |
|
2134 | # ourselves, because runcode is NOT meant to manage history at all. | |
|
2139 | # Note: below, whenever we call run_code, we must sync history | |
|
2140 | # ourselves, because run_code is NOT meant to manage history at all. | |
|
2135 | 2141 | if last_nlines < 2: |
|
2136 | 2142 | # Here we consider the cell split between 'body' and 'last', |
|
2137 | 2143 | # store all history and execute 'body', and if successful, then |
@@ -2139,14 +2145,14 class InteractiveShell(Configurable, Magic): | |||
|
2139 | 2145 | |
|
2140 | 2146 | # Get the main body to run as a cell |
|
2141 | 2147 | ipy_body = ''.join(blocks[:-1]) |
|
2142 | retcode = self.runcode(ipy_body, post_execute=False) | |
|
2148 | retcode = self.run_code(ipy_body, post_execute=False) | |
|
2143 | 2149 | if retcode==0: |
|
2144 | 2150 | # And the last expression via runlines so it produces output |
|
2145 | 2151 | self.run_one_block(last) |
|
2146 | 2152 | else: |
|
2147 | 2153 | # Run the whole cell as one entity, storing both raw and |
|
2148 | 2154 | # processed input in history |
|
2149 | self.runcode(ipy_cell) | |
|
2155 | self.run_code(ipy_cell) | |
|
2150 | 2156 | |
|
2151 | 2157 | # Each cell is a *single* input, regardless of how many lines it has |
|
2152 | 2158 | self.execution_count += 1 |
@@ -2160,7 +2166,7 class InteractiveShell(Configurable, Magic): | |||
|
2160 | 2166 | if len(block.splitlines()) <= 1: |
|
2161 | 2167 | out = self.run_single_line(block) |
|
2162 | 2168 | else: |
|
2163 | out = self.runcode(block) | |
|
2169 | out = self.run_code(block) | |
|
2164 | 2170 | return out |
|
2165 | 2171 | |
|
2166 | 2172 | def run_single_line(self, line): |
@@ -2173,7 +2179,7 class InteractiveShell(Configurable, Magic): | |||
|
2173 | 2179 | It does not update history. |
|
2174 | 2180 | """ |
|
2175 | 2181 | tline = self.prefilter_manager.prefilter_line(line) |
|
2176 | return self.runsource(tline) | |
|
2182 | return self.run_source(tline) | |
|
2177 | 2183 | |
|
2178 | 2184 | def runlines(self, lines, clean=False): |
|
2179 | 2185 | """Run a string of one or more lines of source. |
@@ -2192,7 +2198,7 class InteractiveShell(Configurable, Magic): | |||
|
2192 | 2198 | |
|
2193 | 2199 | # We must start with a clean buffer, in case this is run from an |
|
2194 | 2200 | # interactive IPython session (via a magic, for example). |
|
2195 | self.resetbuffer() | |
|
2201 | self.reset_buffer() | |
|
2196 | 2202 | lines = lines.splitlines() |
|
2197 | 2203 | |
|
2198 | 2204 | # Since we will prefilter all lines, store the user's raw input too |
@@ -2209,7 +2215,7 class InteractiveShell(Configurable, Magic): | |||
|
2209 | 2215 | |
|
2210 | 2216 | if line or more: |
|
2211 | 2217 | more = self.push_line(prefilter_lines(line, more)) |
|
2212 | # IPython's runsource returns None if there was an error | |
|
2218 | # IPython's run_source returns None if there was an error | |
|
2213 | 2219 | # compiling the code. This allows us to stop processing |
|
2214 | 2220 | # right away, so the user gets the error message at the |
|
2215 | 2221 | # right place. |
@@ -2220,7 +2226,7 class InteractiveShell(Configurable, Magic): | |||
|
2220 | 2226 | if more: |
|
2221 | 2227 | self.push_line('\n') |
|
2222 | 2228 | |
|
2223 | def runsource(self, source, filename='<ipython console>', symbol='single'): | |
|
2229 | def run_source(self, source, filename='<ipython console>', symbol='single'): | |
|
2224 | 2230 | """Compile and run some source in the interpreter. |
|
2225 | 2231 | |
|
2226 | 2232 | Arguments are as for compile_command(). |
@@ -2235,7 +2241,7 class InteractiveShell(Configurable, Magic): | |||
|
2235 | 2241 | compile_command() returned None. Nothing happens. |
|
2236 | 2242 | |
|
2237 | 2243 | 3) The input is complete; compile_command() returned a code |
|
2238 | object. The code is executed by calling self.runcode() (which | |
|
2244 | object. The code is executed by calling self.run_code() (which | |
|
2239 | 2245 | also handles run-time exceptions, except for SystemExit). |
|
2240 | 2246 | |
|
2241 | 2247 | The return value is: |
@@ -2271,12 +2277,15 class InteractiveShell(Configurable, Magic): | |||
|
2271 | 2277 | # buffer attribute as '\n'.join(self.buffer). |
|
2272 | 2278 | self.code_to_run = code |
|
2273 | 2279 | # now actually execute the code object |
|
2274 | if self.runcode(code) == 0: | |
|
2280 | if self.run_code(code) == 0: | |
|
2275 | 2281 | return False |
|
2276 | 2282 | else: |
|
2277 | 2283 | return None |
|
2278 | 2284 | |
|
2279 | def runcode(self, code_obj, post_execute=True): | |
|
2285 | # For backwards compatibility | |
|
2286 | runsource = run_source | |
|
2287 | ||
|
2288 | def run_code(self, code_obj, post_execute=True): | |
|
2280 | 2289 | """Execute a code object. |
|
2281 | 2290 | |
|
2282 | 2291 | When an exception occurs, self.showtraceback() is called to display a |
@@ -2299,14 +2308,14 class InteractiveShell(Configurable, Magic): | |||
|
2299 | 2308 | outflag = 1 # happens in more places, so it's easier as default |
|
2300 | 2309 | try: |
|
2301 | 2310 | try: |
|
2302 | self.hooks.pre_runcode_hook() | |
|
2311 | self.hooks.pre_run_code_hook() | |
|
2303 | 2312 | #rprint('Running code') # dbg |
|
2304 | 2313 | exec code_obj in self.user_global_ns, self.user_ns |
|
2305 | 2314 | finally: |
|
2306 | 2315 | # Reset our crash handler in place |
|
2307 | 2316 | sys.excepthook = old_excepthook |
|
2308 | 2317 | except SystemExit: |
|
2309 | self.resetbuffer() | |
|
2318 | self.reset_buffer() | |
|
2310 | 2319 | self.showtraceback(exception_only=True) |
|
2311 | 2320 | warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1) |
|
2312 | 2321 | except self.custom_exceptions: |
@@ -2339,18 +2348,21 class InteractiveShell(Configurable, Magic): | |||
|
2339 | 2348 | self.code_to_run = None |
|
2340 | 2349 | return outflag |
|
2341 | 2350 | |
|
2351 | # For backwards compatibility | |
|
2352 | runcode = run_code | |
|
2353 | ||
|
2342 | 2354 | def push_line(self, line): |
|
2343 | 2355 | """Push a line to the interpreter. |
|
2344 | 2356 | |
|
2345 | 2357 | The line should not have a trailing newline; it may have |
|
2346 | 2358 | internal newlines. The line is appended to a buffer and the |
|
2347 | interpreter's runsource() method is called with the | |
|
2359 | interpreter's run_source() method is called with the | |
|
2348 | 2360 | concatenated contents of the buffer as source. If this |
|
2349 | 2361 | indicates that the command was executed or invalid, the buffer |
|
2350 | 2362 | is reset; otherwise, the command is incomplete, and the buffer |
|
2351 | 2363 | is left as it was after the line was appended. The return |
|
2352 | 2364 | value is 1 if more input is required, 0 if the line was dealt |
|
2353 | with in some way (this is the same as runsource()). | |
|
2365 | with in some way (this is the same as run_source()). | |
|
2354 | 2366 | """ |
|
2355 | 2367 | |
|
2356 | 2368 | # autoindent management should be done here, and not in the |
@@ -2361,20 +2373,23 class InteractiveShell(Configurable, Magic): | |||
|
2361 | 2373 | #print 'push line: <%s>' % line # dbg |
|
2362 | 2374 | self.buffer.append(line) |
|
2363 | 2375 | full_source = '\n'.join(self.buffer) |
|
2364 | more = self.runsource(full_source, self.filename) | |
|
2376 | more = self.run_source(full_source, self.filename) | |
|
2365 | 2377 | if not more: |
|
2366 | 2378 | self.history_manager.store_inputs('\n'.join(self.buffer_raw), |
|
2367 | 2379 | full_source) |
|
2368 | self.resetbuffer() | |
|
2380 | self.reset_buffer() | |
|
2369 | 2381 | self.execution_count += 1 |
|
2370 | 2382 | return more |
|
2371 | 2383 | |
|
2372 | def resetbuffer(self): | |
|
2384 | def reset_buffer(self): | |
|
2373 | 2385 | """Reset the input buffer.""" |
|
2374 | 2386 | self.buffer[:] = [] |
|
2375 | 2387 | self.buffer_raw[:] = [] |
|
2376 | 2388 | self.input_splitter.reset() |
|
2377 | 2389 | |
|
2390 | # For backwards compatibility | |
|
2391 | resetbuffer = reset_buffer | |
|
2392 | ||
|
2378 | 2393 | def _is_secondary_block_start(self, s): |
|
2379 | 2394 | if not s.endswith(':'): |
|
2380 | 2395 | return False |
@@ -1551,7 +1551,7 Currently the magic system has the following functions:\n""" | |||
|
1551 | 1551 | |
|
1552 | 1552 | stats = None |
|
1553 | 1553 | try: |
|
1554 | self.shell.savehist() | |
|
1554 | self.shell.save_hist() | |
|
1555 | 1555 | |
|
1556 | 1556 | if opts.has_key('p'): |
|
1557 | 1557 | stats = self.magic_prun('',0,opts,arg_lst,prog_ns) |
@@ -1670,7 +1670,7 Currently the magic system has the following functions:\n""" | |||
|
1670 | 1670 | # contained therein. |
|
1671 | 1671 | del sys.modules[main_mod_name] |
|
1672 | 1672 | |
|
1673 | self.shell.reloadhist() | |
|
1673 | self.shell.reload_hist() | |
|
1674 | 1674 | |
|
1675 | 1675 | return stats |
|
1676 | 1676 |
@@ -137,29 +137,30 class ParalleMagic(Plugin): | |||
|
137 | 137 | self._enable_autopx() |
|
138 | 138 | |
|
139 | 139 | def _enable_autopx(self): |
|
140 | """Enable %autopx mode by saving the original runsource and installing | |
|
141 | pxrunsource. | |
|
140 | """Enable %autopx mode by saving the original run_source and installing | |
|
141 | pxrun_source. | |
|
142 | 142 | """ |
|
143 | 143 | if self.active_multiengine_client is None: |
|
144 | 144 | print NO_ACTIVE_MULTIENGINE_CLIENT |
|
145 | 145 | return |
|
146 | 146 | |
|
147 | self._original_runsource = self.shell.runsource | |
|
148 | self.shell.runsource = new.instancemethod( | |
|
149 | self.pxrunsource, self.shell, self.shell.__class__ | |
|
147 | self._original_run_source = self.shell.run_source | |
|
148 | self.shell.run_source = new.instancemethod( | |
|
149 | self.pxrun_source, self.shell, self.shell.__class__ | |
|
150 | 150 | ) |
|
151 | 151 | self.autopx = True |
|
152 | 152 | print "%autopx enabled" |
|
153 | 153 | |
|
154 | 154 | def _disable_autopx(self): |
|
155 |
"""Disable %autopx by restoring the original InteractiveShell.runsource. |
|
|
155 | """Disable %autopx by restoring the original InteractiveShell.run_source. | |
|
156 | """ | |
|
156 | 157 | if self.autopx: |
|
157 | self.shell.runsource = self._original_runsource | |
|
158 | self.shell.run_source = self._original_run_source | |
|
158 | 159 | self.autopx = False |
|
159 | 160 | print "%autopx disabled" |
|
160 | 161 | |
|
161 | def pxrunsource(self, ipself, source, filename="<input>", symbol="single"): | |
|
162 | """A parallel replacement for InteractiveShell.runsource.""" | |
|
162 | def pxrun_source(self, ipself, source, filename="<input>", symbol="single"): | |
|
163 | """A parallel replacement for InteractiveShell.run_source.""" | |
|
163 | 164 | |
|
164 | 165 | try: |
|
165 | 166 | code = ipself.compile(source, filename, symbol) |
@@ -209,10 +209,10 class Kernel(Configurable): | |||
|
209 | 209 | reply_content = {} |
|
210 | 210 | try: |
|
211 | 211 | if silent: |
|
212 | # runcode uses 'exec' mode, so no displayhook will fire, and it | |
|
212 | # run_code 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 | shell.runcode(code) | |
|
215 | shell.run_code(code) | |
|
216 | 216 | else: |
|
217 | 217 | # FIXME: the shell calls the exception handler itself. |
|
218 | 218 | shell._reply_content = None |
General Comments 0
You need to be logged in to leave comments.
Login now