Improve consistency of array and array_t with regard to other pytypes

* `array_t(const object &)` now throws on error
* `array_t::ensure()` is intended for casters —- old constructor is
  deprecated
* `array` and `array_t` get default constructors (empty array)
* `array` gets a converting constructor
* `py::isinstance<array_T<T>>()` checks the type (but not flags)

There is only one special thing which must remain: `array_t` gets
its own `type_caster` specialization which uses `ensure` instead
of a simple check.
This commit is contained in:
Dean Moldovan
2016-11-16 01:35:22 +01:00
committed by Wenzel Jakob
parent c7ac16bb2e
commit 4de271027d
4 changed files with 101 additions and 19 deletions

View File

@@ -245,3 +245,30 @@ def test_cast_numpy_int64_to_uint64():
from pybind11_tests.array import function_taking_uint64
function_taking_uint64(123)
function_taking_uint64(np.uint64(123))
@pytest.requires_numpy
def test_isinstance():
from pybind11_tests.array import isinstance_untyped, isinstance_typed
assert isinstance_untyped(np.array([1, 2, 3]), "not an array")
assert isinstance_typed(np.array([1.0, 2.0, 3.0]))
@pytest.requires_numpy
def test_constructors():
from pybind11_tests.array import default_constructors, converting_constructors
defaults = default_constructors()
for a in defaults.values():
assert a.size == 0
assert defaults["array"].dtype == np.array([]).dtype
assert defaults["array_t<int32>"].dtype == np.int32
assert defaults["array_t<double>"].dtype == np.float64
results = converting_constructors([1, 2, 3])
for a in results.values():
np.testing.assert_array_equal(a, [1, 2, 3])
assert results["array"].dtype == np.int_
assert results["array_t<int32>"].dtype == np.int32
assert results["array_t<double>"].dtype == np.float64