This is an automated email from the ASF dual-hosted git repository.
tqchen pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm-ffi.git
The following commit(s) were added to refs/heads/main by this push:
new c88110e fix(Map): `Map.get` should never throw when element is
missing (#48)
c88110e is described below
commit c88110e76e7bfb1c72e8a2bf371afaf7e018aa74
Author: Junru Shao <[email protected]>
AuthorDate: Tue Sep 23 06:56:39 2025 -0700
fix(Map): `Map.get` should never throw when element is missing (#48)
This PR fixes missing element handling within `Map` by correcting the
exception type thrown from `IndexError` to `KeyError`. Additionally, it
introduces a new test to confirm that the `Map.get` method behaves as
expected, i.e. returning `None` for absent keys.
---
include/tvm/ffi/container/map.h | 2 +-
tests/python/test_container.py | 6 ++++++
2 files changed, 7 insertions(+), 1 deletion(-)
diff --git a/include/tvm/ffi/container/map.h b/include/tvm/ffi/container/map.h
index df41d3f..08f9a47 100644
--- a/include/tvm/ffi/container/map.h
+++ b/include/tvm/ffi/container/map.h
@@ -691,7 +691,7 @@ class DenseMapObj : public MapObj {
mapped_type& At(const key_type& key) const {
ListNode iter = Search(key);
if (iter.IsNone()) {
- TVM_FFI_THROW(IndexError) << "key is not in Map";
+ TVM_FFI_THROW(KeyError) << "key is not in Map";
}
return iter.Val();
}
diff --git a/tests/python/test_container.py b/tests/python/test_container.py
index f42e9b3..a025062 100644
--- a/tests/python/test_container.py
+++ b/tests/python/test_container.py
@@ -124,3 +124,9 @@ def test_serialization() -> None:
a = tvm_ffi.convert([1, 2, 3])
b = pickle.loads(pickle.dumps(a))
assert str(b) == "[1, 2, 3]"
+
+
+def test_large_map_get() -> None:
+ amap = tvm_ffi.convert({k: k**2 for k in range(100)})
+ assert amap.get(101) is None
+ assert amap.get(3) == 9