Process arguments as str instead of bytes

This commit is contained in:
Pedro Gimeno 2020-11-08 15:25:01 +01:00
parent 5e88adcff3
commit f1d5905d7e

View file

@ -93,25 +93,25 @@ def parseArgs(s):
State = Space State = Space
p = 0 p = 0
Len = len(s) Len = len(s)
arg = b'' arg = ''
while p < Len: while p < Len:
c = s[p:p+1] c = s[p:p+1]
p += 1 p += 1
if State in (Space, Normal): if State in (Space, Normal):
if c == b'\\': if c == '\\':
State = NBackslash if State == Normal else SBackslash State = NBackslash if State == Normal else SBackslash
elif c == b'"': elif c == '"':
State = DQuote State = DQuote
elif c == b"'": elif c == "'":
State = SQuote State = SQuote
elif c in (b' ', b'\t'): elif c in (' ', '\t'):
if State == Normal: if State == Normal:
State = Space State = Space
args.append(arg) args.append(arg)
arg = b'' arg = ''
# else remain in the 'Space' state # else remain in the 'Space' state
elif c == b'\n': elif c == '\n':
break break
else: else:
State = Normal State = Normal
@ -122,20 +122,20 @@ def parseArgs(s):
else Space if State == SBackslash else Space if State == SBackslash
else Normal) else Normal)
else: else:
if State == DQBackslash and c not in (b'"', b'`', b'$', b'\\'): if State == DQBackslash and c not in ('"', '`', '$', '\\'):
arg += b'\\' arg += '\\'
arg += c arg += c
State = DQuote if State == DQBackslash else Normal State = DQuote if State == DQBackslash else Normal
elif State == DQuote: elif State == DQuote:
if c == b'\\': if c == '\\':
State = DQBackslash State = DQBackslash
# ` and $ are not interpreted by this parser. # ` and $ are not interpreted by this parser.
elif c == b'"': elif c == '"':
State = Normal State = Normal
else: else:
arg += c arg += c
elif State == SQuote: elif State == SQuote:
if c == b"'": if c == "'":
State = Normal State = Normal
else: else:
arg += c arg += c
@ -186,10 +186,10 @@ def parseArgs(s):
# args[i] = argout # args[i] = argout
# return args # return args
def tryRead(fn): def tryRead(fn, Binary = True):
result = None result = None
try: try:
f = open(fn, 'rb') f = open(fn, 'rb' if Binary else 'r')
try: try:
result = f.read() result = f.read()
finally: finally:
@ -701,7 +701,7 @@ def generateScriptTests():
stdin = tryRead(fbase + '.lsl') or '' stdin = tryRead(fbase + '.lsl') or ''
expected_stdout = tryRead(fbase + '.out') or b'' expected_stdout = tryRead(fbase + '.out') or b''
expected_stderr = tryRead(fbase + '.err') or b'' expected_stderr = tryRead(fbase + '.err') or b''
runargs = (parseArgs(tryRead(fbase + '.run')) runargs = (parseArgs(tryRead(fbase + '.run', Binary=False))
or (['main.py', '-y', '-'] if suite != 'Expr' or (['main.py', '-y', '-'] if suite != 'Expr'
else ['main.py', else ['main.py',
# Defaults for Expr: # Defaults for Expr: