Skip to content

Commit 8ac72d5

Browse files
Fixes split_args handling of quotes that start or end inside an argument. (#439)
1 parent e634365 commit 8ac72d5

2 files changed

Lines changed: 57 additions & 31 deletions

File tree

‎src/manage/scriptutils.py‎

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -344,44 +344,50 @@ def split_args(arg_string, argv0=False):
344344
"""Splits a single argument string into separate unquoted items.
345345
346346
If argv0 is True, the first argument is parsed as if it is the executable name.
347+
348+
Arguments are split using the same rules as the C runtime, so quotes may begin
349+
and end anywhere within an argument.
347350
"""
348351
args = []
349352
if argv0 and arg_string[:1] == '"':
350353
a, _, arg_string = arg_string[1:].partition('"')
351354
args.append(a)
352355

353-
arg_buffer = None
354-
quoted_arg = []
355-
bits = arg_string.strip().split(' ')
356-
while bits:
357-
a = bits.pop(0)
358-
pre, quot, post = a.partition('"')
359-
if arg_buffer:
360-
pre = arg_buffer + pre
361-
arg_buffer = None
362-
if not quot:
363-
if quoted_arg:
364-
quoted_arg.append(pre)
365-
quoted_arg.append(' ')
366-
else:
367-
args.append(pre.replace('\\\\', '\\'))
356+
arg = None
357+
in_quotes = False
358+
backslashes = 0
359+
i = 0
360+
while i < len(arg_string):
361+
c = arg_string[i]
362+
i += 1
363+
if c == '\\':
364+
backslashes += 1
368365
continue
369-
if pre[-1:] == '\\' and (len(pre) - len(pre.rstrip('\\'))) % 2 == 1:
370-
arg_buffer = pre[:-1] + quot
371-
if post:
372-
bits.insert(0, post)
366+
if c == '"':
367+
# 2n backslashes + quote -> n backslashes and toggle quoting
368+
# 2n+1 backslashes + quote -> n backslashes and a literal quote
369+
arg = (arg or '') + '\\' * (backslashes // 2)
370+
if backslashes % 2:
371+
arg += '"'
372+
elif in_quotes and arg_string[i:i + 1] == '"':
373+
# A doubled quote inside quotes is a literal quote
374+
arg += '"'
375+
i += 1
376+
else:
377+
in_quotes = not in_quotes
378+
backslashes = 0
373379
continue
374-
elif quoted_arg:
375-
quoted_arg.append(pre)
376-
args.append(''.join(quoted_arg).replace('\\\\', '\\'))
377-
quoted_arg.clear()
380+
if backslashes:
381+
arg = (arg or '') + '\\' * backslashes
382+
backslashes = 0
383+
if c in ' \t' and not in_quotes:
384+
if arg is not None:
385+
args.append(arg)
386+
arg = None
378387
continue
379-
380-
quoted_arg.append(pre)
381-
if pre:
382-
quoted_arg.append(quot)
383-
if post:
384-
bits.insert(0, post)
385-
if quoted_arg:
386-
args.append(''.join(quoted_arg))
388+
arg = (arg or '') + c
389+
if backslashes:
390+
arg = (arg or '') + '\\' * backslashes
391+
if arg is not None:
392+
args.append(arg)
387393
return args

‎tests/test_scriptutils.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,26 @@ def test_quote_args(args, expect):
279279
assert args == split_args(expect), expect
280280

281281

282+
# Examples from "Parsing C command-line arguments" in the Microsoft C runtime
283+
# documentation, plus quotes that begin or end partway through an argument.
284+
@pytest.mark.parametrize("arg_string, expect", [pytest.param(*a, id=a[0]) for a in [
285+
('"a b c" d e', ['a b c', 'd', 'e']),
286+
('"ab\\"c" "\\\\" d', ['ab"c', '\\', 'd']),
287+
('a\\\\\\b d"e f"g h', ['a\\\\\\b', 'de fg', 'h']),
288+
('a\\\\\\"b c d', ['a\\"b', 'c', 'd']),
289+
('a\\\\\\\\"b c" d e', ['a\\\\b c', 'd', 'e']),
290+
('a"b"" c d', ['ab" c d']),
291+
('--opt="a b" -E', ['--opt=a b', '-E']),
292+
('-W"ignore"', ['-Wignore']),
293+
('"a b"c d', ['a bc', 'd']),
294+
('-X utf8', ['-X', 'utf8']),
295+
('x "" y', ['x', '', 'y']),
296+
('a\tb', ['a', 'b']),
297+
]])
298+
def test_split_args(arg_string, expect):
299+
assert expect == split_args(arg_string)
300+
301+
282302
@pytest.mark.parametrize("line, expect_id, expect_line", [pytest.param(*a, id=a[0]) for a in [
283303
("#!/usr/bin/python", "Test1", None),
284304
("#!/usr/bin/python -Es", "Test1", None),

0 commit comments

Comments
 (0)