summaryrefslogtreecommitdiff
path: root/magic/__init__.py
diff options
context:
space:
mode:
authorAdam Hupp <adam@hupp.org>2021-10-04 14:46:37 -0700
committerAdam Hupp <adam@hupp.org>2021-10-04 16:04:28 -0700
commit7f7542fcbc192fef6e4939f4eb748e941a720b2c (patch)
tree8a1bebe0cfa08dd0148aa5120f9768cdfcfe50a1 /magic/__init__.py
parent73bcc7482bf3e6d1f6a74a91bf9689b289a88910 (diff)
downloadpython-magic-7f7542fcbc192fef6e4939f4eb748e941a720b2c.tar.gz
Support os.PathLike types
See https://github.com/ahupp/python-magic/pull/251
Diffstat (limited to 'magic/__init__.py')
-rw-r--r--magic/__init__.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/magic/__init__.py b/magic/__init__.py
index 363e88f..bab7c7b 100644
--- a/magic/__init__.py
+++ b/magic/__init__.py
@@ -100,6 +100,7 @@ class Magic:
# if we're on python3, convert buf to bytes
# otherwise this string is passed as wchar*
# which is not what libmagic expects
+ # NEXTBREAK: only take bytes
if type(buf) == str and str != bytes:
buf = buf.encode('utf-8', errors='replace')
return maybe_decode(magic_buffer(self.cookie, buf))
@@ -229,6 +230,7 @@ def errorcheck_negative_one(result, func, args):
# return str on python3. Don't want to unconditionally
# decode because that results in unicode on python2
def maybe_decode(s):
+ # NEXTBREAK: remove
if str == bytes:
return s
else:
@@ -237,13 +239,28 @@ def maybe_decode(s):
return s.decode('utf-8', 'backslashreplace')
+try:
+ from os import PathLike
+ def unpath(filename):
+ if isinstance(filename, PathLike):
+ return filename.__fspath__()
+ else:
+ return filename
+except ImportError:
+ def unpath(filename):
+ return filename
+
def coerce_filename(filename):
if filename is None:
return None
+
+ filename = unpath(filename)
+
# ctypes will implicitly convert unicode strings to bytes with
# .encode('ascii'). If you use the filesystem encoding
# then you'll get inconsistent behavior (crashes) depending on the user's
# LANG environment variable
+ # NEXTBREAK: remove
is_unicode = (sys.version_info[0] <= 2 and
isinstance(filename, unicode)) or \
(sys.version_info[0] >= 3 and