diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-03-31 03:28:06 -0600 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-03-31 03:51:03 -0600 |
commit | 1a3f1e07ef06b8953b509352ff1c3872534f7583 (patch) | |
tree | 1614fc8f127a0955ab711f426632f7a5741dcaee | |
parent | 6b50631a8d0aa1b64995ce562972068b9f214759 (diff) |
measurestack: Fix pretty-printing of BaseName and QName
-rw-r--r-- | build-aux/measurestack/__init__.py | 26 | ||||
-rw-r--r-- | build-aux/measurestack/test_misc.py | 24 |
2 files changed, 43 insertions, 7 deletions
diff --git a/build-aux/measurestack/__init__.py b/build-aux/measurestack/__init__.py index 549e888..e4f1156 100644 --- a/build-aux/measurestack/__init__.py +++ b/build-aux/measurestack/__init__.py @@ -147,8 +147,16 @@ class BaseName: def __str__(self) -> str: return self._content + def __repr__(self) -> str: + return f"BaseName({self._content!r})" + + def __format__(self, fmt_spec: str, /) -> str: + return repr(self) + def __eq__(self, other: typing.Any) -> bool: - assert isinstance(other, BaseName) + assert isinstance( + other, BaseName + ), f"comparing BaseName with {other.__class__.__name__}" return self._content == other._content def __lt__(self, other: "BaseName") -> bool: @@ -167,6 +175,12 @@ class QName: def __str__(self) -> str: return self._content + def __repr__(self) -> str: + return f"QName({self._content!r})" + + def __format__(self, fmt_spec: str, /) -> str: + return repr(self) + def __eq__(self, other: typing.Any) -> bool: assert isinstance( other, QName @@ -289,7 +303,7 @@ def analyze( raise ValueError(f"unknown edge key {k!r}") if not skip: if node.funcname in graph: - raise ValueError(f"duplicate node {str(node.funcname)!r}") + raise ValueError(f"duplicate node {node.funcname}") graph[node.funcname] = node if ":" in str(node.funcname): basename = node.funcname.base() @@ -330,7 +344,7 @@ def analyze( for node in app.extra_nodes(): if node.funcname in graph: - raise ValueError(f"duplicate node {str(node.funcname)!r}") + raise ValueError(f"duplicate node {node.funcname}") graph[node.funcname] = node missing: set[QName] = set() @@ -341,8 +355,8 @@ def analyze( def resolve_funcname(funcname: QName) -> QName | None: # Handle `ld --wrap` functions - if QName(f"__wrap_{funcname}") in graph: - return QName(f"__wrap_{funcname}") + if QName(f"__wrap_{str(funcname)}") in graph: + return QName(f"__wrap_{str(funcname)}") if ( str(funcname).startswith("__real_") and QName(str(funcname)[len("__real_") :]) in graph @@ -1614,7 +1628,7 @@ def main( if result.groups["Extra"].rows: print_group("Extra") for funcname in sorted(result.included_funcs): - print(f"included: {funcname}") + print(f"included: {str(funcname)}") print("*/") diff --git a/build-aux/measurestack/test_misc.py b/build-aux/measurestack/test_misc.py index 5cdf629..cc496ea 100644 --- a/build-aux/measurestack/test_misc.py +++ b/build-aux/measurestack/test_misc.py @@ -5,8 +5,30 @@ # pylint: disable=unused-variable +import pytest + from . import BaseName, QName -def test_basename() -> None: +def test_name_base() -> None: assert QName("foo.c:bar.1").base() == BaseName("bar") + + +def test_name_pretty() -> None: + name = QName("foo.c:bar.1") + assert f"{name}" == "QName('foo.c:bar.1')" + assert f"{name.base()}" == "BaseName('bar')" + assert f"{[name]}" == "[QName('foo.c:bar.1')]" + assert f"{[name.base()]}" == "[BaseName('bar')]" + + +def test_name_eq() -> None: + name = QName("foo.c:bar.1") + with pytest.raises(AssertionError) as e: + if name == "foo": + pass + assert "comparing QName with str" in str(e) + with pytest.raises(AssertionError) as e: + if name.base() == "foo": + pass + assert "comparing BaseName with str" in str(e) |