1# coding: utf-8 2""" 3 safe._compat 4 5 Compatible module for Python 2 and Python 3. 6 7 :copyright: (c) 2014 by Hsiaoming Yang 8""" 9 10 11import sys 12try: 13 import cPickle as pickle 14except ImportError: 15 import pickle 16 17if sys.version_info[0] == 3: 18 unicode_type = str 19 bytes_type = bytes 20else: 21 unicode_type = unicode 22 bytes_type = str 23 24 25__all__ = ['pickle', 'to_unicode'] 26 27 28def to_unicode(value, encoding='utf-8'): 29 if isinstance(value, unicode_type): 30 return value 31 32 if isinstance(value, bytes_type): 33 return unicode_type(value, encoding=encoding) 34 35 if isinstance(value, int): 36 return unicode_type(str(value)) 37 38 return value 39