[clang-tidy][NFC]remove deps of clang in clang tidy test (#116588)
[llvm-project.git] / mlir / test / python / ir / context_lifecycle.py
blobc20270999425ee33925836ed40de976ee7779918
1 # RUN: %PYTHON %s
2 # Standalone sanity check of context life-cycle.
3 import gc
4 import mlir.ir
6 assert mlir.ir.Context._get_live_count() == 0
8 # Create first context.
9 print("CREATE C1")
10 c1 = mlir.ir.Context()
11 assert mlir.ir.Context._get_live_count() == 1
12 c1_repr = repr(c1)
13 print("C1 = ", c1_repr)
15 print("GETTING AGAIN...")
16 c2 = c1._get_context_again()
17 c2_repr = repr(c2)
18 assert mlir.ir.Context._get_live_count() == 1
19 assert c1_repr == c2_repr
21 print("C2 =", c2)
23 # Make sure new contexts on constructor.
24 print("CREATE C3")
25 c3 = mlir.ir.Context()
26 assert mlir.ir.Context._get_live_count() == 2
27 c3_repr = repr(c3)
28 print("C3 =", c3)
29 assert c3_repr != c1_repr
30 print("FREE C3")
31 c3 = None
32 gc.collect()
33 assert mlir.ir.Context._get_live_count() == 1
35 print("Free C1")
36 c1 = None
37 gc.collect()
38 assert mlir.ir.Context._get_live_count() == 1
39 print("Free C2")
40 c2 = None
41 gc.collect()
42 assert mlir.ir.Context._get_live_count() == 0
44 # Create a context, get its capsule and create from capsule.
45 c4 = mlir.ir.Context()
46 c4_capsule = c4._CAPIPtr
47 assert '"mlir.ir.Context._CAPIPtr"' in repr(c4_capsule)
48 c5 = mlir.ir.Context._CAPICreate(c4_capsule)
49 assert c4 is c5