diff options
author | Christos Zoulas <christos@zoulas.com> | 2022-09-01 10:27:21 +0000 |
---|---|---|
committer | Christos Zoulas <christos@zoulas.com> | 2022-09-01 10:27:21 +0000 |
commit | 1770d23e069fc32cea058fe15bc9c1a8304e3aa1 (patch) | |
tree | 5b7eefcea525b238af4825d499a21790fd460a8d | |
parent | 904234054c5dd798e5f6f21c0c3cccf6071dc5b8 (diff) | |
download | file-git-1770d23e069fc32cea058fe15bc9c1a8304e3aa1.tar.gz |
fix python tests, better cleanup
-rw-r--r-- | python/magic.py | 14 | ||||
-rw-r--r-- | python/tests.py | 8 |
2 files changed, 16 insertions, 6 deletions
diff --git a/python/magic.py b/python/magic.py index 8b48f757..4300ee07 100644 --- a/python/magic.py +++ b/python/magic.py @@ -288,16 +288,26 @@ class MagicDetect(object): if self.mime_magic is None: raise error if self.mime_magic.load() == -1: + self.mime_magic.close() + self.mime_magic = None raise error self.none_magic = open(MAGIC_NONE) if self.none_magic is None: + self.mime_magic.close() + self.mime_magic = None raise error if self.none_magic.load() == -1: + self.none_magic.close() + self.none_magic = None + self.mime_magic.close() + self.mime_magic = None raise error def __del__(self): - self.mime_magic.close() - self.none_magic.close() + if self.mime_magic is not None: + self.mime_magic.close() + if self.none_magic is not None: + self.none_magic.close() threadlocal = threading.local() diff --git a/python/tests.py b/python/tests.py index 197a8fc4..3fc73c64 100644 --- a/python/tests.py +++ b/python/tests.py @@ -8,7 +8,7 @@ import magic class MagicTestCase(unittest.TestCase): filename = 'magic.py' - expected_mime_type = 'text/x-python' + expected_mime_type = 'text/x-script.python' expected_encoding = 'us-ascii' expected_name = 'Python script, ASCII text executable' @@ -22,11 +22,11 @@ class MagicTestCase(unittest.TestCase): self.assert_result(result) def test_detect_from_fobj(self): - with open(self.filename) as fobj: + with open(self.filename, "rb") as fobj: result = magic.detect_from_fobj(fobj) self.assert_result(result) def test_detect_from_content(self): - with open(self.filename) as fobj: - result = magic.detect_from_content(fobj.read(4096)) + with open(self.filename, "rb") as fobj: + result = magic.detect_from_content(fobj.read(8192)) self.assert_result(result) |