From 13e486ebe73887527d34fea748233d9fce1268cc 2019-11-22 17:44:51 From: Matthias Bussonnier Date: 2019-11-22 17:44:51 Subject: [PATCH] Add a failing test if we %run something with multiprocessing --- diff --git a/IPython/core/tests/test_run.py b/IPython/core/tests/test_run.py index 42f2e5c..38d71b3 100644 --- a/IPython/core/tests/test_run.py +++ b/IPython/core/tests/test_run.py @@ -539,6 +539,35 @@ def test_run_tb(): del ip.user_ns['bar'] del ip.user_ns['foo'] + +def test_multiprocessing_run(): + """Set we can run mutiprocesgin without messing up up main namespace + + Note that import `nose.tools as nt` mdify the value s + sys.module['__mp_main__'] so wee need to temporarily set it to None to test + the issue. + """ + with TemporaryDirectory() as td: + mpm = sys.modules.get('__mp_main__') + assert mpm is not None + sys.modules['__mp_main__'] = None + try: + path = pjoin(td, 'test.py') + with open(path, 'w') as f: + f.write("import multiprocessing\nprint('hoy')") + with capture_output() as io: + _ip.run_line_magic('run', path) + _ip.run_cell("i_m_undefined") + out = io.stdout + nt.assert_in("hoy", out) + nt.assert_not_in("AttributeError", out) + nt.assert_in("NameError", out) + nt.assert_equal(out.count("---->"), 1) + except: + raise + finally: + sys.modules['__mp_main__'] = mpm + @dec.knownfailureif(sys.platform == 'win32', "writes to io.stdout aren't captured on Windows") def test_script_tb(): """Test traceback offset in `ipython script.py`"""