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

Function test_iter_object_arrays_basic

numpy/core/tests/test_nditer.py:1045–1082  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1043 op_dtypes=[np.dtype('i4')])
1044
1045def test_iter_object_arrays_basic():
1046 # Check that object arrays work
1047
1048 obj = {'a':3,'b':'d'}
1049 a = np.array([[1, 2, 3], None, obj, None], dtype='O')
1050 if HAS_REFCOUNT:
1051 rc = sys.getrefcount(obj)
1052
1053 # Need to allow references for object arrays
1054 assert_raises(TypeError, nditer, a)
1055 if HAS_REFCOUNT:
1056 assert_equal(sys.getrefcount(obj), rc)
1057
1058 i = nditer(a, ['refs_ok'], ['readonly'])
1059 vals = [x_[()] for x_ in i]
1060 assert_equal(np.array(vals, dtype='O'), a)
1061 vals, i, x = [None]*3
1062 if HAS_REFCOUNT:
1063 assert_equal(sys.getrefcount(obj), rc)
1064
1065 i = nditer(a.reshape(2, 2).T, ['refs_ok', 'buffered'],
1066 ['readonly'], order='C')
1067 assert_(i.iterationneedsapi)
1068 vals = [x_[()] for x_ in i]
1069 assert_equal(np.array(vals, dtype='O'), a.reshape(2, 2).ravel(order='F'))
1070 vals, i, x = [None]*3
1071 if HAS_REFCOUNT:
1072 assert_equal(sys.getrefcount(obj), rc)
1073
1074 i = nditer(a.reshape(2, 2).T, ['refs_ok', 'buffered'],
1075 ['readwrite'], order='C')
1076 with i:
1077 for x in i:
1078 x[...] = None
1079 vals, i, x = [None]*3
1080 if HAS_REFCOUNT:
1081 assert_(sys.getrefcount(obj) == rc-1)
1082 assert_equal(a, np.array([None]*4, dtype='O'))
1083
1084def test_iter_object_arrays_conversions():
1085 # Conversions to/from objects

Callers

nothing calls this directly

Calls 5

assert_raisesFunction · 0.90
assert_equalFunction · 0.90
assert_Function · 0.90
reshapeMethod · 0.80
ravelMethod · 0.45

Tested by

no test coverage detected