safe_shlex_split: Give up after 3 tries.

This commit is contained in:
Florian Bruhin 2014-09-18 16:15:37 +02:00
parent 2e35685fe1
commit 6a86924e60

View File

@ -113,9 +113,11 @@ def safe_shlex_split(s):
""" """
if s is None: if s is None:
raise TypeError("Can't split None!") raise TypeError("Can't split None!")
while True: tokens = None
orig_s = s
for i in range(3):
try: try:
return shlex.split(s) tokens = shlex.split(s)
except ValueError as e: except ValueError as e:
if str(e) == "No closing quotation": if str(e) == "No closing quotation":
# e.g. eggs "bacon ham # e.g. eggs "bacon ham
@ -127,6 +129,11 @@ def safe_shlex_split(s):
s += '\\' s += '\\'
else: else:
raise raise
if tokens is None:
raise AssertionError("Gave up splitting >{}< after {} tries. "
"Attempted fixup: >{}<. This is a bug.".format(
orig_s, i, s))
return tokens
def pastebin(text): def pastebin(text):