diff options
Diffstat (limited to 'magic/__init__.py')
-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 |