##// END OF EJS Templates
bug: additional spaces while parsing timeit-magic options
Aditya Sathe -
Show More
@@ -618,6 +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('preserve_non_opts', False)
622 remainder_arg_str = arg_str
623
621 624 # Check if we have more than one argument to warrant extra processing:
622 625 odict = {} # Dictionary with options
623 626 args = arg_str.split()
@@ -630,8 +633,13 b' class Magics(Configurable):'
630 633 opts,args = getopt(argv, opt_str, long_opts)
631 634 except GetoptError as e:
632 635 raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
633 " ".join(long_opts))) from e
636 " ".join(long_opts))) from e
634 637 for o,a in opts:
638 if mode is 'string' and preserve_non_opts:
639 # 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, '', 1).replace(a, '', 1)
635 643 if o.startswith('--'):
636 644 o = o[2:]
637 645 else:
@@ -649,7 +657,10 b' class Magics(Configurable):'
649 657 # Prepare opts,args for return
650 658 opts = Struct(odict)
651 659 if mode == 'string':
652 args = ' '.join(args)
660 if preserve_non_opts:
661 args = remainder_arg_str.lstrip()
662 else:
663 args = ' '.join(args)
653 664
654 665 return opts,args
655 666
@@ -1073,8 +1073,8 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(line,'n:r:tcp:qo',
1077 posix=False, strict=False)
1076 opts, stmt = self.parse_options(line, 'n:r:tcp:qo',
1077 posix=False, strict=False, preserve_non_opts=True)
1078 1078 if stmt == "" and cell is None:
1079 1079 return
1080 1080
@@ -470,6 +470,21 b' def test_parse_options():'
470 470 nt.assert_equal(m.parse_options(u'foo', '')[1], u'foo')
471 471
472 472
473 def test_parse_options_preserve_non_option_string():
474 """Test to assert preservation of non-option part of magic-block, while parsing magic options."""
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')
479
480
481 def test_run_magic_preserve_code_block():
482 """Test to assert preservation of non-option part of magic-block, while running magic."""
483 _ip.user_ns['spaces'] = []
484 _ip.magic("timeit -n1 -r1 spaces.append([s.count(' ') for s in ['document']])")
485 assert _ip.user_ns['spaces'] == [[0]]
486
487
473 488 def test_dirops():
474 489 """Test various directory handling operations."""
475 490 # curpath = lambda :os.path.splitdrive(os.getcwd())[1].replace('\\','/')
General Comments 0
You need to be logged in to leave comments. Login now