From 085296155a6920051bb1a2218f62928059c74451 2013-10-03 19:21:38 From: Thomas Kluyver Date: 2013-10-03 19:21:38 Subject: [PATCH] Merge pull request #4338 from minrk/sync_imports adjust default value of level in sync_imports --- diff --git a/IPython/parallel/client/view.py b/IPython/parallel/client/view.py index 809b1ed..7bd4326 100644 --- a/IPython/parallel/client/view.py +++ b/IPython/parallel/client/view.py @@ -457,7 +457,7 @@ class DirectView(View): else: user_ns[name] = sys.modules[name] - def view_import(name, globals={}, locals={}, fromlist=[], level=-1): + def view_import(name, globals={}, locals={}, fromlist=[], level=0): """the drop-in replacement for __import__, that optionally imports locally as well. """ @@ -478,7 +478,7 @@ class DirectView(View): imp.release_lock() key = name+':'+','.join(fromlist or []) - if level == -1 and key not in modules: + if level <= 0 and key not in modules: modules.add(key) if not quiet: if fromlist: diff --git a/IPython/parallel/tests/test_view.py b/IPython/parallel/tests/test_view.py index 3cc704e..c9b6e0f 100644 --- a/IPython/parallel/tests/test_view.py +++ b/IPython/parallel/tests/test_view.py @@ -805,4 +805,30 @@ class TestView(ClusterTestCase): view = self.client[-1] tup = view.apply_sync(echoxy, point(1, 2)) self.assertEqual(tup, (2,1)) + + def test_sync_imports(self): + view = self.client[-1] + with capture_output() as io: + with view.sync_imports(): + import IPython + self.assertIn("IPython", io.stdout) + + @interactive + def find_ipython(): + return 'IPython' in globals() + + assert view.apply_sync(find_ipython) + + def test_sync_imports_quiet(self): + view = self.client[-1] + with capture_output() as io: + with view.sync_imports(quiet=True): + import IPython + self.assertEqual(io.stdout, '') + + @interactive + def find_ipython(): + return 'IPython' in globals() + + assert view.apply_sync(find_ipython)