Hi matplotlib developers,
I was answering a question on the -users list and came across a
gridspec __getitem__ bug which causes an infinite loop if the
user tries to iterate over it, because getitem never did a
sanity-check when given an integer key.
from matplotlib import gridspec
gs = gridspec.GridSpec(1,2)
gs[100] # no error is given, a SubplotSpec is returned
[x for x in gs] # causes infinite loop before applying patch
# after applying the patch - which is just lines 171-172 in the
# traceback below
In [9]: gs[100]
gridspec_doc.diff (2.3 KB)
gridspec_getitem.diff (459 Bytes)
···
----------------------------------------------------------
IndexError Traceback (most recent call last)
./matplotlib/<ipython console> in <module>()
./matplotlib/gridspec.pyc
in __getitem__(self, key)
170 key += total
171 if key >= total or key < 0:
--> 172 raise IndexError("index out of range")
173 num1, num2 = key, None
174
IndexError: index out of range
In [10]: [x for x in gs]
Out[10]:
[<matplotlib.gridspec.SubplotSpec object at 0x9b7edcc>,
<matplotlib.gridspec.SubplotSpec object at 0x9b8834c>]
I'm also including a patch for the gridspec docs which create a
grid-of-grids using gridspec in a colorful manner. Note that
there, I explicitly create indexes in the right range, like so:
outer_grid = GridSpec(4, 4)
for i in xrange(16):
inner_grid = GridSpecFromSubplotSpec(3, 3, subplot_spec=outer_grid[i])
for j in xrange(9):
ax = plt.Subplot(f, inner_grid[j])
...
because before applying the getitem patch described above, one
*can't* currently iterate over the spec itself like this:
outer_grid = GridSpec(4, 4)
for cell in outer_grid:
inner_grid = GridSpecFromSubplotSpec(3, 3, subplot_spec=cell)
for subcell in inner_grid:
ax = plt.Subplot(f, subcell)
...
The doc patch also fixes the currently broken .. _gridspec-guide:
anchor name which can be seen peeking out at the top of:
http://matplotlib.sourceforge.net/users/gridspec.html
because there was previously a misplaced leading \ on line 1 of
doc/users/gridspec.rst
best,
--
Paul Ivanov
314 address only used for lists, off-list direct email at:
http://pirsquared.org | GPG/PGP key id: 0x0F3E28F7