[Mlir-commits] [mlir] [mlir:python] Prevent crash in DenseElementsAttr. (PR #163564)
Ingo Müller
llvmlistbot at llvm.org
Wed Oct 15 07:19:55 PDT 2025
https://github.com/ingomueller-net created https://github.com/llvm/llvm-project/pull/163564
This PR fixes a crash in the `bf_getbuffer` implementation of `PyDenseElementsAttribute` that occurred when an element type was not supported, such as `bf16`. I believe that supportion `bf16` is not possible with that protocol but that's out of the scope of this PR. Previsouly, the code raised an `std::exception` out of `bf_getbuffer` that nanobind does not catch (see also pybind/pybind11#3336). The PR makes the function catch all `std::exception`s and manually raises a Python exception instead.
>From b9c9523a48608726e83434b2b64a2f47348141bd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ingo=20M=C3=BCller?= <ingomueller at google.com>
Date: Wed, 15 Oct 2025 14:14:58 +0000
Subject: [PATCH] [mlir:python] Prevent crash in DenseElementsAttr.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
This PR fixes a crash in the `bf_getbuffer` implementation of
`PyDenseElementsAttribute` that occurred when an element type was not
supported, such as `bf16`. I believe that supportion `bf16` is not
possible with that protocol but that's out of the scope of this PR.
Previsouly, the code raised an `std::exception` out of `bf_getbuffer`
that nanobind does not catch (see also pybind/pybind11#3336). The PR
makes the function catch all `std::exception`s and manually raises a
Python exception instead.
Signed-off-by: Ingo Müller <ingomueller at google.com>
---
mlir/lib/Bindings/Python/IRAttributes.cpp | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/mlir/lib/Bindings/Python/IRAttributes.cpp b/mlir/lib/Bindings/Python/IRAttributes.cpp
index 045c0fbf4630f..c0a945e3f4f3b 100644
--- a/mlir/lib/Bindings/Python/IRAttributes.cpp
+++ b/mlir/lib/Bindings/Python/IRAttributes.cpp
@@ -1306,6 +1306,10 @@ PyType_Slot PyDenseElementsAttribute::slots[] = {
e.restore();
nb::chain_error(PyExc_BufferError, "Error converting attribute to buffer");
return -1;
+ } catch (std::exception &e) {
+ nb::chain_error(PyExc_BufferError,
+ "Error converting attribute to buffer: %s", e.what());
+ return -1;
}
view->obj = obj;
view->ndim = 1;
More information about the Mlir-commits
mailing list