Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/matplotlib/tests/test_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ def test_lw_scaling():


def test_is_sorted_and_has_non_nan():
with pytest.raises(ValueError):
_path.is_sorted_and_has_non_nan(None)
with pytest.raises(ValueError):
_path.is_sorted_and_has_non_nan([[[[None]], None]])
with pytest.raises(ValueError):
print(_path.is_sorted_and_has_non_nan([""]))
assert _path.is_sorted_and_has_non_nan(np.array([1, 2, 3]))
assert _path.is_sorted_and_has_non_nan(np.array([1, np.nan, 3]))
assert not _path.is_sorted_and_has_non_nan([3, 5] + [np.nan] * 100 + [0, 2])
Expand Down
25 changes: 25 additions & 0 deletions lib/matplotlib/tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import matplotlib.transforms as mtransforms
from matplotlib.transforms import Affine2D, Bbox, TransformedBbox, _ScaledRotation
from matplotlib.path import Path
from matplotlib._path import count_bboxes_overlapping_bbox
from matplotlib.testing.decorators import image_comparison, check_figures_equal
from unittest.mock import MagicMock

Expand Down Expand Up @@ -900,6 +901,30 @@ def test_bbox_as_strings():
assert eval(format(getattr(b, k), fmt)) == v


def test_count_bboxes_overlapping_bbox():
for invalid_bbox in [
[[[[None]], None]],
[1, 2, 3],
[],
[[1], [2]],
]:
with pytest.raises(ValueError):
count_bboxes_overlapping_bbox(invalid_bbox, None)

corners = (
[[0, 0], [2, 2]],
[[8, 0], [10, 2]],
[[0, 8], [2, 10]],
[[8, 8], [10, 10]],
)
center = [[4, 4], [6, 6]]
bbox = [[0, 0], [10, 10]]

assert count_bboxes_overlapping_bbox(bbox, corners) == 4
assert count_bboxes_overlapping_bbox(bbox, (center, )) == 1
assert count_bboxes_overlapping_bbox(bbox, (center, *corners)) == 5


def test_str_transform():
# The str here should not be considered as "absolutely stable", and may be
# reformatted later; this is just a smoketest for __str__.
Expand Down
3 changes: 3 additions & 0 deletions src/_image_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ image_resample(py::array input_array,

// Ensure input array is contiguous, regardless of dtype
input_array = py::array::ensure(input_array, py::array::c_style);
if (!input_array) {
throw std::invalid_argument("Input array could not be made C-contiguous");

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this is likely unnecessary. I couldn't figure out a way to make this ensure() return a null. That said, throwing an exception is better than crashing.

}

// Validate output array
auto out_ndim = output_array.ndim();
Expand Down
5 changes: 4 additions & 1 deletion src/_path_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ Py_is_sorted_and_has_non_nan(py::object obj)
bool result;

py::array array = py::array::ensure(obj);
if (array.ndim() != 1) {
if (!array || array.ndim() != 1) {
Comment thread
QuLogic marked this conversation as resolved.
throw std::invalid_argument("array must be 1D");
}

Expand All @@ -300,6 +300,9 @@ Py_is_sorted_and_has_non_nan(py::object obj)
result = is_sorted_and_has_non_nan<double>(array);
} else {
array = py::array_t<double>::ensure(obj);
if (!array) {
throw std::invalid_argument("Could not coerce array to double");
}
result = is_sorted_and_has_non_nan<double>(array);
}

Expand Down
5 changes: 3 additions & 2 deletions src/py_converters.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ namespace PYBIND11_NAMESPACE { namespace detail {
}

auto rect_arr = py::array_t<double>::ensure(src);
auto ndim = rect_arr ? rect_arr.ndim() : 0;

if (rect_arr.ndim() == 2) {
if (ndim == 2) {
if (rect_arr.shape(0) != 2 || rect_arr.shape(1) != 2) {
throw py::value_error("Invalid bounding box");
}
Expand All @@ -71,7 +72,7 @@ namespace PYBIND11_NAMESPACE { namespace detail {
value.x2 = *rect_arr.data(1, 0);
value.y2 = *rect_arr.data(1, 1);

} else if (rect_arr.ndim() == 1) {
} else if (ndim == 1) {
if (rect_arr.shape(0) != 4) {
throw py::value_error("Invalid bounding box");
}
Expand Down
Loading