File tree Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -2544,6 +2544,15 @@ def index_of(y):
2544
2544
2545
2545
def safe_first_element (obj ):
2546
2546
if isinstance (obj , collections .Iterator ):
2547
+ # needed to accept `array.flat` as input.
2548
+ # np.flatiter reports as an instance of collections.Iterator
2549
+ # but can still be indexed via [].
2550
+ # This has the side effect of re-setting the iterator, but
2551
+ # that is acceptable.
2552
+ try :
2553
+ return obj [0 ]
2554
+ except TypeError :
2555
+ pass
2547
2556
raise RuntimeError ("matplotlib does not support generators "
2548
2557
"as input" )
2549
2558
return next (iter (obj ))
Original file line number Diff line number Diff line change @@ -499,3 +499,15 @@ class dummy():
499
499
base_set = mapping [ref (objs [0 ])]
500
500
for o in objs [1 :]:
501
501
assert mapping [ref (o )] is base_set
502
+
503
+
504
+ def test_flatiter ():
505
+ x = np .arange (5 )
506
+ it = x .flat
507
+ assert 0 == next (it )
508
+ assert 1 == next (it )
509
+ ret = cbook .safe_first_element (it )
510
+ assert ret == 0
511
+
512
+ assert 0 == next (it )
513
+ assert 1 == next (it )
You can’t perform that action at this time.
0 commit comments