summaryrefslogtreecommitdiff
path: root/numpy/f2py/tests/test_array_from_pyobj.py
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/f2py/tests/test_array_from_pyobj.py')
-rw-r--r--numpy/f2py/tests/test_array_from_pyobj.py234
1 files changed, 104 insertions, 130 deletions
diff --git a/numpy/f2py/tests/test_array_from_pyobj.py b/numpy/f2py/tests/test_array_from_pyobj.py
index 78569a8d6..f7c32f068 100644
--- a/numpy/f2py/tests/test_array_from_pyobj.py
+++ b/numpy/f2py/tests/test_array_from_pyobj.py
@@ -6,7 +6,6 @@ import pytest
import numpy as np
-from numpy.testing import assert_, assert_equal
from numpy.core.multiarray import typeinfo
from . import util
@@ -188,7 +187,7 @@ class Type:
self.NAME = name.upper()
info = typeinfo[self.NAME]
self.type_num = getattr(wrap, "NPY_" + self.NAME)
- assert_equal(self.type_num, info.num)
+ assert self.type_num == info.num
self.dtype = np.dtype(info.type)
self.type = info.type
self.elsize = info.bits / 8
@@ -238,24 +237,21 @@ class Array:
# arr.dtypechar may be different from typ.dtypechar
self.arr = wrap.call(typ.type_num, dims, intent.flags, obj)
- assert_(isinstance(self.arr, np.ndarray), repr(type(self.arr)))
+ assert isinstance(self.arr, np.ndarray)
self.arr_attr = wrap.array_attrs(self.arr)
if len(dims) > 1:
if self.intent.is_intent("c"):
- assert_(intent.flags & wrap.F2PY_INTENT_C)
- assert_(
- not self.arr.flags["FORTRAN"],
- repr((self.arr.flags, getattr(obj, "flags", None))),
- )
- assert_(self.arr.flags["CONTIGUOUS"])
- assert_(not self.arr_attr[6] & wrap.FORTRAN)
+ assert (intent.flags & wrap.F2PY_INTENT_C)
+ assert not self.arr.flags["FORTRAN"]
+ assert self.arr.flags["CONTIGUOUS"]
+ assert (not self.arr_attr[6] & wrap.FORTRAN)
else:
- assert_(not intent.flags & wrap.F2PY_INTENT_C)
- assert_(self.arr.flags["FORTRAN"])
- assert_(not self.arr.flags["CONTIGUOUS"])
- assert_(self.arr_attr[6] & wrap.FORTRAN)
+ assert (not intent.flags & wrap.F2PY_INTENT_C)
+ assert self.arr.flags["FORTRAN"]
+ assert not self.arr.flags["CONTIGUOUS"]
+ assert (self.arr_attr[6] & wrap.FORTRAN)
if obj is None:
self.pyarr = None
@@ -263,71 +259,56 @@ class Array:
return
if intent.is_intent("cache"):
- assert_(isinstance(obj, np.ndarray), repr(type(obj)))
+ assert isinstance(obj, np.ndarray), repr(type(obj))
self.pyarr = np.array(obj).reshape(*dims).copy()
else:
self.pyarr = np.array(
np.array(obj, dtype=typ.dtypechar).reshape(*dims),
order=self.intent.is_intent("c") and "C" or "F",
)
- assert_(self.pyarr.dtype == typ, repr((self.pyarr.dtype, typ)))
+ assert self.pyarr.dtype == typ
self.pyarr.setflags(write=self.arr.flags["WRITEABLE"])
- assert_(self.pyarr.flags["OWNDATA"], (obj, intent))
+ assert self.pyarr.flags["OWNDATA"], (obj, intent)
self.pyarr_attr = wrap.array_attrs(self.pyarr)
if len(dims) > 1:
if self.intent.is_intent("c"):
- assert_(not self.pyarr.flags["FORTRAN"])
- assert_(self.pyarr.flags["CONTIGUOUS"])
- assert_(not self.pyarr_attr[6] & wrap.FORTRAN)
+ assert not self.pyarr.flags["FORTRAN"]
+ assert self.pyarr.flags["CONTIGUOUS"]
+ assert (not self.pyarr_attr[6] & wrap.FORTRAN)
else:
- assert_(self.pyarr.flags["FORTRAN"])
- assert_(not self.pyarr.flags["CONTIGUOUS"])
- assert_(self.pyarr_attr[6] & wrap.FORTRAN)
+ assert self.pyarr.flags["FORTRAN"]
+ assert not self.pyarr.flags["CONTIGUOUS"]
+ assert (self.pyarr_attr[6] & wrap.FORTRAN)
- assert_(self.arr_attr[1] == self.pyarr_attr[1]) # nd
- assert_(self.arr_attr[2] == self.pyarr_attr[2]) # dimensions
+ assert self.arr_attr[1] == self.pyarr_attr[1] # nd
+ assert self.arr_attr[2] == self.pyarr_attr[2] # dimensions
if self.arr_attr[1] <= 1:
- assert_(
- self.arr_attr[3] == self.pyarr_attr[3],
- repr((
- self.arr_attr[3],
- self.pyarr_attr[3],
- self.arr.tobytes(),
- self.pyarr.tobytes(),
- )),
- ) # strides
- assert_(
- self.arr_attr[5][-2:] == self.pyarr_attr[5][-2:],
- repr((self.arr_attr[5], self.pyarr_attr[5])),
- ) # descr
- assert_(
- self.arr_attr[6] == self.pyarr_attr[6],
- repr((
- self.arr_attr[6],
- self.pyarr_attr[6],
- flags2names(0 * self.arr_attr[6] - self.pyarr_attr[6]),
- flags2names(self.arr_attr[6]),
- intent,
- )),
- ) # flags
+ assert self.arr_attr[3] == self.pyarr_attr[3], repr((
+ self.arr_attr[3],
+ self.pyarr_attr[3],
+ self.arr.tobytes(),
+ self.pyarr.tobytes(),
+ )) # strides
+ assert self.arr_attr[5][-2:] == self.pyarr_attr[5][-2:] # descr
+ assert self.arr_attr[6] == self.pyarr_attr[6], repr((
+ self.arr_attr[6],
+ self.pyarr_attr[6],
+ flags2names(0 * self.arr_attr[6] - self.pyarr_attr[6]),
+ flags2names(self.arr_attr[6]),
+ intent,
+ )) # flags
if intent.is_intent("cache"):
- assert_(
- self.arr_attr[5][3] >= self.type.elsize,
- repr((self.arr_attr[5][3], self.type.elsize)),
- )
+ assert self.arr_attr[5][3] >= self.type.elsize
else:
- assert_(
- self.arr_attr[5][3] == self.type.elsize,
- repr((self.arr_attr[5][3], self.type.elsize)),
- )
- assert_(self.arr_equal(self.pyarr, self.arr))
+ assert self.arr_attr[5][3] == self.type.elsize
+ assert (self.arr_equal(self.pyarr, self.arr))
if isinstance(self.obj, np.ndarray):
if typ.elsize == Type(obj.dtype).elsize:
if not intent.is_intent("copy") and self.arr_attr[1] <= 1:
- assert_(self.has_shared_memory())
+ assert self.has_shared_memory()
def arr_equal(self, arr1, arr2):
if arr1.shape != arr2.shape:
@@ -349,12 +330,12 @@ class Array:
class TestIntent:
def test_in_out(self):
- assert_equal(str(intent.in_.out), "intent(in,out)")
- assert_(intent.in_.c.is_intent("c"))
- assert_(not intent.in_.c.is_intent_exact("c"))
- assert_(intent.in_.c.is_intent_exact("c", "in"))
- assert_(intent.in_.c.is_intent_exact("in", "c"))
- assert_(not intent.in_.is_intent("c"))
+ assert str(intent.in_.out) == "intent(in,out)"
+ assert intent.in_.c.is_intent("c")
+ assert not intent.in_.c.is_intent_exact("c")
+ assert intent.in_.c.is_intent_exact("c", "in")
+ assert intent.in_.c.is_intent_exact("in", "c")
+ assert not intent.in_.is_intent("c")
class TestSharedMemory:
@@ -369,17 +350,16 @@ class TestSharedMemory:
def test_in_from_2seq(self):
a = self.array([2], intent.in_, self.num2seq)
- assert_(not a.has_shared_memory())
+ assert not a.has_shared_memory()
def test_in_from_2casttype(self):
for t in self.type.cast_types():
obj = np.array(self.num2seq, dtype=t.dtype)
a = self.array([len(self.num2seq)], intent.in_, obj)
if t.elsize == self.type.elsize:
- assert_(a.has_shared_memory(), repr(
- (self.type.dtype, t.dtype)))
+ assert a.has_shared_memory(), repr((self.type.dtype, t.dtype))
else:
- assert_(not a.has_shared_memory(), repr(t.dtype))
+ assert not a.has_shared_memory()
@pytest.mark.parametrize("write", ["w", "ro"])
@pytest.mark.parametrize("order", ["C", "F"])
@@ -396,7 +376,7 @@ class TestSharedMemory:
def test_inout_2seq(self):
obj = np.array(self.num2seq, dtype=self.type.dtype)
a = self.array([len(self.num2seq)], intent.inout, obj)
- assert_(a.has_shared_memory())
+ assert a.has_shared_memory()
try:
a = self.array([2], intent.in_.inout, self.num2seq)
@@ -411,7 +391,7 @@ class TestSharedMemory:
obj = np.array(self.num23seq, dtype=self.type.dtype, order="F")
shape = (len(self.num23seq), len(self.num23seq[0]))
a = self.array(shape, intent.in_.inout, obj)
- assert_(a.has_shared_memory())
+ assert a.has_shared_memory()
obj = np.array(self.num23seq, dtype=self.type.dtype, order="C")
shape = (len(self.num23seq), len(self.num23seq[0]))
@@ -429,26 +409,26 @@ class TestSharedMemory:
obj = np.array(self.num23seq, dtype=self.type.dtype)
shape = (len(self.num23seq), len(self.num23seq[0]))
a = self.array(shape, intent.in_.c.inout, obj)
- assert_(a.has_shared_memory())
+ assert a.has_shared_memory()
def test_in_copy_from_2casttype(self):
for t in self.type.cast_types():
obj = np.array(self.num2seq, dtype=t.dtype)
a = self.array([len(self.num2seq)], intent.in_.copy, obj)
- assert_(not a.has_shared_memory(), repr(t.dtype))
+ assert not a.has_shared_memory()
def test_c_in_from_23seq(self):
a = self.array(
[len(self.num23seq), len(self.num23seq[0])], intent.in_,
self.num23seq)
- assert_(not a.has_shared_memory())
+ assert not a.has_shared_memory()
def test_in_from_23casttype(self):
for t in self.type.cast_types():
obj = np.array(self.num23seq, dtype=t.dtype)
a = self.array(
[len(self.num23seq), len(self.num23seq[0])], intent.in_, obj)
- assert_(not a.has_shared_memory(), repr(t.dtype))
+ assert not a.has_shared_memory()
def test_f_in_from_23casttype(self):
for t in self.type.cast_types():
@@ -456,9 +436,9 @@ class TestSharedMemory:
a = self.array(
[len(self.num23seq), len(self.num23seq[0])], intent.in_, obj)
if t.elsize == self.type.elsize:
- assert_(a.has_shared_memory(), repr(t.dtype))
+ assert a.has_shared_memory()
else:
- assert_(not a.has_shared_memory(), repr(t.dtype))
+ assert not a.has_shared_memory()
def test_c_in_from_23casttype(self):
for t in self.type.cast_types():
@@ -466,9 +446,9 @@ class TestSharedMemory:
a = self.array(
[len(self.num23seq), len(self.num23seq[0])], intent.in_.c, obj)
if t.elsize == self.type.elsize:
- assert_(a.has_shared_memory(), repr(t.dtype))
+ assert a.has_shared_memory()
else:
- assert_(not a.has_shared_memory(), repr(t.dtype))
+ assert not a.has_shared_memory()
def test_f_copy_in_from_23casttype(self):
for t in self.type.cast_types():
@@ -476,7 +456,7 @@ class TestSharedMemory:
a = self.array(
[len(self.num23seq), len(self.num23seq[0])], intent.in_.copy,
obj)
- assert_(not a.has_shared_memory(), repr(t.dtype))
+ assert not a.has_shared_memory()
def test_c_copy_in_from_23casttype(self):
for t in self.type.cast_types():
@@ -484,7 +464,7 @@ class TestSharedMemory:
a = self.array(
[len(self.num23seq), len(self.num23seq[0])], intent.in_.c.copy,
obj)
- assert_(not a.has_shared_memory(), repr(t.dtype))
+ assert not a.has_shared_memory()
def test_in_cache_from_2casttype(self):
for t in self.type.all_types():
@@ -493,17 +473,17 @@ class TestSharedMemory:
obj = np.array(self.num2seq, dtype=t.dtype)
shape = (len(self.num2seq), )
a = self.array(shape, intent.in_.c.cache, obj)
- assert_(a.has_shared_memory(), repr(t.dtype))
+ assert a.has_shared_memory()
a = self.array(shape, intent.in_.cache, obj)
- assert_(a.has_shared_memory(), repr(t.dtype))
+ assert a.has_shared_memory()
obj = np.array(self.num2seq, dtype=t.dtype, order="F")
a = self.array(shape, intent.in_.c.cache, obj)
- assert_(a.has_shared_memory(), repr(t.dtype))
+ assert a.has_shared_memory()
a = self.array(shape, intent.in_.cache, obj)
- assert_(a.has_shared_memory(), repr(t.dtype))
+ assert a.has_shared_memory(), repr(t.dtype)
try:
a = self.array(shape, intent.in_.cache, obj[::-1])
@@ -534,11 +514,11 @@ class TestSharedMemory:
def test_cache_hidden(self):
shape = (2, )
a = self.array(shape, intent.cache.hide, None)
- assert_(a.arr.shape == shape)
+ assert a.arr.shape == shape
shape = (2, 3)
a = self.array(shape, intent.cache.hide, None)
- assert_(a.arr.shape == shape)
+ assert a.arr.shape == shape
shape = (-1, 3)
try:
@@ -554,20 +534,20 @@ class TestSharedMemory:
def test_hidden(self):
shape = (2, )
a = self.array(shape, intent.hide, None)
- assert_(a.arr.shape == shape)
- assert_(a.arr_equal(a.arr, np.zeros(shape, dtype=self.type.dtype)))
+ assert a.arr.shape == shape
+ assert a.arr_equal(a.arr, np.zeros(shape, dtype=self.type.dtype))
shape = (2, 3)
a = self.array(shape, intent.hide, None)
- assert_(a.arr.shape == shape)
- assert_(a.arr_equal(a.arr, np.zeros(shape, dtype=self.type.dtype)))
- assert_(a.arr.flags["FORTRAN"] and not a.arr.flags["CONTIGUOUS"])
+ assert a.arr.shape == shape
+ assert a.arr_equal(a.arr, np.zeros(shape, dtype=self.type.dtype))
+ assert a.arr.flags["FORTRAN"] and not a.arr.flags["CONTIGUOUS"]
shape = (2, 3)
a = self.array(shape, intent.c.hide, None)
- assert_(a.arr.shape == shape)
- assert_(a.arr_equal(a.arr, np.zeros(shape, dtype=self.type.dtype)))
- assert_(not a.arr.flags["FORTRAN"] and a.arr.flags["CONTIGUOUS"])
+ assert a.arr.shape == shape
+ assert a.arr_equal(a.arr, np.zeros(shape, dtype=self.type.dtype))
+ assert not a.arr.flags["FORTRAN"] and a.arr.flags["CONTIGUOUS"]
shape = (-1, 3)
try:
@@ -583,72 +563,66 @@ class TestSharedMemory:
def test_optional_none(self):
shape = (2, )
a = self.array(shape, intent.optional, None)
- assert_(a.arr.shape == shape)
- assert_(a.arr_equal(a.arr, np.zeros(shape, dtype=self.type.dtype)))
+ assert a.arr.shape == shape
+ assert a.arr_equal(a.arr, np.zeros(shape, dtype=self.type.dtype))
shape = (2, 3)
a = self.array(shape, intent.optional, None)
- assert_(a.arr.shape == shape)
- assert_(a.arr_equal(a.arr, np.zeros(shape, dtype=self.type.dtype)))
- assert_(a.arr.flags["FORTRAN"] and not a.arr.flags["CONTIGUOUS"])
+ assert a.arr.shape == shape
+ assert a.arr_equal(a.arr, np.zeros(shape, dtype=self.type.dtype))
+ assert a.arr.flags["FORTRAN"] and not a.arr.flags["CONTIGUOUS"]
shape = (2, 3)
a = self.array(shape, intent.c.optional, None)
- assert_(a.arr.shape == shape)
- assert_(a.arr_equal(a.arr, np.zeros(shape, dtype=self.type.dtype)))
- assert_(not a.arr.flags["FORTRAN"] and a.arr.flags["CONTIGUOUS"])
+ assert a.arr.shape == shape
+ assert a.arr_equal(a.arr, np.zeros(shape, dtype=self.type.dtype))
+ assert not a.arr.flags["FORTRAN"] and a.arr.flags["CONTIGUOUS"]
def test_optional_from_2seq(self):
obj = self.num2seq
shape = (len(obj), )
a = self.array(shape, intent.optional, obj)
- assert_(a.arr.shape == shape)
- assert_(not a.has_shared_memory())
+ assert a.arr.shape == shape
+ assert not a.has_shared_memory()
def test_optional_from_23seq(self):
obj = self.num23seq
shape = (len(obj), len(obj[0]))
a = self.array(shape, intent.optional, obj)
- assert_(a.arr.shape == shape)
- assert_(not a.has_shared_memory())
+ assert a.arr.shape == shape
+ assert not a.has_shared_memory()
a = self.array(shape, intent.optional.c, obj)
- assert_(a.arr.shape == shape)
- assert_(not a.has_shared_memory())
+ assert a.arr.shape == shape
+ assert not a.has_shared_memory()
def test_inplace(self):
obj = np.array(self.num23seq, dtype=self.type.dtype)
- assert_(not obj.flags["FORTRAN"] and obj.flags["CONTIGUOUS"])
+ assert not obj.flags["FORTRAN"] and obj.flags["CONTIGUOUS"]
shape = obj.shape
a = self.array(shape, intent.inplace, obj)
- assert_(obj[1][2] == a.arr[1][2], repr((obj, a.arr)))
+ assert obj[1][2] == a.arr[1][2], repr((obj, a.arr))
a.arr[1][2] = 54
- assert_(
- obj[1][2] == a.arr[1][2] == np.array(54, dtype=self.type.dtype),
- repr((obj, a.arr)),
- )
- assert_(a.arr is obj)
- assert_(obj.flags["FORTRAN"]) # obj attributes are changed inplace!
- assert_(not obj.flags["CONTIGUOUS"])
+ assert obj[1][2] == a.arr[1][2] == np.array(54, dtype=self.type.dtype)
+ assert a.arr is obj
+ assert obj.flags["FORTRAN"] # obj attributes are changed inplace!
+ assert not obj.flags["CONTIGUOUS"]
def test_inplace_from_casttype(self):
for t in self.type.cast_types():
if t is self.type:
continue
obj = np.array(self.num23seq, dtype=t.dtype)
- assert_(obj.dtype.type == t.type)
- assert_(obj.dtype.type is not self.type.type)
- assert_(not obj.flags["FORTRAN"] and obj.flags["CONTIGUOUS"])
+ assert obj.dtype.type == t.type
+ assert obj.dtype.type is not self.type.type
+ assert not obj.flags["FORTRAN"] and obj.flags["CONTIGUOUS"]
shape = obj.shape
a = self.array(shape, intent.inplace, obj)
- assert_(obj[1][2] == a.arr[1][2], repr((obj, a.arr)))
+ assert obj[1][2] == a.arr[1][2], repr((obj, a.arr))
a.arr[1][2] = 54
- assert_(
- obj[1][2] == a.arr[1][2] == np.array(54,
- dtype=self.type.dtype),
- repr((obj, a.arr)),
- )
- assert_(a.arr is obj)
- assert_(obj.flags["FORTRAN"]) # obj attributes changed inplace!
- assert_(not obj.flags["CONTIGUOUS"])
- assert_(obj.dtype.type is self.type.type) # obj changed inplace!
+ assert obj[1][2] == a.arr[1][2] == np.array(54,
+ dtype=self.type.dtype)
+ assert a.arr is obj
+ assert obj.flags["FORTRAN"] # obj attributes changed inplace!
+ assert not obj.flags["CONTIGUOUS"]
+ assert obj.dtype.type is self.type.type # obj changed inplace!