willtrnr/pyxlsb

Python reports unclosed files

osvenskan opened this issue · 1 comments

While running our project with -W default per the recommendation of Python 3.9, we saw a few warnings about unclosed files which we traced to pyxlsb. Here's a simple demo using 2010_InsPubActs.xlsb which is an XLSB that I found online but I think any XLSB would work.

$ python 
Python 3.8.8 (v3.8.8:024d8058b0, Feb 19 2021, 08:48:17)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyxlsb
>>> pyxlsb.__version__
'1.0.8'
>>> workbook = pyxlsb.open_workbook('2010_InsPubActs.xlsb')
>>> [workbook.get_sheet(sheet_name) for sheet_name in workbook.sheets]
[<pyxlsb.worksheet.Worksheet object at 0x7fd53802be80>]
>>> workbook.close()
>>> ^D
sys:1: ResourceWarning: unclosed file <_io.BufferedRandom name=6>
ResourceWarning: Enable tracemalloc to get the object allocation traceback

I noticed that there's one or two TemporaryFile instances being passed to the call to Worksheet on line 84; maybe they're not getting closed?

Thanks for pyxlsb!

The worksheet must also be closed as well. The workbook object doesn't "reap" the worksheets instances on closure.

The usage exemple uses the with construct on the retrieved sheet for that.

So, this is in some way behaving as intended, but I'll keep this open since the "documentation" could be more clear about this.