##// END OF EJS Templates
fix %autopx in scripts by calling run_code for each ast node...
MinRK -
Show More
@@ -2211,15 +2211,20 b' class InteractiveShell(Configurable, Magic):'
2211
2211
2212 exec_count = self.execution_count
2212 exec_count = self.execution_count
2213 if to_run_exec:
2213 if to_run_exec:
2214 mod = ast.Module(to_run_exec)
2214 for i, node in enumerate(to_run_exec):
2215 self.code_to_run = code = self.compile(mod, cell_name, "exec")
2215 mod = ast.Module([node])
2216 if self.run_code(code) == 1:
2216 self.code_to_run = code = self.compile(mod, cell_name+str(i), "exec")
2217 return
2217 if self.run_code(code) == 1:
2218
2218 return 1
2219
2219 if to_run_interactive:
2220 if to_run_interactive:
2220 mod = ast.Interactive(to_run_interactive)
2221 for i, node in enumerate(to_run_interactive):
2221 self.code_to_run = code = self.compile(mod, cell_name, "single")
2222 mod = ast.Interactive([node])
2222 return self.run_code(code)
2223 self.code_to_run = code = self.compile(mod, cell_name, "single")
2224 if self.run_code(code) == 1:
2225 return 1
2226
2227 return 0
2223
2228
2224
2229
2225 # PENDING REMOVAL: this method is slated for deletion, once our new
2230 # PENDING REMOVAL: this method is slated for deletion, once our new
@@ -2336,11 +2341,17 b' class InteractiveShell(Configurable, Magic):'
2336 When an exception occurs, self.showtraceback() is called to display a
2341 When an exception occurs, self.showtraceback() is called to display a
2337 traceback.
2342 traceback.
2338
2343
2339 Return value: a flag indicating whether the code to be run completed
2344 Parameters
2340 successfully:
2345 ----------
2346 code_obj : code object
2347 A compiled code object, to be executed
2348 post_execute : bool [default: True]
2349 whether to call post_execute hooks after this particular execution.
2341
2350
2342 - 0: successful execution.
2351 Returns
2343 - 1: an error occurred.
2352 -------
2353 0 : successful execution.
2354 1 : an error occurred.
2344 """
2355 """
2345
2356
2346 # Set our own excepthook in case the user code tries to call it
2357 # Set our own excepthook in case the user code tries to call it
@@ -15,12 +15,10 b''
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 import ast
17 import ast
18 import new
19 import re
18 import re
20
19
21 from IPython.core.plugin import Plugin
20 from IPython.core.plugin import Plugin
22 from IPython.utils.traitlets import Bool, Any, Instance
21 from IPython.utils.traitlets import Bool, Any, Instance
23 from IPython.utils.autoattr import auto_attr
24 from IPython.testing import decorators as testdec
22 from IPython.testing import decorators as testdec
25
23
26 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
@@ -146,10 +144,12 b' class ParalleMagic(Plugin):'
146 print NO_ACTIVE_VIEW
144 print NO_ACTIVE_VIEW
147 return
145 return
148
146
147 # override run_cell and run_code
149 self._original_run_cell = self.shell.run_cell
148 self._original_run_cell = self.shell.run_cell
150 self.shell.run_cell = new.instancemethod(
149 self.shell.run_cell = self.pxrun_cell
151 self.pxrun_cell, self.shell, self.shell.__class__
150 self._original_run_code = self.shell.run_code
152 )
151 self.shell.run_code = self.pxrun_code
152
153 self.autopx = True
153 self.autopx = True
154 print "%autopx enabled"
154 print "%autopx enabled"
155
155
@@ -158,14 +158,43 b' class ParalleMagic(Plugin):'
158 """
158 """
159 if self.autopx:
159 if self.autopx:
160 self.shell.run_cell = self._original_run_cell
160 self.shell.run_cell = self._original_run_cell
161 self.shell.run_code = self._original_run_code
161 self.autopx = False
162 self.autopx = False
162 print "%autopx disabled"
163 print "%autopx disabled"
163
164
164 def pxrun_cell(self, ipself, cell, store_history=True):
165 def _maybe_display_output(self, result):
166 """Maybe display the output of a parallel result.
167
168 If self.active_view.block is True, wait for the result
169 and display the result. Otherwise, this is a noop.
170 """
171 if self.active_view.block:
172 try:
173 result.get()
174 except:
175 self.shell.showtraceback()
176 return True
177 else:
178 targets = self.active_view.targets
179 if isinstance(targets, int):
180 targets = [targets]
181 if targets == 'all':
182 targets = self.active_view.client.ids
183 stdout = [s.rstrip() for s in result.stdout]
184 if any(stdout):
185 for i,eid in enumerate(targets):
186 print '[stdout:%i]'%eid, stdout[i]
187 return False
188
189
190 def pxrun_cell(self, cell, store_history=True):
165 """drop-in replacement for InteractiveShell.run_cell.
191 """drop-in replacement for InteractiveShell.run_cell.
166
192
167 This executes code remotely, instead of in the local namespace.
193 This executes code remotely, instead of in the local namespace.
194
195 See InteractiveShell.run_cell for details.
168 """
196 """
197 ipself = self.shell
169 raw_cell = cell
198 raw_cell = cell
170 with ipself.builtin_trap:
199 with ipself.builtin_trap:
171 cell = ipself.prefilter_manager.prefilter_lines(cell)
200 cell = ipself.prefilter_manager.prefilter_lines(cell)
@@ -187,6 +216,7 b' class ParalleMagic(Plugin):'
187 ipself.execution_count += 1
216 ipself.execution_count += 1
188 return None
217 return None
189 except NameError:
218 except NameError:
219 # ignore name errors, because we don't know the remote keys
190 pass
220 pass
191
221
192 if store_history:
222 if store_history:
@@ -195,7 +225,6 b' class ParalleMagic(Plugin):'
195 ipself.history_manager.store_output(ipself.execution_count)
225 ipself.history_manager.store_output(ipself.execution_count)
196 # Each cell is a *single* input, regardless of how many lines it has
226 # Each cell is a *single* input, regardless of how many lines it has
197 ipself.execution_count += 1
227 ipself.execution_count += 1
198 print cell
199
228
200 if re.search(r'get_ipython\(\)\.magic\(u?"%?autopx', cell):
229 if re.search(r'get_ipython\(\)\.magic\(u?"%?autopx', cell):
201 self._disable_autopx()
230 self._disable_autopx()
@@ -206,23 +235,32 b' class ParalleMagic(Plugin):'
206 except:
235 except:
207 ipself.showtraceback()
236 ipself.showtraceback()
208 return False
237 return False
209
238 else:
210 if self.active_view.block:
239 return self._maybe_display_output(result)
211 try:
240
212 result.get()
241 def pxrun_code(self, code_obj, post_execute=True):
213 except:
242 """drop-in replacement for InteractiveShell.run_code.
214 ipself.showtraceback()
243
215 else:
244 This executes code remotely, instead of in the local namespace.
216 targets = self.active_view.targets
245
217 if isinstance(targets, int):
246 See InteractiveShell.run_code for details.
218 targets = [targets]
247 """
219 if targets == 'all':
248 ipself = self.shell
220 targets = self.active_view.client.ids
249 # check code object for the autopx magic
221 stdout = [s.rstrip() for s in result.stdout]
250 if 'get_ipython' in code_obj.co_names and 'magic' in code_obj.co_names and \
222 if any(stdout):
251 any( [ isinstance(c, basestring) and 'autopx' in c for c in code_obj.co_consts ]):
223 for i,eid in enumerate(targets):
252 self._disable_autopx()
224 print '[stdout:%i]'%eid, stdout[i]
225 return False
253 return False
254 else:
255 try:
256 result = self.active_view.execute(code_obj, block=False)
257 except:
258 ipself.showtraceback()
259 return False
260 else:
261 return self._maybe_display_output(result)
262
263
226
264
227
265
228 _loaded = False
266 _loaded = False
@@ -765,11 +765,11 b' class DirectView(View):'
765 print "The IPython parallel magics (%result, %px, %autopx) only work within IPython."
765 print "The IPython parallel magics (%result, %px, %autopx) only work within IPython."
766 else:
766 else:
767 pmagic = ip.plugin_manager.get_plugin('parallelmagic')
767 pmagic = ip.plugin_manager.get_plugin('parallelmagic')
768 if pmagic is not None:
768 if pmagic is None:
769 pmagic.active_view = self
769 ip.magic_load_ext('parallelmagic')
770 else:
770 pmagic = ip.plugin_manager.get_plugin('parallelmagic')
771 print "You must first load the parallelmagic extension " \
771
772 "by doing '%load_ext parallelmagic'"
772 pmagic.active_view = self
773
773
774
774
775 @testdec.skip_doctest
775 @testdec.skip_doctest
General Comments 0
You need to be logged in to leave comments. Login now