|
19 | 19 | import abc
|
20 | 20 | import base64
|
21 | 21 | import contextlib
|
| 22 | +import copy |
22 | 23 | from io import BytesIO, TextIOWrapper
|
23 | 24 | import itertools
|
24 | 25 | import logging
|
@@ -1545,6 +1546,14 @@ def func(frame, *fargs) -> iterable_of_artists
|
1545 | 1546 | - If an iterable, then simply use the values provided. If the
|
1546 | 1547 | iterable has a length, it will override the *save_count* kwarg.
|
1547 | 1548 |
|
| 1549 | + Note that when using ``repeat=True`` (the default) *frames* must |
| 1550 | + be iterable multiple times. This is true for most common iterables. |
| 1551 | +
|
| 1552 | + However, e.g. generator expressions and possibly some custom classes |
| 1553 | + that implement the iterator protocol cannot be used. Technically we |
| 1554 | + require either ``iter(frames) is not frames`` or *frames* must |
| 1555 | + support ``copy.copy(frames)``). |
| 1556 | +
|
1548 | 1557 | - If an integer, then equivalent to passing ``range(frames)``
|
1549 | 1558 |
|
1550 | 1559 | - If a generator function, then must have the signature::
|
@@ -1623,6 +1632,19 @@ def __init__(self, fig, func, frames=None, init_func=None, fargs=None,
|
1623 | 1632 | elif callable(frames):
|
1624 | 1633 | self._iter_gen = frames
|
1625 | 1634 | elif np.iterable(frames):
|
| 1635 | + if iter(frames) is not frames or not kwargs.get('repeat', True): |
| 1636 | + self._iter_gen = lambda: iter(frames) |
| 1637 | + else: |
| 1638 | + # Since repeat=True we copy frames, to ensure that _iter_gen() |
| 1639 | + # returns a new iterator on each call. |
| 1640 | + try: |
| 1641 | + # Fail early if we cannot copy frames. |
| 1642 | + copy.copy(frames) |
| 1643 | + except TypeError: |
| 1644 | + raise ValueError( |
| 1645 | + 'frames must be iterable multiple times if ' |
| 1646 | + 'repeat=True.') |
| 1647 | + self._iter_gen = lambda: iter(copy.copy(frames)) |
1626 | 1648 | self._iter_gen = lambda: iter(frames)
|
1627 | 1649 | if hasattr(frames, '__len__'):
|
1628 | 1650 | self.save_count = len(frames)
|
|
0 commit comments