Permalink
Browse files

fix %px magic output for single target

parallelmagic._maybe_display expects result.stdout to be a list,
but did not handle the single-result case where it is the stdout string
itself, thus printing the first character rather than the whole string.

test included
  • Loading branch information...
1 parent 2676dc3 commit ee1d9ce043b361ac7b0366cb8c2559cd8ef70726 @minrk committed Jun 7, 2011
Showing with 16 additions and 9 deletions.
  1. +15 −9 IPython/extensions/parallelmagic.py
  2. +1 −0 IPython/parallel/tests/test_view.py
@@ -92,7 +92,7 @@ def magic_px(self, ipself, parameter_s=''):
Then you can do the following::
In [24]: %px a = 5
- Parallel execution on engines: all
+ Parallel execution on engine(s): all
Out[24]:
<Results List>
[0] In [7]: a = 5
@@ -102,7 +102,7 @@ def magic_px(self, ipself, parameter_s=''):
if self.active_view is None:
print NO_ACTIVE_VIEW
return
- print "Parallel execution on engines: %s" % self.active_view.targets
+ print "Parallel execution on engine(s): %s" % self.active_view.targets
result = self.active_view.execute(parameter_s, block=False)
if self.active_view.block:
result.get()
@@ -125,9 +125,9 @@ def magic_autopx(self, ipself, parameter_s=''):
%autopx to enabled
In [26]: a = 10
- Parallel execution on engines: [0,1,2,3]
+ Parallel execution on engine(s): [0,1,2,3]
In [27]: print a
- Parallel execution on engines: [0,1,2,3]
+ Parallel execution on engine(s): [0,1,2,3]
[stdout:0] 10
[stdout:1] 10
[stdout:2] 10
@@ -174,15 +174,21 @@ def _maybe_display_output(self, result):
If self.active_view.block is True, wait for the result
and display the result. Otherwise, this is a noop.
"""
+ if isinstance(result.stdout, basestring):
+ # single result
+ stdouts = [result.stdout.rstrip()]
+ else:
+ stdouts = [s.rstrip() for s in result.stdout]
+
targets = self.active_view.targets
if isinstance(targets, int):
targets = [targets]
- if targets == 'all':
+ elif targets == 'all':
targets = self.active_view.client.ids
- stdout = [s.rstrip() for s in result.stdout]
- if any(stdout):
- for i,eid in enumerate(targets):
- print '[stdout:%i]'%eid, stdout[i]
+
+ if any(stdouts):
+ for eid,stdout in zip(targets, stdouts):
+ print '[stdout:%i]'%eid, stdout
def pxrun_cell(self, raw_cell, store_history=True):
@@ -315,6 +315,7 @@ def test_magic_px_blocking(self):
sys.stdout = savestdout
sio.read()
self.assertTrue('[stdout:%i]'%v.targets in sio.buf)
+ self.assertTrue(sio.buf.rstrip().endswith('10'))
self.assertRaisesRemote(ZeroDivisionError, ip.magic_px, '1/0')
def test_magic_px_nonblocking(self):

0 comments on commit ee1d9ce

Please sign in to comment.