summaryrefslogtreecommitdiff
path: root/magic.py
diff options
context:
space:
mode:
authorAdam Hupp <adam@hupp.org>2017-11-20 09:21:00 -0800
committerAdam Hupp <adam@hupp.org>2017-11-20 09:23:19 -0800
commitbec0d4428f6de11ffd90d19dfddb5af9c72d9c63 (patch)
tree52e177a31a1a2d5b55ecc3516ddcf55810c2c3dc /magic.py
parent72c27fa8bfa4b8a53c09cdd42dd72f4c61d20504 (diff)
downloadpython-magic-bec0d4428f6de11ffd90d19dfddb5af9c72d9c63.tar.gz
python3 str handling
In python3 ctypes, a str is passed to ctypes as wchar*. This means the layout of the string magic looks like [ascii, null, ascii, null, etc]. For some reason, magic handles this just fine most of the time, but it's clearly wrong and I'm amazed it worked at all.
Diffstat (limited to 'magic.py')
-rw-r--r--magic.py5
1 files changed, 5 insertions, 0 deletions
diff --git a/magic.py b/magic.py
index a97e929..8637c95 100644
--- a/magic.py
+++ b/magic.py
@@ -72,6 +72,11 @@ class Magic:
"""
with self.lock:
try:
+ # if we're on python3, convert buf to bytes
+ # otherwise this string is passed as wchar*
+ # which is not what libmagic expects
+ if type(buf) == str and str != bytes:
+ buf = buf.encode('utf-8', errors='replace')
return maybe_decode(magic_buffer(self.cookie, buf))
except MagicException as e:
return self._handle509Bug(e)