X Tutup
Skip to content

Add short-circuit return to matplotlib.artist.setp if input is length 0#7801

Merged
tacaswell merged 2 commits intomatplotlib:masterfrom
samsontmr:fix-#7784
Jan 13, 2017
Merged

Add short-circuit return to matplotlib.artist.setp if input is length 0#7801
tacaswell merged 2 commits intomatplotlib:masterfrom
samsontmr:fix-#7784

Conversation

@samsontmr
Copy link
Copy Markdown
Contributor

@samsontmr samsontmr commented Jan 11, 2017

Fixes #7784

Before this patch, an IndexError is raised in matplotlib.artist.setp if argument is an empty list,

import matplotlib
matplotlib.artist.setp([])

@samsontmr samsontmr changed the title Add short-circuit return if input is length 0 Add short-circuit return to matplotlib.artist.setp if input is length 0 Jan 11, 2017
@samsontmr samsontmr changed the title Add short-circuit return to matplotlib.artist.setp if input is length 0 Add short-circuit return to matplotlib.artist.setp if input is length 0 Jan 11, 2017
>>> setp(lines, linewidth=2, color='r') # python style
"""

if ((isinstance(obj, np.ndarray) and not obj.size) or
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just check if not objs: instead (objs is a list defined immediately below).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't if not objs return false if objs is a list of empty lists? Or do you mean to check for if not obj?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree using if not objs: would be better. I would also move this down to L1453 (right above the line with objs[0]) after we have normalized the input to a list.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see! The if else statements guarantee a list, right?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

@tacaswell tacaswell added this to the 2.0.1 (next bug fix release) milestone Jan 11, 2017
@samsontmr
Copy link
Copy Markdown
Contributor Author

Commits squashed

@tacaswell
Copy link
Copy Markdown
Member

Looks good! Could you also add a test to test_artist.py that exercises this code path?

@phobson
Copy link
Copy Markdown
Member

phobson commented Jan 11, 2017

Apologies if I'm missing something obvious here, but couldn't this be at the very top of the function? e.g.,

    if not objs:
        return
    elif not cbook.iterable(obj):
        objs = [obj]
    else:
        objs = list(cbook.flatten(obj))

@tacaswell
Copy link
Copy Markdown
Member

@phobson users will find pathological cases 😉

In [62]: bool([[]])
Out[62]: 
True

@phobson
Copy link
Copy Markdown
Member

phobson commented Jan 11, 2017

@tacaswell wow. great catch with that.

@samsontmr
Copy link
Copy Markdown
Contributor Author

samsontmr commented Jan 12, 2017

@phobson objs doesn't exist at the top either, only obj ;)
@tacaswell will do!

@codecov-io
Copy link
Copy Markdown

Current coverage is 62.10% (diff: 100%)

Merging #7801 into master will decrease coverage by 0.01%

@@             master      #7801   diff @@
==========================================
  Files           174        174          
  Lines         56028      56052    +24   
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
+ Hits          34803      34809     +6   
- Misses        21225      21243    +18   
  Partials          0          0          

Powered by Codecov. Last update 9b1ece0...193da2e

@samsontmr
Copy link
Copy Markdown
Contributor Author

@tacaswell Tests added

@NelleV NelleV changed the title Add short-circuit return to matplotlib.artist.setp if input is length 0 [MRG+1] Add short-circuit return to matplotlib.artist.setp if input is length 0 Jan 13, 2017
Copy link
Copy Markdown
Member

@NelleV NelleV left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@NelleV
Copy link
Copy Markdown
Member

NelleV commented Jan 13, 2017

I've edited your comment to explain what it fixes. It saves us reviewers a lot of time not to have to identify what the original problem was.

@tacaswell tacaswell merged commit 9edcb03 into matplotlib:master Jan 13, 2017
@tacaswell
Copy link
Copy Markdown
Member

This should be backported after 2.0

@samsontmr Thank!

@QuLogic
Copy link
Copy Markdown
Member

QuLogic commented Jan 30, 2017

This doesn't backport that easily, or at least, I'm not familiar enough with that section of the code to do it.

dstansby pushed a commit to dstansby/matplotlib that referenced this pull request Mar 26, 2017
FIX: Add short-circuit return to matplotlib.artist.setp if input is length 0
@tacaswell
Copy link
Copy Markdown
Member

@QuLogic Lets drop the backport on this.

@QuLogic
Copy link
Copy Markdown
Member

QuLogic commented Apr 14, 2017

Works for me.

@QuLogic QuLogic modified the milestones: 2.1 (next point release), 2.0.1 (next bug fix release) Apr 14, 2017
@QuLogic QuLogic changed the title [MRG+1] Add short-circuit return to matplotlib.artist.setp if input is length 0 Add short-circuit return to matplotlib.artist.setp if input is length 0 Apr 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants

X Tutup