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

Skip to content

Commit f132088

Browse files
committed
MNT: Handle malformed input better
1 parent 6606961 commit f132088

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

lib/matplotlib/figure.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,6 +1673,7 @@ def _process_layout(layout):
16731673
return unique_ids, nested
16741674

16751675
def _do_layout(gs, layout, unique_ids, nested):
1676+
16761677
rows, cols = layout.shape
16771678
output = dict()
16781679

@@ -1702,8 +1703,10 @@ def _do_layout(gs, layout, unique_ids, nested):
17021703
gs[slc], **{'label': str(name), **subplot_kw})
17031704

17041705
for (j, k), nested_layout in nested.items():
1705-
if not all(len(r) for r in nested_layout):
1706-
raise ValueError("All of the rows must be the same length")
1706+
if not all(len(r) == len(nested_layout[0])
1707+
for r in nested_layout):
1708+
raise ValueError(
1709+
"All of the rows must be the same length.")
17071710
nested_layout = np.asarray(nested_layout, dtype=object)
17081711
rows, cols = nested_layout.shape
17091712
gs_n = gs[j, k].subgridspec(rows, cols, **gridspec_kw)
@@ -1716,7 +1719,7 @@ def _do_layout(gs, layout, unique_ids, nested):
17161719
f"and the nested layout\n{nested_layout}")
17171720
output.update(nested_output)
17181721
return output
1719-
if not all(len(r) for r in layout):
1722+
if not all(len(r) == len(layout[0]) for r in layout):
17201723
raise ValueError("All of the rows must be the same length")
17211724
layout = np.asarray(layout, dtype=object)
17221725
rows, cols = layout.shape

lib/matplotlib/tests/test_figure.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,10 @@ def test_single_str_input(self, fig_test, fig_ref, str_pattern):
688688
# This more specific test fails 1/3 times
689689
# "There are duplicate keys {.B., .A.} between the outer layout"
690690
"There are duplicate keys .* between the outer layout"
691-
)
691+
),
692+
('AAA\nc\nBBB', 'All of the rows must be the same length'),
693+
([['A', [['B', 'C'], ['D']]], ['E', 'E']],
694+
'All of the rows must be the same length'),
692695
])
693696
def test_fail(self, x, match):
694697
fig = plt.figure()

0 commit comments

Comments
 (0)