llvm-project/mlir/test/python/ir/auto_location_stable_abi.py
Jakub Kuderski 2c9720972e
[mlir][python] Add stable ABI (abi3) support (#183856)
Add `MLIR_ENABLE_PYTHON_STABLE_ABI` cmake flag to build bindings against
the Python limited/stable API (abi3 / PEP 384). This allow for
compatibility across different >=3.12 versions with a single .so /
wheel. We also require CMake >=3.26.

The stable ABI restricts usage to a subset of the CPython C API: frame
and code object structs are opaque, so introspection APIs like
`PyCode_Addr2Location`, `PyFrame_GetLasti`, and `PyFrame_GetCode` are
unavailable. The traceback-based auto-location logic is dropped because
we don’t have stable ABI to produce complete locations.

Assisted-by: claude
2026-03-01 13:45:52 +00:00

26 lines
622 B
Python

# RUN: %PYTHON %s | FileCheck %s
# REQUIRES: python-stable-abi
#
# Verify that traceback-based auto-location is not supported under the stable
# ABI (abi3) and always produces unknown locations.
import gc
from mlir.ir import *
def run(f):
print("\nTEST:", f.__name__)
f()
gc.collect()
assert Context._get_live_count() == 0
# CHECK-LABEL: TEST: testAutoLocationIsUnknown
@run
def testAutoLocationIsUnknown():
with Context() as ctx, loc_tracebacks():
ctx.allow_unregistered_dialects = True
op = Operation.create("custom.op1")
# CHECK: loc(unknown)
print(op.location)