diff --git a/IPython/core/magic.py b/IPython/core/magic.py index d7f30dc..43d606a 100644 --- a/IPython/core/magic.py +++ b/IPython/core/magic.py @@ -618,9 +618,9 @@ class Magics(Configurable): posix = kw.get('posix', os.name == 'posix') strict = kw.get('strict', True) - preserve_non_opts = kw.get('preserve_non_opts', False) + preserve_non_opts = kw.get("preserve_non_opts", False) remainder_arg_str = arg_str - + # Check if we have more than one argument to warrant extra processing: odict = {} # Dictionary with options args = arg_str.split() @@ -632,15 +632,18 @@ class Magics(Configurable): try: opts,args = getopt(argv, opt_str, long_opts) except GetoptError as e: - raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str, - " ".join(long_opts))) from e - for o,a in opts: - if mode is 'string' and preserve_non_opts: + raise UsageError( + '%s ( allowed: "%s" %s)' % (e.msg, opt_str, " ".join(long_opts)) + ) from e + for o, a in opts: + if mode is "string" and preserve_non_opts: # remove option-parts from the original args-string and preserve remaining-part. - # This relies on the arg_split(...) and getopt(...)'s impl spec, that the parsed options are - # returned in the original order. - remainder_arg_str = remainder_arg_str.replace(o, '', 1).replace(a, '', 1) - if o.startswith('--'): + # This relies on the arg_split(...) and getopt(...)'s impl spec, that the parsed options are + # returned in the original order. + remainder_arg_str = remainder_arg_str.replace(o, "", 1).replace( + a, "", 1 + ) + if o.startswith("--"): o = o[2:] else: o = o[1:] diff --git a/IPython/core/magics/execution.py b/IPython/core/magics/execution.py index 32a8476..9b49d79 100644 --- a/IPython/core/magics/execution.py +++ b/IPython/core/magics/execution.py @@ -1073,8 +1073,9 @@ class ExecutionMagics(Magics): does not matter as long as results from timeit.py are not mixed with those from %timeit.""" - opts, stmt = self.parse_options(line, 'n:r:tcp:qo', - posix=False, strict=False, preserve_non_opts=True) + opts, stmt = self.parse_options( + line, "n:r:tcp:qo", posix=False, strict=False, preserve_non_opts=True + ) if stmt == "" and cell is None: return diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index b4bd12f..63c48d0 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -473,17 +473,19 @@ def test_parse_options(): def test_parse_options_preserve_non_option_string(): """Test to assert preservation of non-option part of magic-block, while parsing magic options.""" m = DummyMagics(_ip) - opts, stmt = m.parse_options(' -n1 -r 13 _ = 314 + foo', 'n:r:', preserve_non_opts= True) - nt.assert_equal(opts, {'n': '1', 'r': '13'}) - nt.assert_equal(stmt, '_ = 314 + foo') + opts, stmt = m.parse_options( + " -n1 -r 13 _ = 314 + foo", "n:r:", preserve_non_opts=True + ) + nt.assert_equal(opts, {"n": "1", "r": "13"}) + nt.assert_equal(stmt, "_ = 314 + foo") 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.user_ns["spaces"] = [] _ip.magic("timeit -n1 -r1 spaces.append([s.count(' ') for s in ['document']])") - assert _ip.user_ns['spaces'] == [[0]] - + assert _ip.user_ns["spaces"] == [[0]] + def test_dirops(): """Test various directory handling operations."""