def deunicode(p):
 if isinstance(p, unicode):
  if gUTF:
   return p.encode('utf-8')
  else:
   return p.encode(codepage, 'replace')
 elif isinstance(p, basestring):
  if gUTF:
   try:
    p.decode('utf-8')
    return p
   except:
    return p.decode(codepage).encode('utf-8')
  else:
   try:
    return p.decode('utf-8').encode(codepage, 'replace')
   except:
    return p
 else:
  return p

def deunicode ( p ) : if isinstance ( p , unicode ) : if gUTF : return p . encode ( 'utf-8' ) else : return p . encode ( codepage , 'replace' ) elif isinstance ( p , basestring ) : if gUTF : try : p . decode ( 'utf-8' ) return p except : return p . decode ( codepage ) . encode ( 'utf-8' ) else : try : return p . decode ( 'utf-8' ) . encode ( codepage , 'replace' ) except : return p else : return p
return the correct 8bit ascii encoding for the platform: latin-1 for windows/posix-non-utf and utf-8 for osx/posix-utf .