Spec.format: error on old style format strings (#41934)

This commit is contained in:
Harmen Stoppels 2024-01-03 17:44:59 +01:00
parent 4f7cce68b8
commit b15f9d011c
2 changed files with 20 additions and 0 deletions

View file

@ -213,6 +213,19 @@ def __call__(self, match):
return clr.colorize(re.sub(_SEPARATORS, insert_color(), str(spec)) + "@.") return clr.colorize(re.sub(_SEPARATORS, insert_color(), str(spec)) + "@.")
OLD_STYLE_FMT_RE = re.compile(r"\${[A-Z]+}")
def ensure_modern_format_string(fmt: str) -> None:
"""Ensure that the format string does not contain old ${...} syntax."""
result = OLD_STYLE_FMT_RE.search(fmt)
if result:
raise SpecFormatStringError(
f"Format string `{fmt}` contains old syntax `{result.group(0)}`. "
"This is no longer supported."
)
@lang.lazy_lexicographic_ordering @lang.lazy_lexicographic_ordering
class ArchSpec: class ArchSpec:
"""Aggregate the target platform, the operating system and the target microarchitecture.""" """Aggregate the target platform, the operating system and the target microarchitecture."""
@ -4360,6 +4373,7 @@ def format(self, format_string=DEFAULT_FORMAT, **kwargs):
that accepts a string and returns another one that accepts a string and returns another one
""" """
ensure_modern_format_string(format_string)
color = kwargs.get("color", False) color = kwargs.get("color", False)
transform = kwargs.get("transform", {}) transform = kwargs.get("transform", {})

View file

@ -1517,3 +1517,9 @@ def test_edge_equality_does_not_depend_on_virtual_order():
assert edge1 == edge2 assert edge1 == edge2
assert tuple(sorted(edge1.virtuals)) == edge1.virtuals assert tuple(sorted(edge1.virtuals)) == edge1.virtuals
assert tuple(sorted(edge2.virtuals)) == edge1.virtuals assert tuple(sorted(edge2.virtuals)) == edge1.virtuals
def test_old_format_strings_trigger_error(default_mock_concretization):
s = Spec("a").concretized()
with pytest.raises(SpecFormatStringError):
s.format("${PACKAGE}-${VERSION}-${HASH}")