]> granicus.if.org Git - file/commitdiff
PR/511: Orestis: Restore python-2 compatibility.
authorChristos Zoulas <christos@zoulas.com>
Tue, 2 Feb 2016 19:05:12 +0000 (19:05 +0000)
committerChristos Zoulas <christos@zoulas.com>
Tue, 2 Feb 2016 19:05:12 +0000 (19:05 +0000)
python/magic.py

index 8cff29a9c4aff6312c3e1dbd7322b3ade1da8a78..c48f7d5a3012e1c0a0ac982f9b62a72b7ae62320 100644 (file)
@@ -126,12 +126,15 @@ class Magic(object):
         if isinstance(filename, bytes):
             bi = filename
         else:
-            bi = bytes(filename, 'utf-8')
+            try:  # keep Python 2 compatibility
+                bi = bytes(filename, 'utf-8')
+            except TypeError:
+                bi = bytes(filename)
         r = _file(self._magic_t, bi)
         if isinstance(r, str):
             return r
         else:
-            return str(r'utf-8')
+            return str(r).encode('utf-8')
 
     def descriptor(self, fd):
         """
@@ -149,7 +152,7 @@ class Magic(object):
         if isinstance(r, str):
             return r
         else:
-            return str(r'utf-8')
+            return str(r).encode('utf-8')
 
     def error(self):
         """
@@ -160,7 +163,7 @@ class Magic(object):
         if isinstance(e, str):
             return e
         else:
-            return str(e'utf-8')
+            return str(e).encode('utf-8')
 
     def setflags(self, flags):
         """