Show More
@@ -618,9 +618,9 b' class Magics(Configurable):' | |||
|
618 | 618 | posix = kw.get('posix', os.name == 'posix') |
|
619 | 619 | strict = kw.get('strict', True) |
|
620 | 620 | |
|
621 |
preserve_non_opts = kw.get( |
|
|
621 | preserve_non_opts = kw.get("preserve_non_opts", False) | |
|
622 | 622 | remainder_arg_str = arg_str |
|
623 | ||
|
623 | ||
|
624 | 624 | # Check if we have more than one argument to warrant extra processing: |
|
625 | 625 | odict = {} # Dictionary with options |
|
626 | 626 | args = arg_str.split() |
@@ -632,15 +632,18 b' class Magics(Configurable):' | |||
|
632 | 632 | try: |
|
633 | 633 | opts,args = getopt(argv, opt_str, long_opts) |
|
634 | 634 | except GetoptError as e: |
|
635 |
raise UsageError( |
|
|
636 | " ".join(long_opts))) from e | |
|
637 | for o,a in opts: | |
|
638 | if mode is 'string' and preserve_non_opts: | |
|
635 | raise UsageError( | |
|
636 | '%s ( allowed: "%s" %s)' % (e.msg, opt_str, " ".join(long_opts)) | |
|
637 | ) from e | |
|
638 | for o, a in opts: | |
|
639 | if mode is "string" and preserve_non_opts: | |
|
639 | 640 | # remove option-parts from the original args-string and preserve remaining-part. |
|
640 |
# This relies on the arg_split(...) and getopt(...)'s impl spec, that the parsed options are |
|
|
641 |
# returned in the original order. |
|
|
642 |
remainder_arg_str = remainder_arg_str.replace(o, |
|
|
643 | if o.startswith('--'): | |
|
641 | # This relies on the arg_split(...) and getopt(...)'s impl spec, that the parsed options are | |
|
642 | # returned in the original order. | |
|
643 | remainder_arg_str = remainder_arg_str.replace(o, "", 1).replace( | |
|
644 | a, "", 1 | |
|
645 | ) | |
|
646 | if o.startswith("--"): | |
|
644 | 647 | o = o[2:] |
|
645 | 648 | else: |
|
646 | 649 | o = o[1:] |
@@ -1073,8 +1073,9 b' class ExecutionMagics(Magics):' | |||
|
1073 | 1073 | does not matter as long as results from timeit.py are not mixed with |
|
1074 | 1074 | those from %timeit.""" |
|
1075 | 1075 | |
|
1076 |
opts, stmt = self.parse_options( |
|
|
1077 |
|
|
|
1076 | opts, stmt = self.parse_options( | |
|
1077 | line, "n:r:tcp:qo", posix=False, strict=False, preserve_non_opts=True | |
|
1078 | ) | |
|
1078 | 1079 | if stmt == "" and cell is None: |
|
1079 | 1080 | return |
|
1080 | 1081 |
@@ -473,17 +473,19 b' def test_parse_options():' | |||
|
473 | 473 | def test_parse_options_preserve_non_option_string(): |
|
474 | 474 | """Test to assert preservation of non-option part of magic-block, while parsing magic options.""" |
|
475 | 475 | m = DummyMagics(_ip) |
|
476 | opts, stmt = m.parse_options(' -n1 -r 13 _ = 314 + foo', 'n:r:', preserve_non_opts= True) | |
|
477 | nt.assert_equal(opts, {'n': '1', 'r': '13'}) | |
|
478 | nt.assert_equal(stmt, '_ = 314 + foo') | |
|
476 | opts, stmt = m.parse_options( | |
|
477 | " -n1 -r 13 _ = 314 + foo", "n:r:", preserve_non_opts=True | |
|
478 | ) | |
|
479 | nt.assert_equal(opts, {"n": "1", "r": "13"}) | |
|
480 | nt.assert_equal(stmt, "_ = 314 + foo") | |
|
479 | 481 | |
|
480 | 482 | |
|
481 | 483 | def test_run_magic_preserve_code_block(): |
|
482 | 484 | """Test to assert preservation of non-option part of magic-block, while running magic.""" |
|
483 |
_ip.user_ns[ |
|
|
485 | _ip.user_ns["spaces"] = [] | |
|
484 | 486 | _ip.magic("timeit -n1 -r1 spaces.append([s.count(' ') for s in ['document']])") |
|
485 |
assert _ip.user_ns[ |
|
|
486 | ||
|
487 | assert _ip.user_ns["spaces"] == [[0]] | |
|
488 | ||
|
487 | 489 | |
|
488 | 490 | def test_dirops(): |
|
489 | 491 | """Test various directory handling operations.""" |
General Comments 0
You need to be logged in to leave comments.
Login now