|
6 | 6 |
|
7 | 7 | from nose.tools import assert_equal, assert_true, assert_raises
|
8 | 8 | from matplotlib.testing.decorators import image_comparison, cleanup
|
| 9 | +from matplotlib.axes import Axes |
9 | 10 | import matplotlib.pyplot as plt
|
10 | 11 |
|
11 | 12 |
|
@@ -110,6 +111,36 @@ def test_too_many_figures():
|
110 | 111 | assert len(w) == 1
|
111 | 112 |
|
112 | 113 |
|
| 114 | +def test_iterability_axes_argument(): |
| 115 | + |
| 116 | + # This is a regression test for matplotlib/matplotlib#3196. If one of the |
| 117 | + # arguments returned by _as_mpl_axes defines __getitem__ but is not |
| 118 | + # iterable, this would raise an execption. This is because we check |
| 119 | + # whether the arguments are iterable, and if so we try and convert them |
| 120 | + # to a tuple. However, the ``iterable`` function returns True if |
| 121 | + # __getitem__ is present, but some classes can define __getitem__ without |
| 122 | + # being iterable. The tuple conversion is now done in a try...except in |
| 123 | + # case it fails. |
| 124 | + |
| 125 | + class MyAxes(Axes): |
| 126 | + def __init__(self, *args, **kwargs): |
| 127 | + kwargs.pop('myclass', None) |
| 128 | + return Axes.__init__(self, *args, **kwargs) |
| 129 | + |
| 130 | + class MyClass(object): |
| 131 | + |
| 132 | + def __getitem__(self, item): |
| 133 | + if item != 'a': |
| 134 | + raise ValueError("item should be a") |
| 135 | + |
| 136 | + def _as_mpl_axes(self): |
| 137 | + return MyAxes, {'myclass': self} |
| 138 | + |
| 139 | + fig = plt.figure() |
| 140 | + ax = fig.add_subplot(1, 1, 1, projection=MyClass()) |
| 141 | + plt.close(fig) |
| 142 | + |
| 143 | + |
113 | 144 | if __name__ == "__main__":
|
114 | 145 | import nose
|
115 | 146 | nose.runmodule(argv=['-s', '--with-doctest'], exit=False)
|
0 commit comments