图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

596 lines
22 KiB

  1. import os
  2. import sys
  3. import copy
  4. import platform
  5. import pytest
  6. import numpy as np
  7. from numpy.testing import assert_, assert_equal
  8. from numpy.core.multiarray import typeinfo
  9. from . import util
  10. wrap = None
  11. def setup_module():
  12. """
  13. Build the required testing extension module
  14. """
  15. global wrap
  16. # Check compiler availability first
  17. if not util.has_c_compiler():
  18. pytest.skip("No C compiler available")
  19. if wrap is None:
  20. config_code = """
  21. config.add_extension('test_array_from_pyobj_ext',
  22. sources=['wrapmodule.c', 'fortranobject.c'],
  23. define_macros=[])
  24. """
  25. d = os.path.dirname(__file__)
  26. src = [os.path.join(d, 'src', 'array_from_pyobj', 'wrapmodule.c'),
  27. os.path.join(d, '..', 'src', 'fortranobject.c'),
  28. os.path.join(d, '..', 'src', 'fortranobject.h')]
  29. wrap = util.build_module_distutils(src, config_code,
  30. 'test_array_from_pyobj_ext')
  31. def flags_info(arr):
  32. flags = wrap.array_attrs(arr)[6]
  33. return flags2names(flags)
  34. def flags2names(flags):
  35. info = []
  36. for flagname in ['CONTIGUOUS', 'FORTRAN', 'OWNDATA', 'ENSURECOPY',
  37. 'ENSUREARRAY', 'ALIGNED', 'NOTSWAPPED', 'WRITEABLE',
  38. 'WRITEBACKIFCOPY', 'UPDATEIFCOPY', 'BEHAVED', 'BEHAVED_RO',
  39. 'CARRAY', 'FARRAY'
  40. ]:
  41. if abs(flags) & getattr(wrap, flagname, 0):
  42. info.append(flagname)
  43. return info
  44. class Intent:
  45. def __init__(self, intent_list=[]):
  46. self.intent_list = intent_list[:]
  47. flags = 0
  48. for i in intent_list:
  49. if i == 'optional':
  50. flags |= wrap.F2PY_OPTIONAL
  51. else:
  52. flags |= getattr(wrap, 'F2PY_INTENT_' + i.upper())
  53. self.flags = flags
  54. def __getattr__(self, name):
  55. name = name.lower()
  56. if name == 'in_':
  57. name = 'in'
  58. return self.__class__(self.intent_list + [name])
  59. def __str__(self):
  60. return 'intent(%s)' % (','.join(self.intent_list))
  61. def __repr__(self):
  62. return 'Intent(%r)' % (self.intent_list)
  63. def is_intent(self, *names):
  64. for name in names:
  65. if name not in self.intent_list:
  66. return False
  67. return True
  68. def is_intent_exact(self, *names):
  69. return len(self.intent_list) == len(names) and self.is_intent(*names)
  70. intent = Intent()
  71. _type_names = ['BOOL', 'BYTE', 'UBYTE', 'SHORT', 'USHORT', 'INT', 'UINT',
  72. 'LONG', 'ULONG', 'LONGLONG', 'ULONGLONG',
  73. 'FLOAT', 'DOUBLE', 'CFLOAT']
  74. _cast_dict = {'BOOL': ['BOOL']}
  75. _cast_dict['BYTE'] = _cast_dict['BOOL'] + ['BYTE']
  76. _cast_dict['UBYTE'] = _cast_dict['BOOL'] + ['UBYTE']
  77. _cast_dict['BYTE'] = ['BYTE']
  78. _cast_dict['UBYTE'] = ['UBYTE']
  79. _cast_dict['SHORT'] = _cast_dict['BYTE'] + ['UBYTE', 'SHORT']
  80. _cast_dict['USHORT'] = _cast_dict['UBYTE'] + ['BYTE', 'USHORT']
  81. _cast_dict['INT'] = _cast_dict['SHORT'] + ['USHORT', 'INT']
  82. _cast_dict['UINT'] = _cast_dict['USHORT'] + ['SHORT', 'UINT']
  83. _cast_dict['LONG'] = _cast_dict['INT'] + ['LONG']
  84. _cast_dict['ULONG'] = _cast_dict['UINT'] + ['ULONG']
  85. _cast_dict['LONGLONG'] = _cast_dict['LONG'] + ['LONGLONG']
  86. _cast_dict['ULONGLONG'] = _cast_dict['ULONG'] + ['ULONGLONG']
  87. _cast_dict['FLOAT'] = _cast_dict['SHORT'] + ['USHORT', 'FLOAT']
  88. _cast_dict['DOUBLE'] = _cast_dict['INT'] + ['UINT', 'FLOAT', 'DOUBLE']
  89. _cast_dict['CFLOAT'] = _cast_dict['FLOAT'] + ['CFLOAT']
  90. # 32 bit system malloc typically does not provide the alignment required by
  91. # 16 byte long double types this means the inout intent cannot be satisfied
  92. # and several tests fail as the alignment flag can be randomly true or fals
  93. # when numpy gains an aligned allocator the tests could be enabled again
  94. #
  95. # Furthermore, on macOS ARM64, LONGDOUBLE is an alias for DOUBLE.
  96. if ((np.intp().dtype.itemsize != 4 or np.clongdouble().dtype.alignment <= 8) and
  97. sys.platform != 'win32' and
  98. (platform.system(), platform.processor()) != ('Darwin', 'arm')):
  99. _type_names.extend(['LONGDOUBLE', 'CDOUBLE', 'CLONGDOUBLE'])
  100. _cast_dict['LONGDOUBLE'] = _cast_dict['LONG'] + \
  101. ['ULONG', 'FLOAT', 'DOUBLE', 'LONGDOUBLE']
  102. _cast_dict['CLONGDOUBLE'] = _cast_dict['LONGDOUBLE'] + \
  103. ['CFLOAT', 'CDOUBLE', 'CLONGDOUBLE']
  104. _cast_dict['CDOUBLE'] = _cast_dict['DOUBLE'] + ['CFLOAT', 'CDOUBLE']
  105. class Type:
  106. _type_cache = {}
  107. def __new__(cls, name):
  108. if isinstance(name, np.dtype):
  109. dtype0 = name
  110. name = None
  111. for n, i in typeinfo.items():
  112. if not isinstance(i, type) and dtype0.type is i.type:
  113. name = n
  114. break
  115. obj = cls._type_cache.get(name.upper(), None)
  116. if obj is not None:
  117. return obj
  118. obj = object.__new__(cls)
  119. obj._init(name)
  120. cls._type_cache[name.upper()] = obj
  121. return obj
  122. def _init(self, name):
  123. self.NAME = name.upper()
  124. info = typeinfo[self.NAME]
  125. self.type_num = getattr(wrap, 'NPY_' + self.NAME)
  126. assert_equal(self.type_num, info.num)
  127. self.dtype = np.dtype(info.type)
  128. self.type = info.type
  129. self.elsize = info.bits / 8
  130. self.dtypechar = info.char
  131. def cast_types(self):
  132. return [self.__class__(_m) for _m in _cast_dict[self.NAME]]
  133. def all_types(self):
  134. return [self.__class__(_m) for _m in _type_names]
  135. def smaller_types(self):
  136. bits = typeinfo[self.NAME].alignment
  137. types = []
  138. for name in _type_names:
  139. if typeinfo[name].alignment < bits:
  140. types.append(Type(name))
  141. return types
  142. def equal_types(self):
  143. bits = typeinfo[self.NAME].alignment
  144. types = []
  145. for name in _type_names:
  146. if name == self.NAME:
  147. continue
  148. if typeinfo[name].alignment == bits:
  149. types.append(Type(name))
  150. return types
  151. def larger_types(self):
  152. bits = typeinfo[self.NAME].alignment
  153. types = []
  154. for name in _type_names:
  155. if typeinfo[name].alignment > bits:
  156. types.append(Type(name))
  157. return types
  158. class Array:
  159. def __init__(self, typ, dims, intent, obj):
  160. self.type = typ
  161. self.dims = dims
  162. self.intent = intent
  163. self.obj_copy = copy.deepcopy(obj)
  164. self.obj = obj
  165. # arr.dtypechar may be different from typ.dtypechar
  166. self.arr = wrap.call(typ.type_num, dims, intent.flags, obj)
  167. assert_(isinstance(self.arr, np.ndarray), repr(type(self.arr)))
  168. self.arr_attr = wrap.array_attrs(self.arr)
  169. if len(dims) > 1:
  170. if self.intent.is_intent('c'):
  171. assert_(intent.flags & wrap.F2PY_INTENT_C)
  172. assert_(not self.arr.flags['FORTRAN'],
  173. repr((self.arr.flags, getattr(obj, 'flags', None))))
  174. assert_(self.arr.flags['CONTIGUOUS'])
  175. assert_(not self.arr_attr[6] & wrap.FORTRAN)
  176. else:
  177. assert_(not intent.flags & wrap.F2PY_INTENT_C)
  178. assert_(self.arr.flags['FORTRAN'])
  179. assert_(not self.arr.flags['CONTIGUOUS'])
  180. assert_(self.arr_attr[6] & wrap.FORTRAN)
  181. if obj is None:
  182. self.pyarr = None
  183. self.pyarr_attr = None
  184. return
  185. if intent.is_intent('cache'):
  186. assert_(isinstance(obj, np.ndarray), repr(type(obj)))
  187. self.pyarr = np.array(obj).reshape(*dims).copy()
  188. else:
  189. self.pyarr = np.array(
  190. np.array(obj, dtype=typ.dtypechar).reshape(*dims),
  191. order=self.intent.is_intent('c') and 'C' or 'F')
  192. assert_(self.pyarr.dtype == typ,
  193. repr((self.pyarr.dtype, typ)))
  194. self.pyarr.setflags(write=self.arr.flags['WRITEABLE'])
  195. assert_(self.pyarr.flags['OWNDATA'], (obj, intent))
  196. self.pyarr_attr = wrap.array_attrs(self.pyarr)
  197. if len(dims) > 1:
  198. if self.intent.is_intent('c'):
  199. assert_(not self.pyarr.flags['FORTRAN'])
  200. assert_(self.pyarr.flags['CONTIGUOUS'])
  201. assert_(not self.pyarr_attr[6] & wrap.FORTRAN)
  202. else:
  203. assert_(self.pyarr.flags['FORTRAN'])
  204. assert_(not self.pyarr.flags['CONTIGUOUS'])
  205. assert_(self.pyarr_attr[6] & wrap.FORTRAN)
  206. assert_(self.arr_attr[1] == self.pyarr_attr[1]) # nd
  207. assert_(self.arr_attr[2] == self.pyarr_attr[2]) # dimensions
  208. if self.arr_attr[1] <= 1:
  209. assert_(self.arr_attr[3] == self.pyarr_attr[3],
  210. repr((self.arr_attr[3], self.pyarr_attr[3],
  211. self.arr.tobytes(), self.pyarr.tobytes()))) # strides
  212. assert_(self.arr_attr[5][-2:] == self.pyarr_attr[5][-2:],
  213. repr((self.arr_attr[5], self.pyarr_attr[5]))) # descr
  214. assert_(self.arr_attr[6] == self.pyarr_attr[6],
  215. repr((self.arr_attr[6], self.pyarr_attr[6],
  216. flags2names(0 * self.arr_attr[6] - self.pyarr_attr[6]),
  217. flags2names(self.arr_attr[6]), intent))) # flags
  218. if intent.is_intent('cache'):
  219. assert_(self.arr_attr[5][3] >= self.type.elsize,
  220. repr((self.arr_attr[5][3], self.type.elsize)))
  221. else:
  222. assert_(self.arr_attr[5][3] == self.type.elsize,
  223. repr((self.arr_attr[5][3], self.type.elsize)))
  224. assert_(self.arr_equal(self.pyarr, self.arr))
  225. if isinstance(self.obj, np.ndarray):
  226. if typ.elsize == Type(obj.dtype).elsize:
  227. if not intent.is_intent('copy') and self.arr_attr[1] <= 1:
  228. assert_(self.has_shared_memory())
  229. def arr_equal(self, arr1, arr2):
  230. if arr1.shape != arr2.shape:
  231. return False
  232. return (arr1 == arr2).all()
  233. def __str__(self):
  234. return str(self.arr)
  235. def has_shared_memory(self):
  236. """Check that created array shares data with input array.
  237. """
  238. if self.obj is self.arr:
  239. return True
  240. if not isinstance(self.obj, np.ndarray):
  241. return False
  242. obj_attr = wrap.array_attrs(self.obj)
  243. return obj_attr[0] == self.arr_attr[0]
  244. class TestIntent:
  245. def test_in_out(self):
  246. assert_equal(str(intent.in_.out), 'intent(in,out)')
  247. assert_(intent.in_.c.is_intent('c'))
  248. assert_(not intent.in_.c.is_intent_exact('c'))
  249. assert_(intent.in_.c.is_intent_exact('c', 'in'))
  250. assert_(intent.in_.c.is_intent_exact('in', 'c'))
  251. assert_(not intent.in_.is_intent('c'))
  252. class TestSharedMemory:
  253. num2seq = [1, 2]
  254. num23seq = [[1, 2, 3], [4, 5, 6]]
  255. @pytest.fixture(autouse=True, scope='class', params=_type_names)
  256. def setup_type(self, request):
  257. request.cls.type = Type(request.param)
  258. request.cls.array = lambda self, dims, intent, obj: \
  259. Array(Type(request.param), dims, intent, obj)
  260. def test_in_from_2seq(self):
  261. a = self.array([2], intent.in_, self.num2seq)
  262. assert_(not a.has_shared_memory())
  263. def test_in_from_2casttype(self):
  264. for t in self.type.cast_types():
  265. obj = np.array(self.num2seq, dtype=t.dtype)
  266. a = self.array([len(self.num2seq)], intent.in_, obj)
  267. if t.elsize == self.type.elsize:
  268. assert_(
  269. a.has_shared_memory(), repr((self.type.dtype, t.dtype)))
  270. else:
  271. assert_(not a.has_shared_memory(), repr(t.dtype))
  272. @pytest.mark.parametrize('write', ['w', 'ro'])
  273. @pytest.mark.parametrize('order', ['C', 'F'])
  274. @pytest.mark.parametrize('inp', ['2seq', '23seq'])
  275. def test_in_nocopy(self, write, order, inp):
  276. """Test if intent(in) array can be passed without copies
  277. """
  278. seq = getattr(self, 'num' + inp)
  279. obj = np.array(seq, dtype=self.type.dtype, order=order)
  280. obj.setflags(write=(write == 'w'))
  281. a = self.array(obj.shape, ((order=='C' and intent.in_.c) or intent.in_), obj)
  282. assert a.has_shared_memory()
  283. def test_inout_2seq(self):
  284. obj = np.array(self.num2seq, dtype=self.type.dtype)
  285. a = self.array([len(self.num2seq)], intent.inout, obj)
  286. assert_(a.has_shared_memory())
  287. try:
  288. a = self.array([2], intent.in_.inout, self.num2seq)
  289. except TypeError as msg:
  290. if not str(msg).startswith('failed to initialize intent'
  291. '(inout|inplace|cache) array'):
  292. raise
  293. else:
  294. raise SystemError('intent(inout) should have failed on sequence')
  295. def test_f_inout_23seq(self):
  296. obj = np.array(self.num23seq, dtype=self.type.dtype, order='F')
  297. shape = (len(self.num23seq), len(self.num23seq[0]))
  298. a = self.array(shape, intent.in_.inout, obj)
  299. assert_(a.has_shared_memory())
  300. obj = np.array(self.num23seq, dtype=self.type.dtype, order='C')
  301. shape = (len(self.num23seq), len(self.num23seq[0]))
  302. try:
  303. a = self.array(shape, intent.in_.inout, obj)
  304. except ValueError as msg:
  305. if not str(msg).startswith('failed to initialize intent'
  306. '(inout) array'):
  307. raise
  308. else:
  309. raise SystemError(
  310. 'intent(inout) should have failed on improper array')
  311. def test_c_inout_23seq(self):
  312. obj = np.array(self.num23seq, dtype=self.type.dtype)
  313. shape = (len(self.num23seq), len(self.num23seq[0]))
  314. a = self.array(shape, intent.in_.c.inout, obj)
  315. assert_(a.has_shared_memory())
  316. def test_in_copy_from_2casttype(self):
  317. for t in self.type.cast_types():
  318. obj = np.array(self.num2seq, dtype=t.dtype)
  319. a = self.array([len(self.num2seq)], intent.in_.copy, obj)
  320. assert_(not a.has_shared_memory(), repr(t.dtype))
  321. def test_c_in_from_23seq(self):
  322. a = self.array([len(self.num23seq), len(self.num23seq[0])],
  323. intent.in_, self.num23seq)
  324. assert_(not a.has_shared_memory())
  325. def test_in_from_23casttype(self):
  326. for t in self.type.cast_types():
  327. obj = np.array(self.num23seq, dtype=t.dtype)
  328. a = self.array([len(self.num23seq), len(self.num23seq[0])],
  329. intent.in_, obj)
  330. assert_(not a.has_shared_memory(), repr(t.dtype))
  331. def test_f_in_from_23casttype(self):
  332. for t in self.type.cast_types():
  333. obj = np.array(self.num23seq, dtype=t.dtype, order='F')
  334. a = self.array([len(self.num23seq), len(self.num23seq[0])],
  335. intent.in_, obj)
  336. if t.elsize == self.type.elsize:
  337. assert_(a.has_shared_memory(), repr(t.dtype))
  338. else:
  339. assert_(not a.has_shared_memory(), repr(t.dtype))
  340. def test_c_in_from_23casttype(self):
  341. for t in self.type.cast_types():
  342. obj = np.array(self.num23seq, dtype=t.dtype)
  343. a = self.array([len(self.num23seq), len(self.num23seq[0])],
  344. intent.in_.c, obj)
  345. if t.elsize == self.type.elsize:
  346. assert_(a.has_shared_memory(), repr(t.dtype))
  347. else:
  348. assert_(not a.has_shared_memory(), repr(t.dtype))
  349. def test_f_copy_in_from_23casttype(self):
  350. for t in self.type.cast_types():
  351. obj = np.array(self.num23seq, dtype=t.dtype, order='F')
  352. a = self.array([len(self.num23seq), len(self.num23seq[0])],
  353. intent.in_.copy, obj)
  354. assert_(not a.has_shared_memory(), repr(t.dtype))
  355. def test_c_copy_in_from_23casttype(self):
  356. for t in self.type.cast_types():
  357. obj = np.array(self.num23seq, dtype=t.dtype)
  358. a = self.array([len(self.num23seq), len(self.num23seq[0])],
  359. intent.in_.c.copy, obj)
  360. assert_(not a.has_shared_memory(), repr(t.dtype))
  361. def test_in_cache_from_2casttype(self):
  362. for t in self.type.all_types():
  363. if t.elsize != self.type.elsize:
  364. continue
  365. obj = np.array(self.num2seq, dtype=t.dtype)
  366. shape = (len(self.num2seq),)
  367. a = self.array(shape, intent.in_.c.cache, obj)
  368. assert_(a.has_shared_memory(), repr(t.dtype))
  369. a = self.array(shape, intent.in_.cache, obj)
  370. assert_(a.has_shared_memory(), repr(t.dtype))
  371. obj = np.array(self.num2seq, dtype=t.dtype, order='F')
  372. a = self.array(shape, intent.in_.c.cache, obj)
  373. assert_(a.has_shared_memory(), repr(t.dtype))
  374. a = self.array(shape, intent.in_.cache, obj)
  375. assert_(a.has_shared_memory(), repr(t.dtype))
  376. try:
  377. a = self.array(shape, intent.in_.cache, obj[::-1])
  378. except ValueError as msg:
  379. if not str(msg).startswith('failed to initialize'
  380. ' intent(cache) array'):
  381. raise
  382. else:
  383. raise SystemError(
  384. 'intent(cache) should have failed on multisegmented array')
  385. def test_in_cache_from_2casttype_failure(self):
  386. for t in self.type.all_types():
  387. if t.elsize >= self.type.elsize:
  388. continue
  389. obj = np.array(self.num2seq, dtype=t.dtype)
  390. shape = (len(self.num2seq),)
  391. try:
  392. self.array(shape, intent.in_.cache, obj) # Should succeed
  393. except ValueError as msg:
  394. if not str(msg).startswith('failed to initialize'
  395. ' intent(cache) array'):
  396. raise
  397. else:
  398. raise SystemError(
  399. 'intent(cache) should have failed on smaller array')
  400. def test_cache_hidden(self):
  401. shape = (2,)
  402. a = self.array(shape, intent.cache.hide, None)
  403. assert_(a.arr.shape == shape)
  404. shape = (2, 3)
  405. a = self.array(shape, intent.cache.hide, None)
  406. assert_(a.arr.shape == shape)
  407. shape = (-1, 3)
  408. try:
  409. a = self.array(shape, intent.cache.hide, None)
  410. except ValueError as msg:
  411. if not str(msg).startswith('failed to create intent'
  412. '(cache|hide)|optional array'):
  413. raise
  414. else:
  415. raise SystemError(
  416. 'intent(cache) should have failed on undefined dimensions')
  417. def test_hidden(self):
  418. shape = (2,)
  419. a = self.array(shape, intent.hide, None)
  420. assert_(a.arr.shape == shape)
  421. assert_(a.arr_equal(a.arr, np.zeros(shape, dtype=self.type.dtype)))
  422. shape = (2, 3)
  423. a = self.array(shape, intent.hide, None)
  424. assert_(a.arr.shape == shape)
  425. assert_(a.arr_equal(a.arr, np.zeros(shape, dtype=self.type.dtype)))
  426. assert_(a.arr.flags['FORTRAN'] and not a.arr.flags['CONTIGUOUS'])
  427. shape = (2, 3)
  428. a = self.array(shape, intent.c.hide, None)
  429. assert_(a.arr.shape == shape)
  430. assert_(a.arr_equal(a.arr, np.zeros(shape, dtype=self.type.dtype)))
  431. assert_(not a.arr.flags['FORTRAN'] and a.arr.flags['CONTIGUOUS'])
  432. shape = (-1, 3)
  433. try:
  434. a = self.array(shape, intent.hide, None)
  435. except ValueError as msg:
  436. if not str(msg).startswith('failed to create intent'
  437. '(cache|hide)|optional array'):
  438. raise
  439. else:
  440. raise SystemError('intent(hide) should have failed'
  441. ' on undefined dimensions')
  442. def test_optional_none(self):
  443. shape = (2,)
  444. a = self.array(shape, intent.optional, None)
  445. assert_(a.arr.shape == shape)
  446. assert_(a.arr_equal(a.arr, np.zeros(shape, dtype=self.type.dtype)))
  447. shape = (2, 3)
  448. a = self.array(shape, intent.optional, None)
  449. assert_(a.arr.shape == shape)
  450. assert_(a.arr_equal(a.arr, np.zeros(shape, dtype=self.type.dtype)))
  451. assert_(a.arr.flags['FORTRAN'] and not a.arr.flags['CONTIGUOUS'])
  452. shape = (2, 3)
  453. a = self.array(shape, intent.c.optional, None)
  454. assert_(a.arr.shape == shape)
  455. assert_(a.arr_equal(a.arr, np.zeros(shape, dtype=self.type.dtype)))
  456. assert_(not a.arr.flags['FORTRAN'] and a.arr.flags['CONTIGUOUS'])
  457. def test_optional_from_2seq(self):
  458. obj = self.num2seq
  459. shape = (len(obj),)
  460. a = self.array(shape, intent.optional, obj)
  461. assert_(a.arr.shape == shape)
  462. assert_(not a.has_shared_memory())
  463. def test_optional_from_23seq(self):
  464. obj = self.num23seq
  465. shape = (len(obj), len(obj[0]))
  466. a = self.array(shape, intent.optional, obj)
  467. assert_(a.arr.shape == shape)
  468. assert_(not a.has_shared_memory())
  469. a = self.array(shape, intent.optional.c, obj)
  470. assert_(a.arr.shape == shape)
  471. assert_(not a.has_shared_memory())
  472. def test_inplace(self):
  473. obj = np.array(self.num23seq, dtype=self.type.dtype)
  474. assert_(not obj.flags['FORTRAN'] and obj.flags['CONTIGUOUS'])
  475. shape = obj.shape
  476. a = self.array(shape, intent.inplace, obj)
  477. assert_(obj[1][2] == a.arr[1][2], repr((obj, a.arr)))
  478. a.arr[1][2] = 54
  479. assert_(obj[1][2] == a.arr[1][2] ==
  480. np.array(54, dtype=self.type.dtype), repr((obj, a.arr)))
  481. assert_(a.arr is obj)
  482. assert_(obj.flags['FORTRAN']) # obj attributes are changed inplace!
  483. assert_(not obj.flags['CONTIGUOUS'])
  484. def test_inplace_from_casttype(self):
  485. for t in self.type.cast_types():
  486. if t is self.type:
  487. continue
  488. obj = np.array(self.num23seq, dtype=t.dtype)
  489. assert_(obj.dtype.type == t.type)
  490. assert_(obj.dtype.type is not self.type.type)
  491. assert_(not obj.flags['FORTRAN'] and obj.flags['CONTIGUOUS'])
  492. shape = obj.shape
  493. a = self.array(shape, intent.inplace, obj)
  494. assert_(obj[1][2] == a.arr[1][2], repr((obj, a.arr)))
  495. a.arr[1][2] = 54
  496. assert_(obj[1][2] == a.arr[1][2] ==
  497. np.array(54, dtype=self.type.dtype), repr((obj, a.arr)))
  498. assert_(a.arr is obj)
  499. assert_(obj.flags['FORTRAN']) # obj attributes changed inplace!
  500. assert_(not obj.flags['CONTIGUOUS'])
  501. assert_(obj.dtype.type is self.type.type) # obj changed inplace!