MCPcopy Create free account
hub / github.com/numpy/numpy / test_lifetime_on_error

Method test_lifetime_on_error

numpy/core/tests/test_multiarray.py:9539–9571  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

9537 np.arange(10).view(BadAttributeArray)
9538
9539 def test_lifetime_on_error(self):
9540 # gh-11237
9541 class RaisesInFinalize(np.ndarray):
9542 def __array_finalize__(self, obj):
9543 # crash, but keep this object alive
9544 raise Exception(self)
9545
9546 # a plain object can't be weakref'd
9547 class Dummy: pass
9548
9549 # get a weak reference to an object within an array
9550 obj_arr = np.array(Dummy())
9551 obj_ref = weakref.ref(obj_arr[()])
9552
9553 # get an array that crashed in __array_finalize__
9554 with assert_raises(Exception) as e:
9555 obj_arr.view(RaisesInFinalize)
9556
9557 obj_subarray = e.exception.args[0]
9558 del e
9559 assert_(isinstance(obj_subarray, RaisesInFinalize))
9560
9561 # reference should still be held by obj_arr
9562 break_cycles()
9563 assert_(obj_ref() is not None, "object should not already be dead")
9564
9565 del obj_arr
9566 break_cycles()
9567 assert_(obj_ref() is not None, "obj_arr should not hold the last reference")
9568
9569 del obj_subarray
9570 break_cycles()
9571 assert_(obj_ref() is None, "no references should remain")
9572
9573 def test_can_use_super(self):
9574 class SuperFinalize(np.ndarray):

Callers

nothing calls this directly

Calls 5

assert_raisesFunction · 0.90
assert_Function · 0.90
break_cyclesFunction · 0.90
DummyClass · 0.70
viewMethod · 0.45

Tested by

no test coverage detected