##// END OF EJS Templates
add silent kwarg to run_cell...
MinRK -
Show More
@@ -2429,7 +2429,7 b' class InteractiveShell(SingletonConfigurable, Magic):'
2429 2429 self.showtraceback()
2430 2430 warn('Unknown failure executing module: <%s>' % mod_name)
2431 2431
2432 def run_cell(self, raw_cell, store_history=False):
2432 def run_cell(self, raw_cell, store_history=False, silent=False):
2433 2433 """Run a complete IPython cell.
2434 2434
2435 2435 Parameters
@@ -2440,9 +2440,15 b' class InteractiveShell(SingletonConfigurable, Magic):'
2440 2440 If True, the raw and translated cell will be stored in IPython's
2441 2441 history. For user code calling back into IPython's machinery, this
2442 2442 should be set to False.
2443 silent : bool
2444 If True, avoid side-effets, such as implicit displayhooks, history,
2445 and logging. silent=True forces store_history=False.
2443 2446 """
2444 2447 if (not raw_cell) or raw_cell.isspace():
2445 2448 return
2449
2450 if silent:
2451 store_history = False
2446 2452
2447 2453 for line in raw_cell.splitlines():
2448 2454 self.input_splitter.push(line)
@@ -2467,8 +2473,8 b' class InteractiveShell(SingletonConfigurable, Magic):'
2467 2473 if store_history:
2468 2474 self.history_manager.store_inputs(self.execution_count,
2469 2475 cell, raw_cell)
2470
2471 self.logger.log(cell, raw_cell)
2476 if not silent:
2477 self.logger.log(cell, raw_cell)
2472 2478
2473 2479 if not prefilter_failed:
2474 2480 # don't run if prefilter failed
@@ -2488,12 +2494,16 b' class InteractiveShell(SingletonConfigurable, Magic):'
2488 2494 if store_history:
2489 2495 self.execution_count += 1
2490 2496 return None
2491
2497
2498 interactivity = "none" if silent else "last_expr"
2492 2499 self.run_ast_nodes(code_ast.body, cell_name,
2493 interactivity="last_expr")
2494
2500 interactivity=interactivity)
2501
2495 2502 # Execute any registered post-execution functions.
2496 for func, status in self._post_execute.iteritems():
2503 # unless we are silent
2504 post_exec = [] if silent else self._post_execute.iteritems()
2505
2506 for func, status in post_exec:
2497 2507 if self.disable_failing_post_execute and not status:
2498 2508 continue
2499 2509 try:
@@ -254,6 +254,61 b' class InteractiveShellTestCase(unittest.TestCase):'
254 254 self.assertEqual(ip.var_expand(u"{asdf}"), u"{asdf}")
255 255 # ZeroDivisionError
256 256 self.assertEqual(ip.var_expand(u"{1/0}"), u"{1/0}")
257
258 def test_silent_nopostexec(self):
259 """run_cell(silent=True) doesn't invoke post-exec funcs"""
260 ip = get_ipython()
261
262 d = dict(called=False)
263 def set_called():
264 d['called'] = True
265
266 ip.register_post_execute(set_called)
267 ip.run_cell("1", silent=True)
268 self.assertFalse(d['called'])
269 # double-check that non-silent exec did what we expected
270 # silent to avoid
271 ip.run_cell("1")
272 self.assertTrue(d['called'])
273 # remove post-exec
274 ip._post_execute.pop(set_called)
275
276 def test_silent_noadvance(self):
277 """run_cell(silent=True) doesn't advance execution_count"""
278 ip = get_ipython()
279
280 ec = ip.execution_count
281 # silent should force store_history=False
282 ip.run_cell("1", store_history=True, silent=True)
283
284 self.assertEquals(ec, ip.execution_count)
285 # double-check that non-silent exec did what we expected
286 # silent to avoid
287 ip.run_cell("1", store_history=True)
288 self.assertEquals(ec+1, ip.execution_count)
289
290 def test_silent_nodisplayhook(self):
291 """run_cell(silent=True) doesn't trigger displayhook"""
292 ip = get_ipython()
293
294 d = dict(called=False)
295
296 trap = ip.display_trap
297 save_hook = trap.hook
298
299 def failing_hook(*args, **kwargs):
300 d['called'] = True
301
302 try:
303 trap.hook = failing_hook
304 ip.run_cell("1", silent=True)
305 self.assertFalse(d['called'])
306 # double-check that non-silent exec did what we expected
307 # silent to avoid
308 ip.run_cell("1")
309 self.assertTrue(d['called'])
310 finally:
311 trap.hook = save_hook
257 312
258 313 @skipif(sys.version_info[0] >= 3, "softspace removed in py3")
259 314 def test_print_softspace(self):
General Comments 0
You need to be logged in to leave comments. Login now