Skip to content

UnboundLocalError: local variable 'err_code' referenced before assignment #2071

Closed

Description

@shayanelhami

On this line
ImageFile.py Line 238

if not self.map and not LOAD_TRUNCATED_IMAGES and err_code < 0:

It happens when there is a ValueError exception on line 194:

    try:
        decoder.setimage(self.im, extents)
    except ValueError:
        continue

which leave no opportunity for err_code to be set.

What did you do?

Say I have a 128x128 png with one zip tile:
128x128

and I use this code:

from PIL import Image
im = Image.open("128x128.png")
im.size = (32, 32)
im.save("32x32.png")

What did you expect to happen?

Instead of an UnboundLocalError it should at least raise a meaningful error.

What versions of Pillow and Python are you using?

Python 2.7.12, Pillow commit 533f414

Activity

  1. homm commented on Aug 15, 2016

    @homm
    Member

    We should make read-only property and raise on this:

    im.size = (32, 32)
  2. wiredfool commented on Aug 17, 2016

    @wiredfool
    Member

    Agreed.

  3. added
    Bug
    on Aug 17, 2016
  4. homm commented on Aug 17, 2016

    @homm
    Member

    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.

  5. radarhere commented on Sep 3, 2016

    @radarhere
    Member

    @homm what do you mean by 'only allowed sizes'?

    I can think of two ways to implement this.

    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.

    Neither is a particularly small change.

  6. PeterJCLaw commented on Sep 5, 2016

    @PeterJCLaw

    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.

    I'm using Pillow 3.3.0 from PyPI.

  7. alexwlchan commented on Oct 18, 2016

    @alexwlchan
    Contributor

    @radarhere Rather than changing all the internal setters to use size, could you use @size.setter and implement the custom logic there? Something like:

    @property
    def size(self):
        return self._size
    
    @size.setter
    def size(self, value)
        # validation logic goes here...
        self._size = value

    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.

  8. radarhere commented on Oct 19, 2016

    @radarhere
    Member

    @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.

  9. radarhere commented on Sep 7, 2018

    @radarhere
    Member

    I have created PR #3203 as a suggestion for this.

  10. hugovk commented on Sep 29, 2018

    @hugovk
    Member

    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?

  11. radarhere commented on Sep 30, 2018

    @radarhere
    Member

    I've just found #3044 (comment), where setting the size directly on a TIFF image is given as a solution.

  12. radarhere commented on Sep 30, 2018

    @radarhere
    Member

    Resolved by #3203

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugPNG

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions