Skip to content
Permalink
Browse files Browse the repository at this point in the history
Return str rather than bytes for the description strings on python3.
Assumes utf-8 encoding from magic return values, which I hope is
always the case.
  • Loading branch information
ahupp committed Jun 5, 2016
1 parent f82dc97 commit b166698
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
12 changes: 10 additions & 2 deletions magic.py
Expand Up @@ -72,7 +72,7 @@ def from_buffer(self, buf):
"""
with self.lock:
try:
return magic_buffer(self.cookie, buf)
return maybe_decode(magic_buffer(self.cookie, buf))
except MagicException as e:
return self._handle509Bug(e)

Expand All @@ -82,7 +82,7 @@ def from_file(self, filename):
pass
with self.lock:
try:
return magic_file(self.cookie, filename)
return maybe_decode(magic_file(self.cookie, filename))
except MagicException as e:
return self._handle509Bug(e)

Expand Down Expand Up @@ -189,6 +189,14 @@ def errorcheck_negative_one(result, func, args):
return result


# return str on python3. Don't want to unconditionally

This comment has been minimized.

Copy link
@kovacsbalu

kovacsbalu Aug 2, 2016

Hi, in python3
>>> str == bytes False
but in python2.7
>>> str == bytes True
So probably you need to return s.decode('utf-8') when the relation is true.

This comment has been minimized.

Copy link
@pquentin

pquentin Aug 11, 2016

No, the behavior is correct: Python 3 will return str (Python 2 unicode) and Python 2 will return str (Python 3 bytes), which is the idiomatic thing to do in both versions of Python. The issue is that it's a breaking change in a minor version update.

# decode because that results in unicode on python2
def maybe_decode(s):
if str == bytes:
return s
else:
return s.decode('utf-8')

def coerce_filename(filename):
if filename is None:
return None
Expand Down
9 changes: 3 additions & 6 deletions test/test.py
Expand Up @@ -22,13 +22,11 @@ def assert_values(self, m, expected_values):
expected_value = (expected_value,)

for i in expected_value:
expected_value_bytes = i.encode('utf-8')

with open(filename, 'rb') as f:
buf_value = m.from_buffer(f.read())

file_value = m.from_file(filename)
if buf_value == expected_value_bytes and file_value == expected_value_bytes:
if buf_value == i and file_value == i:
break
else:
self.assertTrue(False, "no match for " + repr(expected_value))
Expand Down Expand Up @@ -86,11 +84,10 @@ def test_keep_going(self):
filename = os.path.join(self.TESTDATA_DIR, 'keep-going.jpg')

m = magic.Magic(mime=True)
self.assertEqual(m.from_file(filename),
'image/jpeg'.encode('utf-8'))
self.assertEqual(m.from_file(filename), 'image/jpeg')

m = magic.Magic(mime=True, keep_going=True)
self.assertEqual(m.from_file(filename), 'image/jpeg'.encode('utf-8'))
self.assertEqual(m.from_file(filename), 'image/jpeg')


def test_rethrow(self):
Expand Down

0 comments on commit b166698

Please sign in to comment.