hivesolutions / appier-extras

Appier on Steroids
http://appier-extras.hive.pt
2 stars 3 forks source link

`resize_image` `get_data()` method throwing #42

Closed 3rdvision closed 9 months ago

3rdvision commented 9 months ago

Bug description

When defining a resizable image for e.g with:

            avatar_data = utils.resize_image(
                avatar.data, etag=avatar.etag, width=width, height=height
            )

and later calling to get the output data using:

output_data = avatar_data.get_data()

it will throw the error `cannot access local variable 'resample':

UnboundLocalError

500 - cannot access local variable 'resample' where it is not associated with a value
File "/home/pc1/.local/lib/python3.11/site-packages/appier/base.py", line 1560, in application_l
result = self.handle()
File "/home/pc1/.local/lib/python3.11/site-packages/appier/base.py", line 1761, in handle
result = self.route()
File "/home/pc1/.local/lib/python3.11/site-packages/appier/base.py", line 2054, in route
return_v = method_i(*args, **kwargs)
File "/home/pc1/Documents/ripe-tech/ripe-id/src/ripe_id/controllers/web/account.py", line 171, in avatar
return account._send_avatar(
File "/home/pc1/.local/lib/python3.11/site-packages/appier_extras/parts/admin/models/account.py", line 697, in _send_avatar
return self.owner.send_file(
File "/home/pc1/.local/lib/python3.11/site-packages/appier/base.py", line 2945, in send_file
contents = contents()
File "/home/pc1/.local/lib/python3.11/site-packages/appier_extras/utils/image.py", line 63, in get_data
if resample == None:

Details

This is related to a recent change.

Although the logic implemented makes sense and initially the method defines resample=None in the python's locals, when we call the inner get_data method, the resample is no longer defined, probably due to a strange unexpected python interpreter response that cleans up the variable thinking it won't be used.

This was later verified to be true by changing the if resample == None: resample = (...) to something else to not trick python interpreter into thinking that resample will always be defined.

I think this is a very strange behavior and might be a python issue.

Incoming PR with a working fix suggestion.

Environment

Python 3.11.6

3rdvision commented 9 months ago

notify @joamag

joamag commented 9 months ago

Ok, this is fishy :) I'll try to make some unit tests just to make sure we can track the issue properly.

joamag commented 9 months ago

Ohh just noticed the code is inside a closure that's probably it.

joamag commented 9 months ago

Thanks @3rdvision this is now fixed!

3rdvision commented 9 months ago

Thanks for the quick action :+1:

Looking forward to the next release with this fix.

joamag commented 9 months ago

Done, version 0.25.1 has been released. https://github.com/hivesolutions/appier-extras/actions/runs/7758046128 https://pypi.org/project/appier-extras/#history