From bb9c685075139ea89c9b241c465c64bf1cffee3d Mon Sep 17 00:00:00 2001 From: ddelange <14880945+ddelange@users.noreply.github.com> Date: Thu, 11 Apr 2024 09:55:40 +0200 Subject: [PATCH] Move magic.mgc injection into Magic class --- magic/__init__.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/magic/__init__.py b/magic/__init__.py index 5d7a47d..31f54af 100644 --- a/magic/__init__.py +++ b/magic/__init__.py @@ -96,6 +96,12 @@ def __init__(self, mime=False, magic_file=None, mime_encoding=False, self.cookie = magic_open(self.flags) self.lock = threading.Lock() + if magic_file is None and not os.environ.get("MAGIC"): + # wheels package the mime database in this directory + # prefer it when no magic file is specified by the user + mime_db = os.path.join(os.path.dirname(__file__), 'magic.mgc') + if os.path.exists(mime_db): + magic_file = mime_db magic_load(self.cookie, magic_file) # MAGIC_EXTENSION was added in 523 or 524, so bail if @@ -354,10 +360,6 @@ def magic_descriptor(cookie, fd): def magic_load(cookie, filename): - mime_db = os.path.join(os.path.dirname(__file__), 'magic.mgc') - if os.path.exists(mime_db): - # wheels package the mime database in this directory - return _magic_load(cookie, coerce_filename(mime_db)) return _magic_load(cookie, coerce_filename(filename))