10000 Merge pull request #3196 from astrofrog/issue-3196 · pytesnim/matplotlib@14475ab · GitHub
[go: up one dir, main page]

Skip to content

Commit 14475ab

Browse files
committed
Merge pull request matplotlib#3196 from astrofrog/issue-3196
Issue with iterability of axes arguments [backport to 1.4.x]
2 parents c7971b3 + 9f2b532 commit 14475ab

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

lib/matplotlib/figure.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,8 +785,14 @@ def fixitems(items):
785785
# to tuples for the key
786786
ret = []
787787
for k, v in items:
788-
if iterable(v):
788+
# some objects can define __getitem__ without being
789+
# iterable and in those cases the conversion to tuples
790+
# will fail. So instead of using the iterable(v) function
791+
# we simply try and convert to a tuple, and proceed if not.
792+
try:
789793
v = tuple(v)
794+
except Exception:
795+
pass
790796
ret.append((k, v))
791797
return tuple(ret)
792798

lib/matplotlib/tests/test_figure.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from nose.tools import assert_equal, assert_true, assert_raises
88
from matplotlib.testing.decorators import image_comparison, cleanup
9+
from matplotlib.axes import Axes
910
import matplotlib.pyplot as plt
1011

1112

@@ -110,6 +111,36 @@ def test_too_many_figures():
110111
assert len(w) == 1
111112

112113

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 = 5AE1 plt.figure()
140+
ax = fig.add_subplot(1, 1, 1, projection=MyClass())
141+
plt.close(fig)
142+
143+
113144
if __name__ == "__main__":
114145
import nose
115146
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)
0