From e2cd7aca43afd18d8294816d39ce800366eab8d3 2022-10-16 13:01:42 From: Matthias Bussonnier Date: 2022-10-16 13:01:42 Subject: [PATCH] MAINT: convert ip.magic to to run_line magic in many places. Too many warnings during test suite make it hard to spot issues. --- diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index 1c793ca..509dd66 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -84,7 +84,7 @@ def test_extract_symbols_raises_exception_with_non_python_code(): def test_magic_not_found(): # magic not found raises UsageError with pytest.raises(UsageError): - _ip.magic('doesntexist') + _ip.run_line_magic("doesntexist", "") # ensure result isn't success when a magic isn't found result = _ip.run_cell('%doesntexist') @@ -116,13 +116,14 @@ def test_config(): magic. """ ## should not raise. - _ip.magic('config') + _ip.run_line_magic("config", "") + def test_config_available_configs(): """ test that config magic prints available configs in unique and sorted order. """ with capture_output() as captured: - _ip.magic('config') + _ip.run_line_magic("config", "") stdout = captured.stdout config_classes = stdout.strip().split('\n')[1:] @@ -131,7 +132,7 @@ def test_config_available_configs(): def test_config_print_class(): """ test that config with a classname prints the class's options. """ with capture_output() as captured: - _ip.magic('config TerminalInteractiveShell') + _ip.run_line_magic("config", "TerminalInteractiveShell") stdout = captured.stdout assert re.match( @@ -144,7 +145,7 @@ def test_rehashx(): _ip.alias_manager.clear_aliases() del _ip.db['syscmdlist'] - _ip.magic('rehashx') + _ip.run_line_magic("rehashx", "") # Practically ALL ipython development systems will have more than 10 aliases assert len(_ip.alias_manager.aliases) > 10 @@ -277,11 +278,11 @@ def test_macro(): cmds = ["a=1", "def b():\n return a**2", "print(a,b())"] for i, cmd in enumerate(cmds, start=1): ip.history_manager.store_inputs(i, cmd) - ip.magic("macro test 1-3") + ip.run_line_magic("macro", "test 1-3") assert ip.user_ns["test"].value == "\n".join(cmds) + "\n" # List macros - assert "test" in ip.magic("macro") + assert "test" in ip.run_line_magic("macro", "") def test_macro_run(): @@ -302,7 +303,7 @@ def test_magic_magic(): """Test %magic""" ip = get_ipython() with capture_output() as captured: - ip.magic("magic") + ip.run_line_magic("magic", "") stdout = captured.stdout assert "%magic" in stdout @@ -316,7 +317,7 @@ def test_numpy_reset_array_undec(): _ip.ex("import numpy as np") _ip.ex("a = np.empty(2)") assert "a" in _ip.user_ns - _ip.magic("reset -f array") + _ip.run_line_magic("reset", "-f array") assert "a" not in _ip.user_ns @@ -326,7 +327,7 @@ def test_reset_out(): # test '%reset -f out', make an Out prompt _ip.run_cell("parrot", store_history=True) assert "dead" in [_ip.user_ns[x] for x in ("_", "__", "___")] - _ip.magic("reset -f out") + _ip.run_line_magic("reset", "-f out") assert "dead" not in [_ip.user_ns[x] for x in ("_", "__", "___")] assert len(_ip.user_ns["Out"]) == 0 @@ -336,7 +337,7 @@ def test_reset_in(): # test '%reset -f in' _ip.run_cell("parrot", store_history=True) assert "parrot" in [_ip.user_ns[x] for x in ("_i", "_ii", "_iii")] - _ip.magic("%reset -f in") + _ip.run_line_magic("reset", "-f in") assert "parrot" not in [_ip.user_ns[x] for x in ("_i", "_ii", "_iii")] assert len(set(_ip.user_ns["In"])) == 1 @@ -344,10 +345,10 @@ def test_reset_in(): def test_reset_dhist(): "Test '%reset dhist' magic" _ip.run_cell("tmp = [d for d in _dh]") # copy before clearing - _ip.magic("cd " + os.path.dirname(pytest.__file__)) - _ip.magic("cd -") + _ip.run_line_magic("cd", os.path.dirname(pytest.__file__)) + _ip.run_line_magic("cd", "-") assert len(_ip.user_ns["_dh"]) > 0 - _ip.magic("reset -f dhist") + _ip.run_line_magic("reset", "-f dhist") assert len(_ip.user_ns["_dh"]) == 0 _ip.run_cell("_dh = [d for d in tmp]") # restore @@ -472,8 +473,8 @@ def test_time_local_ns(): def test_doctest_mode(): "Toggle doctest_mode twice, it should be a no-op and run without error" - _ip.magic('doctest_mode') - _ip.magic('doctest_mode') + _ip.run_line_magic("doctest_mode", "") + _ip.run_line_magic("doctest_mode", "") def test_parse_options(): @@ -498,7 +499,9 @@ def test_parse_options_preserve_non_option_string(): def test_run_magic_preserve_code_block(): """Test to assert preservation of non-option part of magic-block, while running magic.""" _ip.user_ns["spaces"] = [] - _ip.magic("timeit -n1 -r1 spaces.append([s.count(' ') for s in ['document']])") + _ip.run_line_magic( + "timeit", "-n1 -r1 spaces.append([s.count(' ') for s in ['document']])" + ) assert _ip.user_ns["spaces"] == [[0]] @@ -509,13 +512,13 @@ def test_dirops(): startdir = os.getcwd() ipdir = os.path.realpath(_ip.ipython_dir) try: - _ip.magic('cd "%s"' % ipdir) + _ip.run_line_magic("cd", '"%s"' % ipdir) assert curpath() == ipdir - _ip.magic('cd -') + _ip.run_line_magic("cd", "-") assert curpath() == startdir - _ip.magic('pushd "%s"' % ipdir) + _ip.run_line_magic("pushd", '"%s"' % ipdir) assert curpath() == ipdir - _ip.magic('popd') + _ip.run_line_magic("popd", "") assert curpath() == startdir finally: os.chdir(startdir) @@ -542,7 +545,7 @@ def test_xmode(): # Calling xmode three times should be a no-op xmode = _ip.InteractiveTB.mode for i in range(4): - _ip.magic("xmode") + _ip.run_line_magic("xmode", "") assert _ip.InteractiveTB.mode == xmode def test_reset_hard(): @@ -557,7 +560,7 @@ def test_reset_hard(): _ip.run_cell("a") assert monitor == [] - _ip.magic("reset -f") + _ip.run_line_magic("reset", "-f") assert monitor == [1] class TestXdel(tt.TempFileMixin): @@ -570,14 +573,14 @@ class TestXdel(tt.TempFileMixin): "a = A()\n") self.mktmp(src) # %run creates some hidden references... - _ip.magic("run %s" % self.fname) + _ip.run_line_magic("run", "%s" % self.fname) # ... as does the displayhook. _ip.run_cell("a") monitor = _ip.user_ns["A"].monitor assert monitor == [] - _ip.magic("xdel a") + _ip.run_line_magic("xdel", "a") # Check that a's __del__ method has been called. gc.collect(0) @@ -614,7 +617,7 @@ def test_whos(): def __repr__(self): raise Exception() _ip.user_ns['a'] = A() - _ip.magic("whos") + _ip.run_line_magic("whos", "") def doctest_precision(): """doctest for %precision @@ -655,12 +658,12 @@ def test_psearch(): def test_timeit_shlex(): """test shlex issues with timeit (#1109)""" _ip.ex("def f(*a,**kw): pass") - _ip.magic('timeit -n1 "this is a bug".count(" ")') - _ip.magic('timeit -r1 -n1 f(" ", 1)') - _ip.magic('timeit -r1 -n1 f(" ", 1, " ", 2, " ")') - _ip.magic('timeit -r1 -n1 ("a " + "b")') - _ip.magic('timeit -r1 -n1 f("a " + "b")') - _ip.magic('timeit -r1 -n1 f("a " + "b ")') + _ip.run_line_magic("timeit", '-n1 "this is a bug".count(" ")') + _ip.run_line_magic("timeit", '-r1 -n1 f(" ", 1)') + _ip.run_line_magic("timeit", '-r1 -n1 f(" ", 1, " ", 2, " ")') + _ip.run_line_magic("timeit", '-r1 -n1 ("a " + "b")') + _ip.run_line_magic("timeit", '-r1 -n1 f("a " + "b")') + _ip.run_line_magic("timeit", '-r1 -n1 f("a " + "b ")') def test_timeit_special_syntax(): @@ -738,9 +741,9 @@ def test_extension(): try: _ip.user_ns.pop('arq', None) invalidate_caches() # Clear import caches - _ip.magic("load_ext daft_extension") + _ip.run_line_magic("load_ext", "daft_extension") assert _ip.user_ns["arq"] == 185 - _ip.magic("unload_ext daft_extension") + _ip.run_line_magic("unload_ext", "daft_extension") assert 'arq' not in _ip.user_ns finally: sys.path.remove(daft_path) @@ -755,17 +758,17 @@ def test_notebook_export_json(): _ip.history_manager.store_inputs(i, cmd) with TemporaryDirectory() as td: outfile = os.path.join(td, "nb.ipynb") - _ip.magic("notebook %s" % outfile) + _ip.run_line_magic("notebook", "%s" % outfile) class TestEnv(TestCase): def test_env(self): - env = _ip.magic("env") + env = _ip.run_line_magic("env", "") self.assertTrue(isinstance(env, dict)) def test_env_secret(self): - env = _ip.magic("env") + env = _ip.run_line_magic("env", "") hidden = "" with mock.patch.dict( os.environ, @@ -776,35 +779,35 @@ class TestEnv(TestCase): "VAR": "abc" } ): - env = _ip.magic("env") + env = _ip.run_line_magic("env", "") assert env["API_KEY"] == hidden assert env["SECRET_THING"] == hidden assert env["JUPYTER_TOKEN"] == hidden assert env["VAR"] == "abc" def test_env_get_set_simple(self): - env = _ip.magic("env var val1") + env = _ip.run_line_magic("env", "var val1") self.assertEqual(env, None) - self.assertEqual(os.environ['var'], 'val1') - self.assertEqual(_ip.magic("env var"), 'val1') - env = _ip.magic("env var=val2") + self.assertEqual(os.environ["var"], "val1") + self.assertEqual(_ip.run_line_magic("env", "var"), "val1") + env = _ip.run_line_magic("env", "var=val2") self.assertEqual(env, None) self.assertEqual(os.environ['var'], 'val2') def test_env_get_set_complex(self): - env = _ip.magic("env var 'val1 '' 'val2") + env = _ip.run_line_magic("env", "var 'val1 '' 'val2") self.assertEqual(env, None) self.assertEqual(os.environ['var'], "'val1 '' 'val2") - self.assertEqual(_ip.magic("env var"), "'val1 '' 'val2") - env = _ip.magic('env var=val2 val3="val4') + self.assertEqual(_ip.run_line_magic("env", "var"), "'val1 '' 'val2") + env = _ip.run_line_magic("env", 'var=val2 val3="val4') self.assertEqual(env, None) self.assertEqual(os.environ['var'], 'val2 val3="val4') def test_env_set_bad_input(self): - self.assertRaises(UsageError, lambda: _ip.magic("set_env var")) + self.assertRaises(UsageError, lambda: _ip.run_line_magic("set_env", "var")) def test_env_set_whitespace(self): - self.assertRaises(UsageError, lambda: _ip.magic("env var A=B")) + self.assertRaises(UsageError, lambda: _ip.run_line_magic("env", "var A=B")) class CellMagicTestCase(TestCase): @@ -1308,7 +1311,7 @@ def test_ls_magic(): ip = get_ipython() json_formatter = ip.display_formatter.formatters['application/json'] json_formatter.enabled = True - lsmagic = ip.magic('lsmagic') + lsmagic = ip.run_line_magic("lsmagic", "") with warnings.catch_warnings(record=True) as w: j = json_formatter(lsmagic) assert sorted(j) == ["cell", "line"] @@ -1358,16 +1361,16 @@ def test_logging_magic_not_quiet(): def test_time_no_var_expand(): - _ip.user_ns['a'] = 5 - _ip.user_ns['b'] = [] - _ip.magic('time b.append("{a}")') - assert _ip.user_ns['b'] == ['{a}'] + _ip.user_ns["a"] = 5 + _ip.user_ns["b"] = [] + _ip.run_line_magic("time", 'b.append("{a}")') + assert _ip.user_ns["b"] == ["{a}"] # this is slow, put at the end for local testing. def test_timeit_arguments(): "Test valid timeit arguments, should not cause SyntaxError (GH #1269)" - _ip.magic("timeit -n1 -r1 a=('#')") + _ip.run_line_magic("timeit", "-n1 -r1 a=('#')") MINIMAL_LAZY_MAGIC = """ @@ -1442,7 +1445,7 @@ def test_run_module_from_import_hook(): sys.meta_path.insert(0, MyTempImporter()) with capture_output() as captured: - _ip.magic("run -m my_tmp") + _ip.run_line_magic("run", "-m my_tmp") _ip.run_cell("import my_tmp") output = "Loaded my_tmp\nI just ran a script\nLoaded my_tmp\n" diff --git a/IPython/core/tests/test_run.py b/IPython/core/tests/test_run.py index ae20ce6..e27ba2c 100644 --- a/IPython/core/tests/test_run.py +++ b/IPython/core/tests/test_run.py @@ -180,13 +180,13 @@ class TestMagicRunPass(tt.TempFileMixin): _ip = get_ipython() # This fails on Windows if self.tmpfile.name has spaces or "~" in it. # See below and ticket https://bugs.launchpad.net/bugs/366353 - _ip.magic('run %s' % self.fname) + _ip.run_line_magic("run", self.fname) def run_tmpfile_p(self): _ip = get_ipython() # This fails on Windows if self.tmpfile.name has spaces or "~" in it. # See below and ticket https://bugs.launchpad.net/bugs/366353 - _ip.magic('run -p %s' % self.fname) + _ip.run_line_magic("run", "-p %s" % self.fname) def test_builtins_id(self): """Check that %run doesn't damage __builtins__ """ @@ -216,20 +216,20 @@ class TestMagicRunPass(tt.TempFileMixin): def test_run_debug_twice(self): # https://github.com/ipython/ipython/issues/10028 _ip = get_ipython() - with tt.fake_input(['c']): - _ip.magic('run -d %s' % self.fname) - with tt.fake_input(['c']): - _ip.magic('run -d %s' % self.fname) + with tt.fake_input(["c"]): + _ip.run_line_magic("run", "-d %s" % self.fname) + with tt.fake_input(["c"]): + _ip.run_line_magic("run", "-d %s" % self.fname) def test_run_debug_twice_with_breakpoint(self): """Make a valid python temp file.""" _ip = get_ipython() - with tt.fake_input(['b 2', 'c', 'c']): - _ip.magic('run -d %s' % self.fname) + with tt.fake_input(["b 2", "c", "c"]): + _ip.run_line_magic("run", "-d %s" % self.fname) - with tt.fake_input(['c']): - with tt.AssertNotPrints('KeyError'): - _ip.magic('run -d %s' % self.fname) + with tt.fake_input(["c"]): + with tt.AssertNotPrints("KeyError"): + _ip.run_line_magic("run", "-d %s" % self.fname) class TestMagicRunSimple(tt.TempFileMixin): @@ -239,7 +239,7 @@ class TestMagicRunSimple(tt.TempFileMixin): src = ("class foo: pass\n" "def f(): return foo()") self.mktmp(src) - _ip.magic("run %s" % self.fname) + _ip.run_line_magic("run", str(self.fname)) _ip.run_cell("t = isinstance(f(), foo)") assert _ip.user_ns["t"] is True @@ -277,7 +277,7 @@ class TestMagicRunSimple(tt.TempFileMixin): " break\n" % ("run " + empty.fname) ) self.mktmp(src) - _ip.magic("run %s" % self.fname) + _ip.run_line_magic("run", str(self.fname)) _ip.run_cell("ip == get_ipython()") assert _ip.user_ns["i"] == 4 @@ -288,8 +288,8 @@ class TestMagicRunSimple(tt.TempFileMixin): with tt.TempFileMixin() as empty: empty.mktmp("") - _ip.magic("run %s" % self.fname) - _ip.magic("run %s" % empty.fname) + _ip.run_line_magic("run", self.fname) + _ip.run_line_magic("run", empty.fname) assert _ip.user_ns["afunc"]() == 1 def test_tclass(self): @@ -323,23 +323,23 @@ tclass.py: deleting object: C-third self.mktmp(src) _ip.run_cell("zz = 23") try: - _ip.magic("run -i %s" % self.fname) + _ip.run_line_magic("run", "-i %s" % self.fname) assert _ip.user_ns["yy"] == 23 finally: - _ip.magic('reset -f') + _ip.run_line_magic("reset", "-f") _ip.run_cell("zz = 23") try: - _ip.magic("run -i %s" % self.fname) + _ip.run_line_magic("run", "-i %s" % self.fname) assert _ip.user_ns["yy"] == 23 finally: - _ip.magic('reset -f') + _ip.run_line_magic("reset", "-f") def test_unicode(self): """Check that files in odd encodings are accepted.""" mydir = os.path.dirname(__file__) - na = os.path.join(mydir, 'nonascii.py') - _ip.magic('run "%s"' % na) + na = os.path.join(mydir, "nonascii.py") + _ip.run_line_magic("run", "%r" % na) assert _ip.user_ns["u"] == "Ўт№Ф" def test_run_py_file_attribute(self): @@ -347,9 +347,9 @@ tclass.py: deleting object: C-third src = "t = __file__\n" self.mktmp(src) _missing = object() - file1 = _ip.user_ns.get('__file__', _missing) - _ip.magic('run %s' % self.fname) - file2 = _ip.user_ns.get('__file__', _missing) + file1 = _ip.user_ns.get("__file__", _missing) + _ip.run_line_magic("run", self.fname) + file2 = _ip.user_ns.get("__file__", _missing) # Check that __file__ was equal to the filename in the script's # namespace. @@ -363,9 +363,9 @@ tclass.py: deleting object: C-third src = "t = __file__\n" self.mktmp(src, ext='.ipy') _missing = object() - file1 = _ip.user_ns.get('__file__', _missing) - _ip.magic('run %s' % self.fname) - file2 = _ip.user_ns.get('__file__', _missing) + file1 = _ip.user_ns.get("__file__", _missing) + _ip.run_line_magic("run", self.fname) + file2 = _ip.user_ns.get("__file__", _missing) # Check that __file__ was equal to the filename in the script's # namespace. @@ -378,18 +378,18 @@ tclass.py: deleting object: C-third """ Test that %run -t -N does not raise a TypeError for N > 1.""" src = "pass" self.mktmp(src) - _ip.magic('run -t -N 1 %s' % self.fname) - _ip.magic('run -t -N 10 %s' % self.fname) + _ip.run_line_magic("run", "-t -N 1 %s" % self.fname) + _ip.run_line_magic("run", "-t -N 10 %s" % self.fname) def test_ignore_sys_exit(self): """Test the -e option to ignore sys.exit()""" src = "import sys; sys.exit(1)" self.mktmp(src) - with tt.AssertPrints('SystemExit'): - _ip.magic('run %s' % self.fname) + with tt.AssertPrints("SystemExit"): + _ip.run_line_magic("run", self.fname) - with tt.AssertNotPrints('SystemExit'): - _ip.magic('run -e %s' % self.fname) + with tt.AssertNotPrints("SystemExit"): + _ip.run_line_magic("run", "-e %s" % self.fname) def test_run_nb(self): """Test %run notebook.ipynb""" @@ -404,7 +404,7 @@ tclass.py: deleting object: C-third src = writes(nb, version=4) self.mktmp(src, ext='.ipynb') - _ip.magic("run %s" % self.fname) + _ip.run_line_magic("run", self.fname) assert _ip.user_ns["answer"] == 42 @@ -478,12 +478,16 @@ class TestMagicRunWithPackage(unittest.TestCase): sys.path[:] = [p for p in sys.path if p != self.tempdir.name] self.tempdir.cleanup() - def check_run_submodule(self, submodule, opts=''): - _ip.user_ns.pop('x', None) - _ip.magic('run {2} -m {0}.{1}'.format(self.package, submodule, opts)) - self.assertEqual(_ip.user_ns['x'], self.value, - 'Variable `x` is not loaded from module `{0}`.' - .format(submodule)) + def check_run_submodule(self, submodule, opts=""): + _ip.user_ns.pop("x", None) + _ip.run_line_magic( + "run", "{2} -m {0}.{1}".format(self.package, submodule, opts) + ) + self.assertEqual( + _ip.user_ns["x"], + self.value, + "Variable `x` is not loaded from module `{0}`.".format(submodule), + ) def test_run_submodule_with_absolute_import(self): self.check_run_submodule('absolute') @@ -533,17 +537,17 @@ def test_run__name__(): f.write("q = __name__") _ip.user_ns.pop("q", None) - _ip.magic("run {}".format(path)) + _ip.run_line_magic("run", "{}".format(path)) assert _ip.user_ns.pop("q") == "__main__" - _ip.magic("run -n {}".format(path)) + _ip.run_line_magic("run", "-n {}".format(path)) assert _ip.user_ns.pop("q") == "foo" try: - _ip.magic("run -i -n {}".format(path)) + _ip.run_line_magic("run", "-i -n {}".format(path)) assert _ip.user_ns.pop("q") == "foo" finally: - _ip.magic('reset -f') + _ip.run_line_magic("reset", "-f") def test_run_tb(): @@ -563,7 +567,7 @@ def test_run_tb(): ) ) with capture_output() as io: - _ip.magic('run {}'.format(path)) + _ip.run_line_magic("run", "{}".format(path)) out = io.stdout assert "execfile" not in out assert "RuntimeError" in out