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

Skip to content

Commit bdaaf59

Browse files
committed
MNT: raise exception if generators passed in
- moved next(iter(obj)) logic into a cbook function
1 parent 1ba3a51 commit bdaaf59

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2331,11 +2331,11 @@ def broken_barh(self, xranges, yrange, **kwargs):
23312331
"""
23322332
# process the unit information
23332333
if len(xranges):
2334-
xdata = next(iter(xranges))
2334+
xdata = cbook.safe_first_element(xranges)
23352335
else:
23362336
xdata = None
23372337
if len(yrange):
2338-
ydata = next(iter(yrange))
2338+
ydata = cbook.safe_first_element(yrange)
23392339
else:
23402340
ydata = None
23412341
self._process_unit_info(xdata=xdata,
@@ -5886,7 +5886,8 @@ def _normalize_input(inp, ename='input'):
58865886
Name to use in ValueError if `inp` can not be normalized
58875887
58885888
"""
5889-
if isinstance(x, np.ndarray) or not iterable(next(iter(inp))):
5889+
if (isinstance(x, np.ndarray) or
5890+
not iterable(cbook.safe_first_element(inp))):
58905891
# TODO: support masked arrays;
58915892
inp = np.asarray(inp)
58925893
if inp.ndim == 2:

lib/matplotlib/cbook.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from matplotlib.externals import six
1313
from matplotlib.externals.six.moves import xrange, zip
1414
from itertools import repeat
15+
import collections
1516

1617
import datetime
1718
import errno
@@ -2536,6 +2537,13 @@ def index_of(y):
25362537
return np.arange(y.shape[0], dtype=float), y
25372538

25382539

2540+
def safe_first_element(obj):
2541+
if isinstance(obj, collections.Iterator):
2542+
raise RuntimeError("matplotlib does not support generators "
2543+
"as input")
2544+
return next(iter(obj))
2545+
2546+
25392547
def get_label(y, default_name):
25402548
try:
25412549
return y.name

lib/matplotlib/dates.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1561,7 +1561,7 @@ def default_units(x, axis):
15611561
x = x.ravel()
15621562

15631563
try:
1564-
x = next(iter(x))
1564+
x = cbook.safe_first_element(x)
15651565
except (TypeError, StopIteration):
15661566
pass
15671567

0 commit comments

Comments
 (0)