Fix test suite failure without numpy and improve module init diagnostics

Fixes #357.
This commit is contained in:
Dean Moldovan 2016-08-25 17:08:09 +02:00
parent 69b6246677
commit 23919174a7
4 changed files with 40 additions and 11 deletions

View File

@ -202,3 +202,27 @@ def pytest_namespace():
'requires_eigen_and_scipy': skipif(not have_eigen or not scipy, 'requires_eigen_and_scipy': skipif(not have_eigen or not scipy,
reason="eigen and/or scipy are not installed"), reason="eigen and/or scipy are not installed"),
} }
def _test_import_pybind11():
"""Early diagnostic for test module initialization errors
When there is an error during initialization, the first import will report the
real error while all subsequent imports will report nonsense. This import test
is done early (in the pytest configuration file, before any tests) in order to
avoid the noise of having all tests fail with identical error messages.
Any possible exception is caught here and reported manually *without* the stack
trace. This further reduces noise since the trace would only show pytest internals
which are not useful for debugging pybind11 module issues.
"""
# noinspection PyBroadException
try:
import pybind11_tests
except Exception as e:
print("Failed to import pybind11_tests from pytest:")
print(" {}: {}".format(type(e).__name__, e))
sys.exit(1)
_test_import_pybind11()

View File

@ -3,8 +3,7 @@ import pytest
with pytest.suppress(ImportError): with pytest.suppress(ImportError):
import numpy as np import numpy as np
ref = np.array([[ 0, 3, 0, 0, 0, 11],
ref = np.array([[ 0, 3, 0, 0, 0, 11],
[22, 0, 0, 0, 17, 11], [22, 0, 0, 0, 17, 11],
[ 7, 5, 0, 1, 0, 11], [ 7, 5, 0, 1, 0, 11],
[ 0, 0, 0, 0, 0, 11], [ 0, 0, 0, 0, 0, 11],

View File

@ -268,6 +268,12 @@ py::list test_dtype_methods() {
} }
void init_ex_numpy_dtypes(py::module &m) { void init_ex_numpy_dtypes(py::module &m) {
try {
py::module::import("numpy");
} catch (...) {
return;
}
PYBIND11_NUMPY_DTYPE(SimpleStruct, x, y, z); PYBIND11_NUMPY_DTYPE(SimpleStruct, x, y, z);
PYBIND11_NUMPY_DTYPE(PackedStruct, x, y, z); PYBIND11_NUMPY_DTYPE(PackedStruct, x, y, z);
PYBIND11_NUMPY_DTYPE(NestedStruct, a, b); PYBIND11_NUMPY_DTYPE(NestedStruct, a, b);

View File

@ -2,15 +2,15 @@ import pytest
with pytest.suppress(ImportError): with pytest.suppress(ImportError):
import numpy as np import numpy as np
simple_dtype = np.dtype({'names': ['x', 'y', 'z'],
'formats': ['?', 'u4', 'f4'],
'offsets': [0, 4, 8]})
packed_dtype = np.dtype([('x', '?'), ('y', 'u4'), ('z', 'f4')])
def assert_equal(actual, expected_data, expected_dtype): def assert_equal(actual, expected_data, expected_dtype):
np.testing.assert_equal(actual, np.array(expected_data, dtype=expected_dtype)) np.testing.assert_equal(actual, np.array(expected_data, dtype=expected_dtype))
simple_dtype = np.dtype({'names': ['x', 'y', 'z'],
'formats': ['?', 'u4', 'f4'],
'offsets': [0, 4, 8]})
packed_dtype = np.dtype([('x', '?'), ('y', 'u4'), ('z', 'f4')])
@pytest.requires_numpy @pytest.requires_numpy
def test_format_descriptors(): def test_format_descriptors():