[Git][reproducible-builds/diffoscope][master] Don't rely on magic.Magic(...) to have an identical API between file's...

Chris Lamb gitlab at salsa.debian.org
Fri Feb 12 12:56:36 UTC 2021



Chris Lamb pushed to branch master at Reproducible Builds / diffoscope


Commits:
74c0d3f4 by Chris Lamb at 2021-02-12T12:56:24+00:00
Don't rely on magic.Magic(...) to have an identical API between file's magic.py and PyPI's python-magic library. (Closes: reproducible-builds/diffoscope#238)

- - - - -


1 changed file:

- diffoscope/comparators/utils/file.py


Changes:

=====================================
diffoscope/comparators/utils/file.py
=====================================
@@ -65,21 +65,17 @@ def _run_tests(fold, tests):
 
 
 class File(metaclass=abc.ABCMeta):
-    if hasattr(magic, "Magic"):  # use python-magic
-
-        @classmethod
-        def guess_file_type(cls, path):
-            if not hasattr(cls, "_mimedb"):
-                cls._mimedb = magic.Magic()
-            return maybe_decode(cls._mimedb.from_file(path))
-
-        @classmethod
-        def guess_encoding(cls, path):
-            if not hasattr(cls, "_mimedb_encoding"):
-                cls._mimedb_encoding = magic.Magic(mime_encoding=True)
-            return maybe_decode(cls._mimedb_encoding.from_file(path))
+    """
+    There are two Python modules named 'magic'. One of these ships with the
+    file upstream under the python/ directory and PyPI's `python-magic` and
+    they have slightly different interfaces despite attempts at a compatibility
+    layer.
+    """
 
-    else:  # use Magic-file-extensions from file
+    # Try and detect the instance of the libmagic shared library (loaded via
+    # ctypes) used by the magic.py shipped with file.
+    #
+    if hasattr(magic, "_libraries"):
 
         @classmethod
         def guess_file_type(cls, path):
@@ -97,6 +93,21 @@ class File(metaclass=abc.ABCMeta):
                 cls._mimedb_encoding.load()
             return cls._mimedb_encoding.file(path)
 
+    else:
+        # Assume we are using python-magic
+
+        @classmethod
+        def guess_file_type(cls, path):
+            if not hasattr(cls, "_mimedb"):
+                cls._mimedb = magic.Magic()
+            return maybe_decode(cls._mimedb.from_file(path))
+
+        @classmethod
+        def guess_encoding(cls, path):
+            if not hasattr(cls, "_mimedb_encoding"):
+                cls._mimedb_encoding = magic.Magic(mime_encoding=True)
+            return maybe_decode(cls._mimedb_encoding.from_file(path))
+
     def __init__(self, container=None):
         self._comments = []
         self._container = container



View it on GitLab: https://salsa.debian.org/reproducible-builds/diffoscope/-/commit/74c0d3f40efb49cfd78d3592bc87c76211e20570

-- 
View it on GitLab: https://salsa.debian.org/reproducible-builds/diffoscope/-/commit/74c0d3f40efb49cfd78d3592bc87c76211e20570
You're receiving this email because of your account on salsa.debian.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.reproducible-builds.org/pipermail/rb-commits/attachments/20210212/9c115a05/attachment.htm>


More information about the rb-commits mailing list