@ -19,7 +19,7 @@
from __future__ import ( absolute_import , division , print_function )
from __future__ import ( absolute_import , division , print_function )
__metaclass__ = type
__metaclass__ = type
from six import string_types , text_type , binary_type
from six import string_types , text_type , binary_type , PY3
# to_bytes and to_unicode were written by Toshio Kuratomi for the
# to_bytes and to_unicode were written by Toshio Kuratomi for the
# python-kitchen library https://pypi.python.org/pypi/kitchen
# python-kitchen library https://pypi.python.org/pypi/kitchen
@ -37,6 +37,9 @@ _LATIN1_ALIASES = frozenset(('latin-1', 'LATIN-1', 'latin1', 'LATIN1',
# EXCEPTION_CONVERTERS is defined below due to using to_unicode
# EXCEPTION_CONVERTERS is defined below due to using to_unicode
if PY3 :
basestring = ( str , bytes )
def to_unicode ( obj , encoding = ' utf-8 ' , errors = ' replace ' , nonstring = None ) :
def to_unicode ( obj , encoding = ' utf-8 ' , errors = ' replace ' , nonstring = None ) :
''' Convert an object into a :class:`unicode` string
''' Convert an object into a :class:`unicode` string
@ -90,7 +93,7 @@ def to_unicode(obj, encoding='utf-8', errors='replace', nonstring=None):
'''
'''
# Could use isbasestring/isunicode here but we want this code to be as
# Could use isbasestring/isunicode here but we want this code to be as
# fast as possible
# fast as possible
if isinstance ( obj , ( string_types, text_type ) ) :
if isinstance ( obj , base string) :
if isinstance ( obj , text_type ) :
if isinstance ( obj , text_type ) :
return obj
return obj
if encoding in _UTF8_ALIASES :
if encoding in _UTF8_ALIASES :
@ -112,7 +115,7 @@ def to_unicode(obj, encoding='utf-8', errors='replace', nonstring=None):
simple = None
simple = None
if not simple :
if not simple :
try :
try :
simple = str ( obj )
simple = text_type ( obj )
except UnicodeError :
except UnicodeError :
try :
try :
simple = obj . __str__ ( )
simple = obj . __str__ ( )
@ -123,7 +126,7 @@ def to_unicode(obj, encoding='utf-8', errors='replace', nonstring=None):
return simple
return simple
elif nonstring in ( ' repr ' , ' strict ' ) :
elif nonstring in ( ' repr ' , ' strict ' ) :
obj_repr = repr ( obj )
obj_repr = repr ( obj )
if isinstance ( obj_repr , str ) :
if isinstance ( obj_repr , binary_type ) :
obj_repr = text_type ( obj_repr , encoding , errors )
obj_repr = text_type ( obj_repr , encoding , errors )
if nonstring == ' repr ' :
if nonstring == ' repr ' :
return obj_repr
return obj_repr
@ -199,15 +202,15 @@ def to_bytes(obj, encoding='utf-8', errors='replace', nonstring=None):
'''
'''
# Could use isbasestring, isbytestring here but we want this to be as fast
# Could use isbasestring, isbytestring here but we want this to be as fast
# as possible
# as possible
if isinstance ( obj , ( string_types, text_type ) ) :
if isinstance ( obj , base string) :
if isinstance ( obj , str ) :
if isinstance ( obj , binary_type ) :
return obj
return obj
return obj . encode ( encoding , errors )
return obj . encode ( encoding , errors )
if not nonstring :
if not nonstring :
nonstring = ' simplerepr '
nonstring = ' simplerepr '
if nonstring == ' empty ' :
if nonstring == ' empty ' :
return ' '
return b ' '
elif nonstring == ' passthru ' :
elif nonstring == ' passthru ' :
return obj
return obj
elif nonstring == ' simplerepr ' :
elif nonstring == ' simplerepr ' :
@ -222,7 +225,7 @@ def to_bytes(obj, encoding='utf-8', errors='replace', nonstring=None):
try :
try :
simple = obj . __unicode__ ( )
simple = obj . __unicode__ ( )
except ( AttributeError , UnicodeError ) :
except ( AttributeError , UnicodeError ) :
simple = ' '
simple = b ' '
if isinstance ( simple , text_type ) :
if isinstance ( simple , text_type ) :
simple = simple . encode ( encoding , ' replace ' )
simple = simple . encode ( encoding , ' replace ' )
return simple
return simple
@ -230,7 +233,7 @@ def to_bytes(obj, encoding='utf-8', errors='replace', nonstring=None):
try :
try :
obj_repr = obj . __repr__ ( )
obj_repr = obj . __repr__ ( )
except ( AttributeError , UnicodeError ) :
except ( AttributeError , UnicodeError ) :
obj_repr = ' '
obj_repr = b ' '
if isinstance ( obj_repr , text_type ) :
if isinstance ( obj_repr , text_type ) :
obj_repr = obj_repr . encode ( encoding , errors )
obj_repr = obj_repr . encode ( encoding , errors )
else :
else :