[Mlir-commits] [mlir] [mlir][py] Mark all type caster `from_{cpp, python}` methods as noexcept (PR #143866)

Nicholas Junge llvmlistbot at llvm.org
Wed Jul 9 09:26:29 PDT 2025


nicholasjng wrote:

Hey, I'm still on this. 

My current idea is to return an invalid handle from `mlirApiObjectToCapsule` (i.e. one the wraps `nullptr`), but either the `mlirCapsuleTo*` family or the `PyCapsule_GetPointer` Python C API doesn't like that. I'm getting a `RuntimeError: std::bad_cast`, which I'm not sure how to address (I'll probably need to debug the bindings directly in C++).

Otherwise, my idea is to just return `nanobind::none` from `mlirApiObjectToCapsule`, and return early in `from_python` if `capsule.is_none()`. That is not as nice of a solution since it requires that logic to be inserted in all `from_python` type caster methods, and I'm not sure if the results play nice with the current testing assumptions.

I'll have to read up on the Python lit testing as well, since I don't know how to interpret the failed test output correctly (see below). But I'm working on it.

<details>
<summary>Test output</summary>

```
Command Output (stdout):
--
# RUN: at line 1
/Users/nicholasjunge/Workspaces/c++/llvm-project/mlir/.venv/bin/python3 /Users/nicholasjunge/Workspaces/c++/llvm-project/mlir/test/python/dialects/python_test.py pybind11 | /Users/nicholasjunge/Workspaces/c++/llvm-project/build/bin/FileCheck /Users/nicholasjunge/Workspaces/c++/llvm-project/mlir/test/python/dialects/python_test.py
# executed command: /Users/nicholasjunge/Workspaces/c++/llvm-project/mlir/.venv/bin/python3 /Users/nicholasjunge/Workspaces/c++/llvm-project/mlir/test/python/dialects/python_test.py pybind11
# executed command: /Users/nicholasjunge/Workspaces/c++/llvm-project/build/bin/FileCheck /Users/nicholasjunge/Workspaces/c++/llvm-project/mlir/test/python/dialects/python_test.py
# RUN: at line 2
/Users/nicholasjunge/Workspaces/c++/llvm-project/mlir/.venv/bin/python3 /Users/nicholasjunge/Workspaces/c++/llvm-project/mlir/test/python/dialects/python_test.py nanobind | /Users/nicholasjunge/Workspaces/c++/llvm-project/build/bin/FileCheck /Users/nicholasjunge/Workspaces/c++/llvm-project/mlir/test/python/dialects/python_test.py
# executed command: /Users/nicholasjunge/Workspaces/c++/llvm-project/mlir/.venv/bin/python3 /Users/nicholasjunge/Workspaces/c++/llvm-project/mlir/test/python/dialects/python_test.py nanobind
# .---command stderr------------
# | Traceback (most recent call last):
# |   File "/Users/nicholasjunge/Workspaces/c++/llvm-project/mlir/test/python/dialects/python_test.py", line 329, in <module>
# |     @run
# |      ^^^
# |   File "/Users/nicholasjunge/Workspaces/c++/llvm-project/mlir/test/python/dialects/python_test.py", line 35, in run
# |     f()
# |   File "/Users/nicholasjunge/Workspaces/c++/llvm-project/mlir/test/python/dialects/python_test.py", line 362, in testCustomAttribute
# |     TestAttr(42)
# | RuntimeError: std::bad_cast
# `-----------------------------
# error: command failed with exit status: 1
# executed command: /Users/nicholasjunge/Workspaces/c++/llvm-project/build/bin/FileCheck /Users/nicholasjunge/Workspaces/c++/llvm-project/mlir/test/python/dialects/python_test.py
# .---command stderr------------
# | /Users/nicholasjunge/Workspaces/c++/llvm-project/mlir/test/python/dialects/python_test.py:424:16: error: CHECK-LABEL: expected string not found in input
# | # CHECK-LABEL: TEST: testTensorValue
# |                ^
# | <stdin>:50:26: note: scanning from here
# | TEST: testCustomAttribute
# |                          ^
# | <stdin>:51:3: note: possible intended match here
# | #python_test.test_attr
# |   ^
# | 
# | Input file: <stdin>
# | Check file: /Users/nicholasjunge/Workspaces/c++/llvm-project/mlir/test/python/dialects/python_test.py
# | 
# | -dump-input=help explains the following input dump.
# | 
# | Input was:
# | <<<<<<
# |              .
# |              .
# |              .
# |             45:  
# |             46: TEST: testOptionalOperandOp 
# |             47: op1.input is None: True 
# |             48: op2.input is None: False 
# |             49:  
# |             50: TEST: testCustomAttribute 
# | label:424'0                              X error: no match found
# |             51: #python_test.test_attr 
# | label:424'0     ~~~~~~~~~~~~~~~~~~~~~~~
# | label:424'1       ?                     possible intended match
# |             52: python_test.custom_attributed_op {test_attr = #python_test.test_attr} () -> () 
# | label:424'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |             53:  
# | label:424'0     ~
# |             54: #python_test.test_attr 
# | label:424'0     ~~~~~~~~~~~~~~~~~~~~~~~
# |             55: TestAttr(#python_test.test_attr) 
# | label:424'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | >>>>>>
# `-----------------------------
# error: command failed with exit status: 1

--

********************
********************
```

</details>

https://github.com/llvm/llvm-project/pull/143866


More information about the Mlir-commits mailing list