##// END OF EJS Templates
add strict flag to arg_split, to optionally ignore shlex parse errors...
MinRK -
Show More
@@ -266,6 +266,7 b' python-profiler package from non-free.""")'
266 # Get options
266 # Get options
267 list_all = kw.get('list_all',0)
267 list_all = kw.get('list_all',0)
268 posix = kw.get('posix', os.name == 'posix')
268 posix = kw.get('posix', os.name == 'posix')
269 strict = kw.get('strict', True)
269
270
270 # Check if we have more than one argument to warrant extra processing:
271 # Check if we have more than one argument to warrant extra processing:
271 odict = {} # Dictionary with options
272 odict = {} # Dictionary with options
@@ -273,7 +274,7 b' python-profiler package from non-free.""")'
273 if len(args) >= 1:
274 if len(args) >= 1:
274 # If the list of inputs only has 0 or 1 thing in it, there's no
275 # If the list of inputs only has 0 or 1 thing in it, there's no
275 # need to look for options
276 # need to look for options
276 argv = arg_split(arg_str,posix)
277 argv = arg_split(arg_str, posix, strict)
277 # Do regular option processing
278 # Do regular option processing
278 try:
279 try:
279 opts,args = getopt(argv,opt_str,*long_opts)
280 opts,args = getopt(argv,opt_str,*long_opts)
@@ -1865,7 +1866,7 b' Currently the magic system has the following functions:\\n"""'
1865 scaling = [1, 1e3, 1e6, 1e9]
1866 scaling = [1, 1e3, 1e6, 1e9]
1866
1867
1867 opts, stmt = self.parse_options(parameter_s,'n:r:tcp:',
1868 opts, stmt = self.parse_options(parameter_s,'n:r:tcp:',
1868 posix=False)
1869 posix=False, strict=False)
1869 if stmt == "":
1870 if stmt == "":
1870 return
1871 return
1871 timefunc = timeit.default_timer
1872 timefunc = timeit.default_timer
@@ -344,3 +344,12 b' def test_psearch():'
344 with tt.AssertPrints("dict.fromkeys"):
344 with tt.AssertPrints("dict.fromkeys"):
345 _ip.run_cell("dict.fr*?")
345 _ip.run_cell("dict.fr*?")
346
346
347 def test_timeit_shlex():
348 """test shlex issues with timeit (#1109)"""
349 _ip.ex("def f(*a,**kw): pass")
350 _ip.magic('timeit -n1 "this is a bug".count(" ")')
351 _ip.magic('timeit -r1 -n1 f(" ", 1)')
352 _ip.magic('timeit -r1 -n1 f(" ", 1, " ", 2, " ")')
353 _ip.magic('timeit -r1 -n1 ("a " + "b")')
354 _ip.magic('timeit -r1 -n1 f("a " + "b")')
355 _ip.magic('timeit -r1 -n1 f("a " + "b ")')
@@ -146,12 +146,18 b' def getoutputerror(cmd):'
146 return py3compat.bytes_to_str(out), py3compat.bytes_to_str(err)
146 return py3compat.bytes_to_str(out), py3compat.bytes_to_str(err)
147
147
148
148
149 def arg_split(s, posix=False):
149 def arg_split(s, posix=False, strict=True):
150 """Split a command line's arguments in a shell-like manner.
150 """Split a command line's arguments in a shell-like manner.
151
151
152 This is a modified version of the standard library's shlex.split()
152 This is a modified version of the standard library's shlex.split()
153 function, but with a default of posix=False for splitting, so that quotes
153 function, but with a default of posix=False for splitting, so that quotes
154 in inputs are respected."""
154 in inputs are respected.
155
156 if strict=False, then any errors shlex.split would raise will result in the
157 unparsed remainder being the last element of the list, rather than raising.
158 This is because we sometimes use arg_split to parse things other than
159 command-line args.
160 """
155
161
156 # Unfortunately, python's shlex module is buggy with unicode input:
162 # Unfortunately, python's shlex module is buggy with unicode input:
157 # http://bugs.python.org/issue1170
163 # http://bugs.python.org/issue1170
@@ -163,7 +169,25 b' def arg_split(s, posix=False):'
163 s = s.encode('utf-8')
169 s = s.encode('utf-8')
164 lex = shlex.shlex(s, posix=posix)
170 lex = shlex.shlex(s, posix=posix)
165 lex.whitespace_split = True
171 lex.whitespace_split = True
166 tokens = list(lex)
172 # Extract tokens, ensuring that things like leaving open quotes
173 # does not cause this to raise. This is important, because we
174 # sometimes pass Python source through this (e.g. %timeit f(" ")),
175 # and it shouldn't raise an exception.
176 # It may be a bad idea to parse things that are not command-line args
177 # through this function, but we do, so let's be safe about it.
178 tokens = []
179 while True:
180 try:
181 tokens.append(lex.next())
182 except StopIteration:
183 break
184 except ValueError:
185 if strict:
186 raise
187 # couldn't parse, get remaining blob as last token
188 tokens.append(lex.token)
189 break
190
167 if is_unicode:
191 if is_unicode:
168 # Convert the tokens back to unicode.
192 # Convert the tokens back to unicode.
169 tokens = [x.decode('utf-8') for x in tokens]
193 tokens = [x.decode('utf-8') for x in tokens]
General Comments 0
You need to be logged in to leave comments. Login now