Fix string format recursive replace
Also error on placeholder index out of range.
This commit is contained in:
parent
cf76ffad14
commit
312bc2ca81
|
@ -2999,12 +2999,19 @@ different subdirectory.
|
||||||
def format_string(self, templ, args):
|
def format_string(self, templ, args):
|
||||||
if isinstance(args, mparser.ArgumentNode):
|
if isinstance(args, mparser.ArgumentNode):
|
||||||
args = args.arguments
|
args = args.arguments
|
||||||
for (i, arg) in enumerate(args):
|
arg_strings = []
|
||||||
|
for arg in args:
|
||||||
arg = self.evaluate_statement(arg)
|
arg = self.evaluate_statement(arg)
|
||||||
if isinstance(arg, bool): # Python boolean is upper case.
|
if isinstance(arg, bool): # Python boolean is upper case.
|
||||||
arg = str(arg).lower()
|
arg = str(arg).lower()
|
||||||
templ = templ.replace('@{}@'.format(i), str(arg))
|
arg_strings.append(str(arg))
|
||||||
return templ
|
|
||||||
|
def arg_replace(match):
|
||||||
|
idx = int(match.group(1))
|
||||||
|
if idx >= len(arg_strings):
|
||||||
|
raise InterpreterException('Format placeholder @{}@ out of range.'.format(idx))
|
||||||
|
return arg_strings[idx]
|
||||||
|
return re.sub(r'@(\d+)@', arg_replace, templ)
|
||||||
|
|
||||||
# Only permit object extraction from the same subproject
|
# Only permit object extraction from the same subproject
|
||||||
def validate_extraction(self, buildtarget):
|
def validate_extraction(self, buildtarget):
|
||||||
|
|
|
@ -13,6 +13,8 @@ subs2 = '42'
|
||||||
|
|
||||||
assert(templ2.format(subs2) == '42', 'String formatting with variables is broken.')
|
assert(templ2.format(subs2) == '42', 'String formatting with variables is broken.')
|
||||||
|
|
||||||
|
assert('@@0@@ @@1@@'.format(1, 2) == '@1@ @2@', 'String format is recursive.')
|
||||||
|
|
||||||
long = 'abcde'
|
long = 'abcde'
|
||||||
prefix = 'abc'
|
prefix = 'abc'
|
||||||
suffix = 'cde'
|
suffix = 'cde'
|
||||||
|
|
Loading…
Reference in New Issue