File handle leaks on unsuccessful save
When Workbook.save()
fails for whatever reason, the file handle of the archive is not closed.
Here is a contrived example: repro.py. I use portalocker
to simulate some external process which has an exclusive lock on the file I'm trying to write to (e.g. the file is open in Microsoft Excel).
The standard way of preventing this is to use context managers. Here is my proposed way to fix it (docstrings omitted for brevity):
- The
save_workbook
function inexcel.py
becomes:def save_workbook(workbook, filename): with ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True) as archive: ExcelWriter(workbook, archive).save()
- The
save
method ofExcelWriter
becomes:def save(self): """Write data into the archive.""" self.write_data()