jwilk / didjvu

DjVu encoder with foreground/background separation
https://jwilk.net/software/didjvu
GNU General Public License v2.0
10 stars 8 forks source link

gamera_support.load_image(): unclosed file #23

Closed FriedrichFroebel closed 2 years ago

FriedrichFroebel commented 2 years ago

When doing some tests refactoring on my Python 3 fork of this project, I stumbled upon some Python warning regarding unclosed files which appeared to not be referenced any more:

/home/me/github/jwilk/didjvu/tests/test_gamera.py:60: ResourceWarning: unclosed file <_io.BufferedReader name='/home/me/github/jwilk/didjvu/tests/data/greyscale-packbits.tiff'>
  in_image = gamera_support.load_image(path)
Object allocated at (most recent call last):
  File /home/me/github/jwilk/didjvu/tests/test_gamera.py", lineno 79
    self._test_methods('greyscale-packbits.tiff')
  File "/home/me/github/jwilk/didjvu/tests/test_gamera.py", lineno 73
    self._test_one_method(filename=filename, method=method, kwargs=kwargs)
  File "/home/me/github/jwilk/didjvu/tests/test_gamera.py", lineno 60
    in_image = gamera_support.load_image(path)
  File "/home/me/github/jwilk/didjvu/lib/gamera_support.py", lineno 65
    pil_image = PILImage.open(filename)
  File "/home/me/github/jwilk/env/lib/python3.8/site-packages/PIL/Image.py", lineno 2968
    fp = builtins.open(filename, "rb")

The issue seems to be that the corresponding method loads the image using PIL at the beginning (pil_image = PIL.open(filename)), but does not seem to be required any more after having passed it into the corresponding Gamera loader methods. Just calling pil_image.close() at the end of the moment seems to fix this - at least all existing tests still pass.

Please note that while this appeared in the Python 3 version only and I am not able to tell you which configuration change suddenly brought this warning up, I had a look at the offending code as well and assume that correctly closing the file there should make sense for the Python 2 case as well.

jwilk commented 2 years ago

while this appeared in the Python 3 version only and I am not able to tell you which configuration change suddenly brought this warning up

These warnings were added in Python 3.2: https://bugs.python.org/issue10093

FriedrichFroebel commented 2 years ago

While these warnings might have been added in Python 3.2, I have used the same Python virtualenv for the whole refactoring. It seems like nose has been hiding them, while the stdlib unittest test runner reports them by default. Nevertheless, this specific aspect should be (at least partially) independent from the actual issue and just becomes relevant when trying to reproduce the actual warning message.

jwilk commented 2 years ago

These warnings are not shown by default. But unittest enables them since Python 3.2: https://bugs.python.org/issue10535

FriedrichFroebel commented 2 years ago

Thanks for the explanations - now it makes sense for me why these warnings have appeared.

jwilk commented 2 years ago

Fixed in 6d9bd4a9cbf882d88d5a76bd4d6217c22f72f169.