Print key in KeyError in map.__getitem__/__delitem__ (#5397)

* Print key in map.getitem/delitem KeyError

* Add tests

* Fix tests

* Make robust

* Make clang-tidy happy

* Return a Python str

* Show beginning and end of the message

* Avoid implicit conversion

* Split out `format_message_key_error_key_object()` to reduce amount of templated code.

* Use `"✄✄✄"` instead of `"..."`

Also rename variable to `cut_length`, to not get into even/odd issues with the meaning of "half".

---------

Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
This commit is contained in:
Paul-Edouard Sarlin
2024-10-08 19:49:35 +02:00
committed by GitHub
parent c4a05f9344
commit 56e69a20a5
2 changed files with 57 additions and 2 deletions

View File

@@ -302,6 +302,25 @@ def test_map_delitem():
assert list(mm) == ["b"]
assert list(mm.items()) == [("b", 2.5)]
with pytest.raises(KeyError) as excinfo:
mm["a_long_key"]
assert "a_long_key" in str(excinfo.value)
with pytest.raises(KeyError) as excinfo:
del mm["a_long_key"]
assert "a_long_key" in str(excinfo.value)
cut_length = 100
k_very_long = "ab" * cut_length + "xyz"
with pytest.raises(KeyError) as excinfo:
mm[k_very_long]
assert k_very_long in str(excinfo.value)
k_very_long += "@"
with pytest.raises(KeyError) as excinfo:
mm[k_very_long]
k_repr = k_very_long[:cut_length] + "✄✄✄" + k_very_long[-cut_length:]
assert k_repr in str(excinfo.value)
um = m.UnorderedMapStringDouble()
um["ua"] = 1.1
um["ub"] = 2.6