From b2d567ee887953a0e749a799e20d138e7732d424 Mon Sep 17 00:00:00 2001 From: Adam Hupp Date: Fri, 15 Jan 2021 13:12:53 -0800 Subject: temorarily remove dropped support for python2 --- magic/__init__.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/magic/__init__.py b/magic/__init__.py index 5fe6068..7cb5085 100644 --- a/magic/__init__.py +++ b/magic/__init__.py @@ -282,8 +282,15 @@ def maybe_decode(s): def coerce_filename(filename): if filename is None: return None - - if isinstance(filename, str): + # 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 + is_unicode = (sys.version_info[0] <= 2 and + isinstance(filename, unicode)) or \ + (sys.version_info[0] >= 3 and + isinstance(filename, str)) + if is_unicode: return filename.encode('utf-8', 'surrogateescape') else: return filename -- cgit v1.2.1