Benjamin Root, on 2011-01-04 19:48,  wrote:
> 2011/1/4 Paul Ivanov <pivanov...@gmail.com>
> 
> > 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
> Does this patch prevent the use of negative indexes?  If so, then we might
> want to rethink that in order to be more consistent with numpy arrays and
> python lists...

No, it does not prevent the use of negative indexes, and ensures
that the negative index is within the allowed bounds, so we do
the right thing. Here's the full patch for context:

lib/matplotlib/gridspec.py
169             else:
170                 if key < 0:
171                     key += total
172+                if key >= total or key < 0:
173+                    raise IndexError("index out of range")
174                 num1, num2 = key, None


As you can see, the reason line 172 has the key < 0 check is
because the allowed range of negative indexes gets dealt with on
the two preceding lines. So for gs=gridspec.GridSpec(1,2);
Both gs[-1] and gs[-2] are legal, but gs[-3] will raise an
IndexError.

-- 
Paul Ivanov
314 address only used for lists,  off-list direct email at:
http://pirsquared.org | GPG/PGP key id: 0x0F3E28F7 

Attachment: signature.asc
Description: Digital signature

------------------------------------------------------------------------------
Learn how Oracle Real Application Clusters (RAC) One Node allows customers
to consolidate database storage, standardize their database environment, and, 
should the need arise, upgrade to a full multi-node Oracle RAC database 
without downtime or disruption
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel

Reply via email to