Commit f37ab30f authored by Yuki Shiino's avatar Yuki Shiino Committed by Commit Bot

bind-gen: Make LooseFormatter support {} and {0}

Make LooseFormatter support implicit ({}) and explicit ({0}, {1},
...) positional argument specifiers.

Bug: 839389
Change-Id: I8a820cba90a2be580e63c15d35c194c4ba31c702
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1897575
Commit-Queue: Yuki Shiino <yukishiino@chromium.org>
Reviewed-by: default avatarHitoshi Yoshida <peria@chromium.org>
Cr-Commit-Position: refs/heads/master@{#712459}
parent af367756
......@@ -64,13 +64,25 @@ class CodeNode(object):
self.undefined_code_symbols = []
class _LooseFormatter(string.Formatter):
def __init__(self):
string.Formatter.__init__(self)
self._loose_formatter_indexing_count_ = 0
def get_value(self, key, args, kwargs):
if isinstance(key, (int, long)):
return args[key]
assert isinstance(key, str)
if not key:
# Before Python 3.1, when a positional argument specifier is
# omitted, |format_string="{}"| produces |key=""|.
index = self._loose_formatter_indexing_count_
self._loose_formatter_indexing_count_ += 1
return args[index]
if key in kwargs:
return kwargs[key]
else:
return "{" + key + "}"
_template_formatter = _LooseFormatter()
_gensym_seq_id = 0
@classmethod
......@@ -83,7 +95,7 @@ class CodeNode(object):
"${template_var} 42"
without raising an exception that |template_var| is unbound.
"""
return cls._template_formatter.format(format_string, *args, **kwargs)
return cls._LooseFormatter().format(format_string, *args, **kwargs)
@classmethod
def gensym(cls):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment