test_array_from_pyobj.py 22 KB

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