From 855dc86d1d70deab86490b6e779b091e252ad2e4 Mon Sep 17 00:00:00 2001 From: Vadim Lebedev Date: Fri, 22 Jul 2022 22:50:01 +0200 Subject: [PATCH] is_archive: Avoid closing a file not opened by is_archive --- libarchive/__init__.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libarchive/__init__.py b/libarchive/__init__.py index c546db9..ec8c2d8 100644 --- a/libarchive/__init__.py +++ b/libarchive/__init__.py @@ -158,8 +158,10 @@ def is_archive(f, formats=(None, ), filters=(None, )): This function will return True if the file can be opened as an archive using the given format(s)/filter(s).''' + need_close : bool = False if isinstance(f, str): f = open(f, 'r') + need_close = True a = _libarchive.archive_read_new() for format in formats: format = get_func(format, FORMATS, 0) @@ -180,7 +182,8 @@ def is_archive(f, formats=(None, ), filters=(None, )): finally: _libarchive.archive_read_close(a) _libarchive.archive_read_free(a) - f.close() + if need_close: + f.close() class EntryReadStream(object):