Allow Python buffer objects through util.convert_bytes()

This commit is contained in:
Thomas Perl 2016-11-20 11:50:05 +01:00
parent 87bca5f82a
commit 746b7590bf
1 changed files with 5 additions and 1 deletions

View File

@ -1435,10 +1435,14 @@ def convert_bytes(d):
u'Hello'
>>> convert_bytes(u'Hey')
u'Hey'
>>> type(convert_bytes(buffer('hoho')))
<type 'buffer'>
"""
if d is None:
return d
if any(isinstance(d, t) for t in (int, long, bool, float)):
if isinstance(d, buffer):
return d
elif any(isinstance(d, t) for t in (int, long, bool, float)):
return d
elif not isinstance(d, unicode):
return d.decode('utf-8', 'ignore')