MCPcopy Create free account
hub / github.com/apache/tvm-ffi / test_error_no_cyclic_reference

Function test_error_no_cyclic_reference

tests/python/test_error.py:120–153  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

118
119
120def test_error_no_cyclic_reference() -> None:
121 # This test case ensures that when an error is raised from C++ side,
122 # there is no cyclic reference that slows down the garbage collection.
123 # Please see `_with_append_backtrace` in error.py
124
125 # temporarily disable gc
126 gc.disable()
127
128 try:
129 # We should create a class as a probe to detect gc activity
130 # beacuse weakref doesn't support list, dict or other trivial types
131 class SampleObject: ...
132
133 # trigger a C++ side KeyError by accessing a non-existent key
134 def trigger_cpp_side_error() -> None:
135 try:
136 tmp_map: tvm_ffi.Map = tvm_ffi.Map(dict())
137 tmp_map["a"]
138 except KeyError:
139 pass
140
141 def may_create_cyclic_reference() -> weakref.ReferenceType:
142 obj = SampleObject()
143 trigger_cpp_side_error()
144 return weakref.ref(obj)
145
146 wref = may_create_cyclic_reference()
147
148 # if the object is not collected, wref() will return the object
149 assert wref() is None, "Cyclic reference occurs inside error handling pipeline"
150
151 finally:
152 # re-enable gc whenever exception occurs
153 gc.enable()

Callers

nothing calls this directly

Calls 1

Tested by

no test coverage detected