diff options
author | Adam Hupp <adam@hupp.org> | 2021-01-15 13:12:53 -0800 |
---|---|---|
committer | Adam Hupp <adam@hupp.org> | 2021-01-15 13:12:53 -0800 |
commit | b2d567ee887953a0e749a799e20d138e7732d424 (patch) | |
tree | 9ac371e907ed4a57392ee7e397673c8bdbb3ad76 | |
parent | 442d70dcee602ec76e1108721818fe46fc97188e (diff) | |
download | python-magic-b2d567ee887953a0e749a799e20d138e7732d424.tar.gz |
temorarily remove dropped support for python2
-rw-r--r-- | magic/__init__.py | 11 |
1 files 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 |