Skip to content

Commit

Permalink
Move magic.mgc injection into Magic class
Browse files Browse the repository at this point in the history
  • Loading branch information
ddelange committed Apr 11, 2024
1 parent 359e007 commit bb9c685
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions magic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))


Expand Down

0 comments on commit bb9c685

Please sign in to comment.