You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
UnboundLocalError: local variable 'err_code' referenced before assignment #2071
1 - Using a @property decorator, and changing all of the internal setters to _size to avoid raising the error.
2 - Using a __set_attr__ hook, and setting a _loaded boolean at the end of each load method, so that an error is raised when attempting to set size after it is true.
Not sure if it's the same issue, but I've seen a similar symptom when trying to load a (corrupted) GIF file. The GIF in question causes a ValueError at the same location and then also the UnboundLocalError.
My scenario didn't involve any assignment of the size of the image, just doing Image.open("bad.gif").load() was enough.
@alexwlchan the problem is that the goal here is not to add validation logic, but to raise an error based on any external setting of the property. So then, in order to not raise an error wherever size is set internally, _size would need to be set directly by all internal Pillow code. That's perfectly possible, it's just not a small change.
Activity
homm commentedon Aug 15, 2016
We should make read-only property and raise on this:
wiredfool commentedon Aug 17, 2016
Agreed.
homm commentedon Aug 17, 2016
Actually, the size assigning is used at least for icns files.
We'll need to create some smart property which will accept only allowed sizes and only before loading.
radarhere commentedon Sep 3, 2016
@homm what do you mean by 'only allowed sizes'?
I can think of two ways to implement this.
1 - Using a
@propertydecorator, and changing all of the internal setters to_sizeto avoid raising the error.2 - Using a
__set_attr__hook, and setting a_loadedboolean at the end of each load method, so that an error is raised when attempting to setsizeafter it is true.Neither is a particularly small change.
PeterJCLaw commentedon Sep 5, 2016
Not sure if it's the same issue, but I've seen a similar symptom when trying to load a (corrupted) GIF file. The GIF in question causes a
ValueErrorat the same location and then also theUnboundLocalError.My scenario didn't involve any assignment of the size of the image, just doing
Image.open("bad.gif").load()was enough.I'm using Pillow 3.3.0 from PyPI.
alexwlchan commentedon Oct 18, 2016
@radarhere Rather than changing all the internal setters to use
size, could you use@size.setterand implement the custom logic there? Something like:Then all the existing assignments continue to work without modifications, but we get the nice validation logic.
I’d offer a patch, but I confess that it’s not completely obvious to me what the conditions are that should trip those validation checks.
radarhere commentedon Oct 19, 2016
@alexwlchan the problem is that the goal here is not to add validation logic, but to raise an error based on any external setting of the property. So then, in order to not raise an error wherever
sizeis set internally,_sizewould need to be set directly by all internal Pillow code. That's perfectly possible, it's just not a small change.radarhere commentedon Sep 7, 2018
I have created PR #3203 as a suggestion for this.
hugovk commentedon Sep 29, 2018
Should it give a deprecation warning to begin with, before raising in a later release? Or in this case is it okay to go straight to the raise?
radarhere commentedon Sep 30, 2018
I've just found #3044 (comment), where setting the size directly on a TIFF image is given as a solution.
radarhere commentedon Sep 30, 2018
Resolved by #3203