def unicode_from_os(e):
 if (sys.version_info >= (3,)):
  return str(e)
 try:
  if isinstance(e, Exception):
   e = e.args[0]
  if isinstance(e, str_cls):
   return e
  if isinstance(e, int):
   e = str(e)
  return str_cls(e, _encoding)
 except UnicodeDecodeError:
  for encoding in _fallback_encodings:
   try:
    return str_cls(e, encoding, errors='strict')
   except:
    pass
 return str_cls(e, errors='replace')

def unicode_from_os ( e ) : if ( sys . version_info >= ( 3 , ) ) : return str ( e ) try : if isinstance ( e , Exception ) : e = e . args [ 0 ] if isinstance ( e , str_cls ) : return e if isinstance ( e , int ) : e = str ( e ) return str_cls ( e , _encoding ) except UnicodeDecodeError : for encoding in _fallback_encodings : try : return str_cls ( e , encoding , errors = 'strict' ) except : pass return str_cls ( e , errors = 'replace' )
this is needed as some exceptions coming from the os are already encoded and so just calling unicode(e) will result in an unicodedecodeerror as the string isnt in ascii form .