From ac9ed9c1c4a62bf17eeb3e8887c7fb4b47aeda72 2024-11-01 14:32:07 From: M Bussonnier Date: 2024-11-01 14:32:07 Subject: [PATCH] do not use .magic(...) that has been deprecated for a decade (#14566) --- diff --git a/IPython/core/magics/basic.py b/IPython/core/magics/basic.py index 3e3eb2d..f707bcc 100644 --- a/IPython/core/magics/basic.py +++ b/IPython/core/magics/basic.py @@ -472,7 +472,7 @@ Currently the magic system has the following functions:""", ptformatter.pprint = dstore.rc_pprint disp_formatter.active_types = dstore.rc_active_types - shell.magic('xmode ' + dstore.xmode) + shell.run_line_magic("xmode", dstore.xmode) # mode here is the state before we switch; switch_doctest_mode takes # the mode we're switching to. diff --git a/IPython/core/tests/test_logger.py b/IPython/core/tests/test_logger.py index 10e4620..003f603 100644 --- a/IPython/core/tests/test_logger.py +++ b/IPython/core/tests/test_logger.py @@ -21,7 +21,7 @@ def test_logstart_unicode(): logfname = os.path.join(tdir, "test_unicode.log") _ip.run_cell("'abc€'") try: - _ip.magic("logstart -to %s" % logfname) + _ip.run_line_magic("logstart", "-to %s" % logfname) _ip.run_cell("'abc€'") finally: _ip.logger.logstop() diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index c757b9c..036f250 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -827,7 +827,7 @@ def test_prun_special_syntax(): @dec.skipif(execution.profile is None) def test_prun_quotes(): "Test that prun does not clobber string escapes (GH #1302)" - _ip.magic(r"prun -q x = '\t'") + _ip.run_line_magic("prun", r"-q x = '\t'") assert _ip.user_ns["x"] == "\t" @@ -838,7 +838,7 @@ def test_extension(): print(' ', p) print('CWD', os.getcwd()) - pytest.raises(ImportError, _ip.magic, "load_ext daft_extension") + pytest.raises(ImportError, _ip.run_line_magic, "load_ext", "daft_extension") daft_path = os.path.join(os.path.dirname(__file__), "daft_extension") sys.path.insert(0, daft_path) try: diff --git a/IPython/core/tests/test_run.py b/IPython/core/tests/test_run.py index 23337b3..971f8e3 100644 --- a/IPython/core/tests/test_run.py +++ b/IPython/core/tests/test_run.py @@ -271,10 +271,10 @@ class TestMagicRunSimple(tt.TempFileMixin): "ip = get_ipython()\n" "for i in range(5):\n" " try:\n" - " ip.magic(%r)\n" + " ip.run_line_magic(%r, %r)\n" " except NameError as e:\n" " print(i)\n" - " break\n" % ("run " + empty.fname) + " break\n" % ("run", empty.fname) ) self.mktmp(src) _ip.run_line_magic("run", str(self.fname)) @@ -339,7 +339,7 @@ tclass.py: deleting object: C-third """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) + _ip.run_line_magic("run", na) assert _ip.user_ns["u"] == "Ўт№Ф" def test_run_py_file_attribute(self): diff --git a/IPython/core/tests/test_ultratb.py b/IPython/core/tests/test_ultratb.py index c2e72e4..c8f5271 100644 --- a/IPython/core/tests/test_ultratb.py +++ b/IPython/core/tests/test_ultratb.py @@ -175,7 +175,8 @@ class IndentationErrorTest(unittest.TestCase): with tt.AssertPrints("IndentationError"): with tt.AssertPrints("zoom()", suppress=False): - ip.magic('run %s' % fname) + ip.run_line_magic("run", fname) + @skip_without("pandas") def test_dynamic_code(): @@ -237,7 +238,7 @@ bar() f.write(se_file_1) with tt.AssertPrints(["7/", "SyntaxError"]): - ip.magic("run " + fname) + ip.run_line_magic("run", fname) # Modify the file with open(fname, "w", encoding="utf-8") as f: @@ -245,7 +246,7 @@ bar() # The SyntaxError should point to the correct line with tt.AssertPrints(["7/", "SyntaxError"]): - ip.magic("run " + fname) + ip.run_line_magic("run", fname) def test_non_syntaxerror(self): # SyntaxTB may be called with an error other than a SyntaxError