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

Function test_outer_bad_subclass

numpy/core/tests/test_umath.py:4673–4701  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

4671 assert actual.__class__.__name__ == 'foo'
4672
4673def test_outer_bad_subclass():
4674 class BadArr1(np.ndarray):
4675 def __array_finalize__(self, obj):
4676 # The outer call reshapes to 3 dims, try to do a bad reshape.
4677 if self.ndim == 3:
4678 self.shape = self.shape + (1,)
4679
4680 def __array_prepare__(self, obj, context=None):
4681 return obj
4682
4683 class BadArr2(np.ndarray):
4684 def __array_finalize__(self, obj):
4685 if isinstance(obj, BadArr2):
4686 # outer inserts 1-sized dims. In that case disturb them.
4687 if self.shape[-1] == 1:
4688 self.shape = self.shape[::-1]
4689
4690 def __array_prepare__(self, obj, context=None):
4691 return obj
4692
4693 for cls in [BadArr1, BadArr2]:
4694 arr = np.ones((2, 3)).view(cls)
4695 with assert_raises(TypeError) as a:
4696 # The first array gets reshaped (not the second one)
4697 np.add.outer(arr, [1, 2])
4698
4699 # This actually works, since we only see the reshaping error:
4700 arr = np.ones((2, 3)).view(cls)
4701 assert type(np.add.outer([1, 2], arr)) is cls
4702
4703def test_outer_exceeds_maxdims():
4704 deep = np.ones((1,) * 17)

Callers

nothing calls this directly

Calls 3

assert_raisesFunction · 0.90
viewMethod · 0.45
outerMethod · 0.45

Tested by

no test coverage detected