Ensure get_container doesnt leak a tdir on error

This commit is contained in:
Kovid Goyal 2022-07-19 12:29:50 +05:30
parent 202f537913
commit a5516efe2b
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -1551,9 +1551,18 @@ def get_container(path, log=None, tdir=None, tweak_mode=False):
isdir = os.path.isdir(path) isdir = os.path.isdir(path)
except Exception: except Exception:
isdir = False isdir = False
ebook = (AZW3Container if path.rpartition('.')[-1].lower() in {'azw3', 'mobi', 'original_azw3', 'original_mobi'} and not isdir own_tdir = not tdir
else EpubContainer)(path, log, tdir=tdir) ebook_cls = (AZW3Container if path.rpartition('.')[-1].lower() in {'azw3', 'mobi', 'original_azw3', 'original_mobi'} and not isdir
ebook.tweak_mode = tweak_mode else EpubContainer)
if own_tdir:
tdir = PersistentTemporaryDirectory(f'_{ebook_cls.book_type}_container')
try:
ebook = ebook_cls(path, log, tdir=tdir)
ebook.tweak_mode = tweak_mode
except BaseException:
if own_tdir:
shutil.rmtree(tdir, ignore_errors=True)
raise
return ebook return ebook