@ -19,6 +19,8 @@
from __future__ import ( absolute_import , division , print_function )
__metaclass__ = type
from six import string_types , text_type , binary_type
# to_bytes and to_unicode were written by Toshio Kuratomi for the
# python-kitchen library https://pypi.python.org/pypi/kitchen
# They are licensed in kitchen under the terms of the GPLv2+
@ -88,13 +90,13 @@ def to_unicode(obj, encoding='utf-8', errors='replace', nonstring=None):
'''
# Could use isbasestring/isunicode here but we want this code to be as
# fast as possible
if isinstance ( obj , base string) :
if isinstance ( obj , unicod e) :
if isinstance ( obj , ( string_types, text_type ) ) :
if isinstance ( obj , text_typ e) :
return obj
if encoding in _UTF8_ALIASES :
return unicod e( obj , ' utf-8 ' , errors )
return text_typ e( obj , ' utf-8 ' , errors )
if encoding in _LATIN1_ALIASES :
return unicod e( obj , ' latin-1 ' , errors )
return text_typ e( obj , ' latin-1 ' , errors )
return obj . decode ( encoding , errors )
if not nonstring :
@ -116,13 +118,13 @@ def to_unicode(obj, encoding='utf-8', errors='replace', nonstring=None):
simple = obj . __str__ ( )
except ( UnicodeError , AttributeError ) :
simple = u ' '
if isinstance ( simple , str ) :
return unicod e( simple , encoding , errors )
if isinstance ( simple , binary_type ) :
return text_typ e( simple , encoding , errors )
return simple
elif nonstring in ( ' repr ' , ' strict ' ) :
obj_repr = repr ( obj )
if isinstance ( obj_repr , str ) :
obj_repr = unicod e( obj_repr , encoding , errors )
obj_repr = text_typ e( obj_repr , encoding , errors )
if nonstring == ' repr ' :
return obj_repr
raise TypeError ( ' to_unicode was given " %(obj)s " which is neither '
@ -197,7 +199,7 @@ def to_bytes(obj, encoding='utf-8', errors='replace', nonstring=None):
'''
# Could use isbasestring, isbytestring here but we want this to be as fast
# as possible
if isinstance ( obj , base string) :
if isinstance ( obj , ( string_types, text_type ) ) :
if isinstance ( obj , str ) :
return obj
return obj . encode ( encoding , errors )
@ -210,7 +212,7 @@ def to_bytes(obj, encoding='utf-8', errors='replace', nonstring=None):
return obj
elif nonstring == ' simplerepr ' :
try :
simple = str ( obj )
simple = binary_type ( obj )
except UnicodeError :
try :
simple = obj . __str__ ( )
@ -221,7 +223,7 @@ def to_bytes(obj, encoding='utf-8', errors='replace', nonstring=None):
simple = obj . __unicode__ ( )
except ( AttributeError , UnicodeError ) :
simple = ' '
if isinstance ( simple , unicod e) :
if isinstance ( simple , text_typ e) :
simple = simple . encode ( encoding , ' replace ' )
return simple
elif nonstring in ( ' repr ' , ' strict ' ) :
@ -229,10 +231,10 @@ def to_bytes(obj, encoding='utf-8', errors='replace', nonstring=None):
obj_repr = obj . __repr__ ( )
except ( AttributeError , UnicodeError ) :
obj_repr = ' '
if isinstance ( obj_repr , unicod e) :
if isinstance ( obj_repr , text_typ e) :
obj_repr = obj_repr . encode ( encoding , errors )
else :
obj_repr = str ( obj_repr )
obj_repr = binary_type ( obj_repr )
if nonstring == ' repr ' :
return obj_repr
raise TypeError ( ' to_bytes was given " %(obj)s " which is neither '