def recode(self, text): encoding = self.encodings.get(text) if encoding is None: return text if sys.version_info[0] >= 3: # sys.stdin is set up for surrogates in GPGKeys.input() return decode(encode(text, encoding), errors='surrogateescape') else: return encode(decode(text), encoding)
def gpgdecode(text): """Decode a GnuPG string. Returns decoded string and source encoding. """ try: encoding = 'utf-8' text = decode(text, encoding, errors='strict') except UnicodeDecodeError: try: encoding = 'latin-1' text = decode(text, encoding, errors='strict') except UnicodeDecodeError: encoding = getpreferredencoding() text = decode(text, encoding) return text, encoding