@@ -808,6 +808,14 @@ def test_casting(self):
808808 t = [('a' , '>i4' ), ('b' , '<f8' ), ('c' , 'i4' )]
809809 assert_ (not np .can_cast (a .dtype , t , casting = casting ))
810810
811+ def test_objview (self ):
812+ # https://github.com/numpy/numpy/issues/3286
813+ a = np .array ([], dtype = [('a' , 'f' ), ('b' , 'f' ), ('c' , 'O' )])
814+ a [['a' , 'b' ]] # TypeError?
815+
816+ # https://github.com/numpy/numpy/issues/3253
817+ dat2 = np .zeros (3 , [('A' , 'i' ), ('B' , '|O' )])
818+ new2 = dat2 [['B' , 'A' ]] # TypeError?
811819
812820class TestBool (TestCase ):
813821 def test_test_interning (self ):
@@ -3576,8 +3584,9 @@ def test_record_no_hash(self):
35763584
35773585class TestView (TestCase ):
35783586 def test_basic (self ):
3579- x = np .array ([(1 , 2 , 3 , 4 ), (5 , 6 , 7 , 8 )], dtype = [('r' , np .int8 ), ('g' , np .int8 ),
3580- ('b' , np .int8 ), ('a' , np .int8 )])
3587+ x = np .array ([(1 , 2 , 3 , 4 ), (5 , 6 , 7 , 8 )],
3588+ dtype = [('r' , np .int8 ), ('g' , np .int8 ),
3589+ ('b' , np .int8 ), ('a' , np .int8 )])
35813590 # We must be specific about the endianness here:
35823591 y = x .view (dtype = '<i4' )
35833592 # ... and again without the keyword.
@@ -5242,6 +5251,89 @@ def test_collections_hashable(self):
52425251 x = np .array ([])
52435252 self .assertFalse (isinstance (x , collections .Hashable ))
52445253
5254+ from numpy .core ._internal import _view_is_safe
5255+
5256+ class TestObjViewSafetyFuncs :
5257+ def test_view_safety (self ):
5258+ psize = dtype ('p' ).itemsize
5259+
5260+ # creates dtype but with extra character code - for missing 'p' fields
5261+ def mtype (s ):
5262+ n , offset , fields = 0 , 0 , []
5263+ for c in s .split (',' ): #subarrays won't work
5264+ if c != '-' :
5265+ fields .append (('f{0}' .format (n ), c , offset ))
5266+ n += 1
5267+ offset += dtype (c ).itemsize if c != '-' else psize
5268+
5269+ names , formats , offsets = zip (* fields )
5270+ return dtype ({'names' : names , 'formats' : formats ,
5271+ 'offsets' : offsets , 'itemsize' : offset })
5272+
5273+ # test nonequal itemsizes with objects:
5274+ # these should succeed:
5275+ _view_is_safe (dtype ('O,p,O,p' ), dtype ('O,p,O,p,O,p' ))
5276+ _view_is_safe (dtype ('O,O' ), dtype ('O,O,O' ))
5277+ # these should fail:
5278+ assert_raises (TypeError , _view_is_safe , dtype ('O,O,p' ), dtype ('O,O' ))
5279+ assert_raises (TypeError , _view_is_safe , dtype ('O,O,p' ), dtype ('O,p' ))
5280+ assert_raises (TypeError , _view_is_safe , dtype ('O,O,p' ), dtype ('p,O' ))
5281+
5282+ # test nonequal itemsizes with missing fields:
5283+ # these should succeed:
5284+ _view_is_safe (mtype ('-,p,-,p' ), mtype ('-,p,-,p,-,p' ))
5285+ _view_is_safe (dtype ('p,p' ), dtype ('p,p,p' ))
5286+ # these should fail:
5287+ assert_raises (TypeError , _view_is_safe , mtype ('p,p,-' ), mtype ('p,p' ))
5288+ assert_raises (TypeError , _view_is_safe , mtype ('p,p,-' ), mtype ('p,-' ))
5289+ assert_raises (TypeError , _view_is_safe , mtype ('p,p,-' ), mtype ('-,p' ))
5290+
5291+ # scans through positions at which we can view a type
5292+ def scanView (d1 , otype ):
5293+ goodpos = []
5294+ for shift in range (d1 .itemsize - dtype (otype ).itemsize + 1 ):
5295+ d2 = dtype ({'names' : ['f0' ], 'formats' : [otype ],
5296+ 'offsets' : [shift ], 'itemsize' : d1 .itemsize })
5297+ try :
5298+ _view_is_safe (d1 , d2 )
5299+ except TypeError :
5300+ pass
5301+ else :
5302+ goodpos .append (shift )
5303+ return goodpos
5304+
5305+ # test partial overlap with object field
5306+ assert_equal (scanView (dtype ('p,O,p,p,O,O' ), 'p' ),
5307+ [0 ] + list (range (2 * psize , 3 * psize + 1 )))
5308+ assert_equal (scanView (dtype ('p,O,p,p,O,O' ), 'O' ),
5309+ [psize , 4 * psize , 5 * psize ])
5310+
5311+ # test partial overlap with missing field
5312+ assert_equal (scanView (mtype ('p,-,p,p,-,-' ), 'p' ),
5313+ [0 ] + list (range (2 * psize , 3 * psize + 1 )))
5314+
5315+ # test nested structures with objects:
5316+ nestedO = dtype ([('f0' , 'p' ), ('f1' , 'p,O,p' )])
5317+ assert_equal (scanView (nestedO , 'p' ), list (range (psize + 1 )) + [3 * psize ])
5318+ assert_equal (scanView (nestedO , 'O' ), [2 * psize ])
5319+
5320+ # test nested structures with missing fields:
5321+ nestedM = dtype ([('f0' , 'p' ), ('f1' , mtype ('p,-,p' ))])
5322+ assert_equal (scanView (nestedM , 'p' ), list (range (psize + 1 )) + [3 * psize ])
5323+
5324+ # test subarrays with objects
5325+ subarrayO = dtype ('p,(2,3)O,p' )
5326+ assert_equal (scanView (subarrayO , 'p' ), [0 , 7 * psize ])
5327+ assert_equal (scanView (subarrayO , 'O' ),
5328+ list (range (psize , 6 * psize + 1 , psize )))
5329+
5330+ #test dtype with overlapping fields
5331+ overlapped = dtype ({'names' : ['f0' , 'f1' , 'f2' , 'f3' ],
5332+ 'formats' : ['p' , 'p' , 'p' , 'p' ],
5333+ 'offsets' : [0 , 1 , 3 * psize - 1 , 3 * psize ],
5334+ 'itemsize' : 4 * psize })
5335+ assert_equal (scanView (overlapped , 'p' ), [0 , 1 , 3 * psize - 1 , 3 * psize ])
5336+
52455337
52465338if __name__ == "__main__" :
52475339 run_module_suite ()
0 commit comments