from robot.api import logger from robot.utils import asserts from robot.libraries.BuiltIn import BuiltIn # add hexlify to codecs def hexlify_decode_plus(data, errors='strict'): udata, length = hexlify_codec.hex_decode(data, errors) return (udata.rstrip(), length) hexlify_codec_plus = codecs.CodecInfo( name='hexlify', encode=hexlify_codec.hex_encode, decode=hexlify_decode_plus, incrementalencoder=hexlify_codec.IncrementalEncoder, incrementaldecoder=hexlify_codec.IncrementalDecoder, streamwriter=hexlify_codec.StreamWriter, streamreader=hexlify_codec.StreamReader) codecs.register(lambda c: hexlify_codec_plus if c == 'hexlify' else None) class SerialConsoleError(RuntimeError): ROBOT_SUPPRESS_NAME = True ROBOT_EXIT_ON_FAILURE = True class SerialConsole: """A test library for Linux serial console manipulation and validation. """
def getregentry(***REMOVED***: return codecs.CodecInfo( name='cp1252', encode=Codec(***REMOVED***.encode,
while True: if size <= len(self.buffer): d = self.buffer[:size] self.buffer = self.buffer[size:] self.offset += size return d if final: d = self.buffer self.buffer = '' self.offset += len(d) return d c = self.stream.read(8196) final = len(c) < 8196 or len(c) self.buffer += self.decoder.decode(c, final) def tell(self): return self.offset _codecinfo = codecs.CodecInfo( name='zlib_raw', encode=zlib_raw_encode, decode=zlib_raw_decode, incrementalencoder=IncrementalEncoder, incrementaldecoder=IncrementalDecoder, streamreader=StreamReader, streamwriter=StreamWriter, )
fstring_decode = decode SUPPORTS_FSTRINGS = _natively_supports_fstrings() if SUPPORTS_FSTRINGS: # pragma: no cover decode = utf_8.decode # noqa IncrementalDecoder = utf_8.incrementaldecoder # noqa StreamReader = utf_8.streamreader # noqa # codec api codec_map = { name: codecs.CodecInfo( name=name, encode=utf_8.encode, decode=decode, incrementalencoder=utf_8.incrementalencoder, incrementaldecoder=IncrementalDecoder, streamreader=StreamReader, streamwriter=utf_8.streamwriter, ) for name in ('future-fstrings', 'future_fstrings') } def register(): # pragma: no cover codecs.register(codec_map.get) def main(argv=None): parser = argparse.ArgumentParser(description='Prints transformed source.') parser.add_argument('filename')
def getregentry(): return codecs.CodecInfo(name='utf-32-be', encode=encode, decode=decode, incrementalencoder=IncrementalEncoder, incrementaldecoder=IncrementalDecoder, streamreader=StreamReader, streamwriter=StreamWriter)
def make_sloppy_codec(encoding): """ Take a codec name, and return a 'sloppy' version of that codec that can encode and decode the unassigned bytes in that encoding. Single-byte encodings in the standard library are defined using some boilerplate classes surrounding the functions that do the actual work, `codecs.charmap_decode` and `charmap_encode`. This function, given an encoding name, *defines* those boilerplate classes. """ # Make an array of all 256 possible bytes. all_bytes = bytearray(range(256)) # Get a list of what they would decode to in Latin-1. sloppy_chars = list(all_bytes.decode('latin-1')) # Get a list of what they decode to in the given encoding. Use the # replacement character for unassigned bytes. if PY26: decoded_chars = all_bytes.decode(encoding, 'replace') else: decoded_chars = all_bytes.decode(encoding, errors='replace') # Update the sloppy_chars list. Each byte that was successfully decoded # gets its decoded value in the list. The unassigned bytes are left as # they are, which gives their decoding in Latin-1. for i, char in enumerate(decoded_chars): if char != REPLACEMENT_CHAR: sloppy_chars[i] = char # Create the data structures that tell the charmap methods how to encode # and decode in this sloppy encoding. decoding_table = ''.join(sloppy_chars) encoding_table = codecs.charmap_build(decoding_table) # Now produce all the class boilerplate. Look at the Python source for # `encodings.cp1252` for comparison; this is almost exactly the same, # except I made it follow pep8. class Codec(codecs.Codec): def encode(self, input, errors='strict'): return codecs.charmap_encode(input, errors, encoding_table) def decode(self, input, errors='strict'): return codecs.charmap_decode(input, errors, decoding_table) class IncrementalEncoder(codecs.IncrementalEncoder): def encode(self, input, final=False): return codecs.charmap_encode(input, self.errors, encoding_table)[0] class IncrementalDecoder(codecs.IncrementalDecoder): def decode(self, input, final=False): return codecs.charmap_decode(input, self.errors, decoding_table)[0] class StreamWriter(Codec, codecs.StreamWriter): pass class StreamReader(Codec, codecs.StreamReader): pass return codecs.CodecInfo( name='sloppy-' + encoding, encode=Codec().encode, decode=Codec().decode, incrementalencoder=IncrementalEncoder, incrementaldecoder=IncrementalDecoder, streamreader=StreamReader, streamwriter=StreamWriter, )
def _iconv_factory(codec_name): codec_name = codec_name.lower() if codec_name in codecs_dict: def iconvencode(input, errors='strict', encoding=codec_name): extra = [] if errors == 'ignore': extra.append('-c') elif errors != 'strict': raise NotImplementedError("%r error handling not implemented" " for codec %r" % (errors, encoding)) _input = input.encode('utf-8') iconv = _run_iconv('utf-8', encoding, extra) output, error = iconv.communicate(_input) if error: error = error.splitlines()[0] raise UnicodeEncodeError(encoding, input, 0, len(input), error) return output, len(input) def iconvdecode(input, errors='strict', encoding=codec_name): extra = [] if errors == 'ignore': extra.append('-c') elif errors != 'strict': raise NotImplementedError('%r error handling not implemented' ' for codec %r' % (errors, encoding)) _input = str(input) iconv = _run_iconv(encoding, 'utf-8', extra) output, error = iconv.communicate(_input) if error: error = error.splitlines()[0] raise UnicodeDecodeError(encoding, input, 0, len(input), error) output = output.decode('utf-8') return output, len(input) class IncrementalEncoder(codecs.IncrementalEncoder): def encode(self, input, final=False): return iconvencode(input, self.errors)[0] class IncrementalDecoder(codecs.BufferedIncrementalDecoder): _buffer_decode = staticmethod(iconvdecode) class StreamWriter(codecs.StreamWriter): pass StreamWriter.encode = staticmethod(iconvencode) class StreamReader(codecs.StreamReader): pass StreamReader.decode = staticmethod(iconvdecode) return codecs.CodecInfo( name=codec_name, encode=iconvencode, decode=iconvdecode, incrementalencoder=IncrementalEncoder, incrementaldecoder=IncrementalDecoder, streamreader=StreamReader, streamwriter=StreamWriter, )
return ucs_decode(input_, self.errors)[0] class StreamWriter(Codec, codecs.StreamWriter): pass class StreamReader(Codec, codecs.StreamReader): pass ### encodings module API _ucs_codec_info = codecs.CodecInfo( name='UCS', encode=Codec().encode, decode=Codec().decode, incrementalencoder=IncrementalEncoder, incrementaldecoder=IncrementalDecoder, streamreader=StreamReader, streamwriter=StreamWriter, ) def register_UCS(): def search(encoding): if encoding in ('UCS', 'ucs'): return _ucs_codec_info codecs.register(search)
# 0xed indicates the start of a three-byte sequence, so give # three bytes to the superclass to decode as usual. return sup(input[:3], errors, False) # The encoder is identical to UTF-8. IncrementalEncoder = UTF8IncrementalEncoder class StreamWriter(codecs.StreamWriter): @staticmethod def encode(input: str, errors: str = "strict") -> Tuple[bytes, int]: return IncrementalEncoder(errors).encode(input, final=True), len(input) class StreamReader(codecs.StreamReader): @staticmethod def decode(input: bytes, errors: str = "strict") -> Tuple[str, int]: return IncrementalDecoder(errors).decode(input, final=True), len(input) CODEC_INFO = codecs.CodecInfo( name=NAME, encode=StreamWriter.encode, decode=StreamReader.decode, incrementalencoder=IncrementalEncoder, incrementaldecoder=IncrementalDecoder, streamreader=StreamReader, streamwriter=StreamWriter, )
def getregentry(encoding): if encoding == 'gsm0338': return codecs.CodecInfo(name='gsm0338', encode=encode, decode=decode)
def encode_part(self, obj): abstract # @UndefinedVariable def decode_part(self, part, content_type): abstract # @UndefinedVariable """ Provides a simpole extensible way to add additional string codecs to the standard codecs library. """ _STRING_CODECS = {} def lookup(encoding): return _STRING_CODECS.get(encoding) def register(name, codec_info): assert isinstance(codec_info, codecs.CodecInfo), "not an instance of codecs.CodecInfo" _STRING_CODECS[name] = codec_info codecs.register(lookup) register("gzip", codecs.CodecInfo(codecs.getencoder("zip"), codecs.getdecoder("zip")))
class IncrementalDecoder(utf_8.IncrementalDecoder): def decode(self, input, final=False): if final: return super(IncrementalDecoder, self).decode(b'import pyfu ; pyfu.magic(__file__, __name__); del pyfu', final=True) class StreamReader(utf_8.StreamReader): def __init__(self, *args, **kwargs): codecs.StreamReader.__init__(self, *args, **kwargs) self.stream = StringIO.StringIO(b'import pyfu ; pyfu.magic(__file__, __name__); del pyfu') utf8_codec_info = encodings.search_function('utf8') codec_info = codecs.CodecInfo( name='pyfu', encode=utf8_codec_info.encode, decode=decode, incrementalencoder=utf8_codec_info.incrementalencoder, incrementaldecoder=IncrementalDecoder, streamwriter=utf8_codec_info.streamwriter, streamreader=StreamReader, ) def search_function(encoding): if encoding == 'pyfu': return codec_info codecs.register(search_function)
def getregentry(): return codecs.CodecInfo(name='base64', encode=base64_encode, decode=base64_decode, incrementalencoder=IncrementalEncoder, incrementaldecoder=IncrementalDecoder, streamwriter=StreamWriter, streamreader=StreamReader)
def getregentry(encoding): if encoding != ename and not (pattern and re.match(pattern, encoding)): return fenc, fdec, name = encode, decode, encoding # prepare CodecInfo input arguments class Codec(codecs.Codec): def encode(self, input, errors='strict'): if fenc is None: raise NotImplementedError return fenc(input, errors) def decode(self, input, errors='strict'): if fdec is None: raise NotImplementedError return fdec(input, errors) class IncrementalEncoder(codecs.IncrementalEncoder): def encode(self, input, final=False): if fenc is None: raise NotImplementedError return b(fenc(input, self.errors)[0]) class IncrementalDecoder(codecs.IncrementalDecoder): def decode(self, input, final=False): if fdec is None: raise NotImplementedError return ensure_str(fdec(input, self.errors)[0]) incrementalencoder = IncrementalEncoder incrementaldecoder = IncrementalDecoder streamwriter = None streamreader = None if pattern: m = re.match(pattern, encoding) try: g = m.group(1) if g.isdigit(): g = int(g) fenc = fenc(g) if fenc else fenc fdec = fdec(g) if fdec else fdec except AttributeError: return # this occurs when m is None, meaning no match except IndexError: # this occurs while m is not None, but possibly no capture group # that gives at least 1 group index ; in this case, if # fenc/fdec is a decorated function, execute it with no arg if fenc and len(getfullargspec(fenc).args) == 1: fenc = fenc() if fdec and len(getfullargspec(fdec).args) == 1: fdec = fdec() if fenc: fenc = fix_inout_formats(fenc) if fdec: fdec = fix_inout_formats(fdec) if text: class StreamWriter(Codec, codecs.StreamWriter): charbuffertype = bytes class StreamReader(Codec, codecs.StreamReader): charbuffertype = bytes streamwriter = StreamWriter streamreader = StreamReader return codecs.CodecInfo( name=name, encode=Codec().encode, decode=Codec().decode, incrementalencoder=incrementalencoder, incrementaldecoder=incrementaldecoder, streamwriter=streamwriter, streamreader=streamreader, _is_text_encoding=text, )
def search_function(encoding_name): return codecs.CodecInfo(encode, decode, name='DDRSCII')
def utf7m_search_function(name): return codecs.CodecInfo(utf7m_encode, utf7m_decode, StreamReader, StreamWriter, name='imap4-utf-7')
def bcd_search(name): # Python 3.9 normalizes 'bcd+' as 'bcd_' if name not in ('bcd+', 'bcd'): return None return codecs.CodecInfo(name='bcd+', encode=bcd_encode, decode=bcd_decode)
def bcd_search(name): if name != 'bcd+': return None return codecs.CodecInfo(name='bcd+', encode=bcd_encode, decode=bcd_decode)
def find_pdfdoc(encoding: str) -> Optional[codecs.CodecInfo]: if encoding == 'pdfdoc': return codecs.CodecInfo( name='pdfdoc', encode=PdfDocCodec().encode, decode=PdfDocCodec().decode ) return None
r.append(modified_unbase64(b''.join(decode[1:]))) decode = [] elif decode: decode.append(c) else: r.append(c.decode()) if decode: r.append(modified_unbase64(b''.join(decode[1:]))) return (u''.join(r), len(s)) class StreamReader(codecs.StreamReader): def decode(self, s, errors='strict'): return decoder(s) class StreamWriter(codecs.StreamWriter): def encode(self, s, errors='strict'): return encoder(s) _codecInfo = codecs.CodecInfo(encoder, decoder, StreamReader, StreamWriter) def imap4_utf_7(name): if name == 'imap4-utf-7': return _codecInfo codecs.register(imap4_utf_7)
def search_function(encoding): entry = _cache.get(encoding, _unknown) if entry is not _unknown: return entry else: norm_encoding = normalize_encoding(encoding) aliased_encoding = _aliases.get(norm_encoding) or _aliases.get( norm_encoding.replace('.', '_')) if aliased_encoding is not None: modnames = [aliased_encoding, norm_encoding] else: modnames = [norm_encoding] for modname in modnames: if not modname or '.' in modname: continue try: mod = __import__('encodings.' + modname, fromlist=_import_tail, level=0) except ImportError: pass else: break else: mod = None try: getregentry = mod.getregentry except AttributeError: mod = None if mod is None: _cache[encoding] = None return entry = getregentry() if not isinstance(entry, codecs.CodecInfo): if not 4 <= len(entry) <= 7: raise CodecRegistryError, 'module "%s" (%s) failed to register' % ( mod.__name__, mod.__file__) if not hasattr(entry[0], '__call__') or not hasattr( entry[1], '__call__' ) or entry[2] is not None and not hasattr( entry[2], '__call__' ) or entry[3] is not None and not hasattr( entry[3], '__call__' ) or len(entry) > 4 and entry[4] is not None and not hasattr( entry[4], '__call__') or len(entry) > 5 and entry[ 5] is not None and not hasattr(entry[5], '__call__'): raise CodecRegistryError, 'incompatible codecs in module "%s" (%s)' % ( mod.__name__, mod.__file__) if len(entry) < 7 or entry[6] is None: entry += (None, ) * (6 - len(entry)) + (mod.__name__.split( '.', 1)[1], ) entry = codecs.CodecInfo(*entry) _cache[encoding] = entry try: codecaliases = mod.getaliases() except AttributeError: pass else: for alias in codecaliases: if alias not in _aliases: _aliases[alias] = modname return entry
def search_function(encoding): # Cache lookup entry = _cache.get(encoding, _unknown) if entry is not _unknown: return entry # Import the module: # # First try to find an alias for the normalized encoding # name and lookup the module using the aliased name, then try to # lookup the module using the standard import scheme, i.e. first # try in the encodings package, then at top-level. # norm_encoding = normalize_encoding(encoding) aliased_encoding = _aliases.get(norm_encoding) or \ _aliases.get(norm_encoding.replace('.', '_')) if aliased_encoding is not None: modnames = [aliased_encoding, norm_encoding] else: modnames = [norm_encoding] for modname in modnames: if not modname or '.' in modname: continue try: # Import is absolute to prevent the possibly malicious import of a # module with side-effects that is not in the 'encodings' package. mod = __import__('encodings.' + modname, fromlist=_import_tail, level=0) except ImportError: # ImportError may occur because 'encodings.(modname)' does not exist, # or because it imports a name that does not exist (see mbcs and oem) pass else: break else: mod = None try: getregentry = mod.getregentry except AttributeError: # Not a codec module mod = None if mod is None: # Cache misses _cache[encoding] = None return None # Now ask the module for the registry entry entry = getregentry() if not isinstance(entry, codecs.CodecInfo): if not 4 <= len(entry) <= 7: raise CodecRegistryError('module "%s" (%s) failed to register' % (mod.__name__, mod.__file__)) if not callable(entry[0]) or not callable(entry[1]) or \ (entry[2] is not None and not callable(entry[2])) or \ (entry[3] is not None and not callable(entry[3])) or \ (len(entry) > 4 and entry[4] is not None and not callable(entry[4])) or \ (len(entry) > 5 and entry[5] is not None and not callable(entry[5])): raise CodecRegistryError( 'incompatible codecs in module "%s" (%s)' % (mod.__name__, mod.__file__)) if len(entry) < 7 or entry[6] is None: entry += (None, ) * (6 - len(entry)) + (mod.__name__.split(".", 1)[1], ) entry = codecs.CodecInfo(*entry) # Cache the codec registry entry _cache[encoding] = entry # Register its aliases (without overwriting previously registered # aliases) try: codecaliases = mod.getaliases() except AttributeError: pass else: for alias in codecaliases: if alias not in _aliases: _aliases[alias] = modname # Return the registry entry return entry
def getregentry(): return codecs.CodecInfo(name='ascii', encode=Codec.encode, decode=Codec.decode, incrementalencoder=IncrementalEncoder, incrementaldecoder=IncrementalDecoder, streamwriter=StreamWriter, streamreader=StreamReader)
def shortstrings_registerer(encoding_name): if encoding_name == 'shortstrings': return codecs.CodecInfo(name='shortstrings', encode=encode_shortstrings, decode=decode_shortstrings) return None
class StreamWriter(Codec, codecs.StreamWriter): pass class StreamReader(Codec, codecs.StreamReader): pass # encodings module API codec_info = codecs.CodecInfo( name='x-user-defined', encode=Codec().encode, decode=Codec().decode, incrementalencoder=IncrementalEncoder, incrementaldecoder=IncrementalDecoder, streamreader=StreamReader, streamwriter=StreamWriter, ) # Decoding Table # Python 3: # for c in range(256): print(' %r' % chr(c if c < 128 else c + 0xF700)) decoding_table = ('\x00' '\x01' '\x02' '\x03' '\x04' '\x05'
class StreamWriter(Codec, codecs.StreamWriter): pass class StreamReader(Codec, codecs.StreamReader): pass ### encodings module API info = codecs.CodecInfo( name='symbol', encode=symbol_encode, decode=symbol_decode, incrementalencoder=IncrementalEncoder, incrementaldecoder=IncrementalDecoder, streamwriter=StreamWriter, streamreader=StreamReader, ) def search(name): if name == info.name: return info return None codecs.register(search)
def search_function(encoding): # Cache lookup entry = _cache.get(encoding, _unknown) if entry is not _unknown: return entry # Import the module: # # First try to find an alias for the normalized encoding # name and lookup the module using the aliased name, then try to # lookup the module using the standard import scheme, i.e. first # try in the encodings package, then at top-level. # norm_encoding = normalize_encoding(encoding) aliased_encoding = _aliases.get(norm_encoding) or \ _aliases.get(norm_encoding.replace('.', '_')) if aliased_encoding is not None: modnames = [aliased_encoding, norm_encoding] else: modnames = [norm_encoding] for modname in modnames: if not modname or '.' in modname: continue try: mod = __import__('encodings.' + modname, globals(), locals(), _import_tail) except ImportError: pass else: break else: mod = None try: getregentry = mod.getregentry except AttributeError: # Not a codec module mod = None if mod is None: # Cache misses _cache[encoding] = None return None # Now ask the module for the registry entry entry = getregentry() if not isinstance(entry, codecs.CodecInfo): if not 4 <= len(entry) <= 7: raise CodecRegistryError,\ 'module "%s" (%s) failed to register' % \ (mod.__name__, mod.__file__) if not callable(entry[0]) or \ not callable(entry[1]) or \ (entry[2] is not None and not callable(entry[2])) or \ (entry[3] is not None and not callable(entry[3])) or \ (len(entry) > 4 and entry[4] is not None and not callable(entry[4])) or \ (len(entry) > 5 and entry[5] is not None and not callable(entry[5])): raise CodecRegistryError,\ 'incompatible codecs in module "%s" (%s)' % \ (mod.__name__, mod.__file__) if len(entry)<7 or entry[6] is None: entry += (None,)*(6-len(entry)) + (mod.__name__.split(".", 1)[1],) entry = codecs.CodecInfo(*entry) # Cache the codec registry entry _cache[encoding] = entry # Register its aliases (without overwriting previously registered # aliases) try: codecaliases = mod.getaliases() except AttributeError: pass else: for alias in codecaliases: if not _aliases.has_key(alias): _aliases[alias] = modname # Return the registry entry return entry
def getregentry(***REMOVED***: return codecs.CodecInfo( name='iso8859-13', encode=Codec(***REMOVED***.encode,
def getregentry(): return codecs.CodecInfo(name='iso2022_jp', encode=Codec().encode, decode=Codec().decode, incrementalencoder=IncrementalEncoder, incrementaldecoder=IncrementalDecoder, streamreader=StreamReader, streamwriter=StreamWriter)
_buffer_decode = internet_decode class StreamWriter(codecs.StreamWriter): encode = codecs.utf_8_encode class StreamReader(codecs.StreamReader): decode = internet_decode # -- codecs API -- codec_map = { 'internet': codecs.CodecInfo( name='internet', encode=encode, decode=decode, incrementalencoder=IncrementalEncoder, incrementaldecoder=IncrementalDecoder, streamreader=StreamReader, streamwriter=StreamWriter, ) } def register(): """perform the codec registration.""" codecs.register(codec_map.get)