summaryrefslogtreecommitdiff
path: root/magic.py
diff options
context:
space:
mode:
authorAdam Hupp <adam@hupp.org>2014-01-04 12:48:39 -0800
committerAdam Hupp <adam@hupp.org>2014-01-04 13:01:41 -0800
commita5ebf0d558c3a851c0884d30a0051836f7db0c2a (patch)
treeeeb8c036fd53d1430ab90a6547f0a78856ac9bfd /magic.py
parenta0f2249dad53b0c6e7997560d8d333830c578b96 (diff)
downloadpython-magic-a5ebf0d558c3a851c0884d30a0051836f7db0c2a.tar.gz
Properly handle unicode filenames given in both byte-str and unicode
values, across both python 3 and 2.
Diffstat (limited to 'magic.py')
-rw-r--r--magic.py13
1 files changed, 12 insertions, 1 deletions
diff --git a/magic.py b/magic.py
index 5025842..e7336c3 100644
--- a/magic.py
+++ b/magic.py
@@ -192,7 +192,18 @@ def errorcheck_negative_one(result, func, args):
def coerce_filename(filename):
if filename is None:
return None
- return filename.encode(sys.getfilesystemencoding())
+
+ # ctypes will implicitly convert unicode strings to bytes with
+ # .encode('ascii'). A more useful default here is
+ # getfilesystemencoding(). We need to leave byte-str unchanged.
+ is_unicode = (sys.version_info.major <= 2 and
+ isinstance(filename, unicode)) or \
+ (sys.version_info.major >= 3 and
+ isinstance(filename, str))
+ if is_unicode:
+ return filename.encode(sys.getfilesystemencoding())
+ else:
+ return filename
magic_open = libmagic.magic_open
magic_open.restype = magic_t