diff options
| author | Adam Hupp <adam@hupp.org> | 2017-11-20 09:21:00 -0800 |
|---|---|---|
| committer | Adam Hupp <adam@hupp.org> | 2017-11-20 09:23:19 -0800 |
| commit | bec0d4428f6de11ffd90d19dfddb5af9c72d9c63 (patch) | |
| tree | 52e177a31a1a2d5b55ecc3516ddcf55810c2c3dc /magic.py | |
| parent | 72c27fa8bfa4b8a53c09cdd42dd72f4c61d20504 (diff) | |
| download | python-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.py | 5 |
1 files changed, 5 insertions, 0 deletions
@@ -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) |
