MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / StreamRecoder

Class StreamRecoder

tools/python-3.11.9-amd64/Lib/codecs.py:764–879  ·  view source on GitHub ↗

StreamRecoder instances translate data from one encoding to another. They use the complete set of APIs returned by the codecs.lookup() function to implement their task. Data written to the StreamRecoder is first decoded into an intermediate format (depending

Source from the content-addressed store, hash-verified

762###
763
764class StreamRecoder:
765
766 """ StreamRecoder instances translate data from one encoding to another.
767
768 They use the complete set of APIs returned by the
769 codecs.lookup() function to implement their task.
770
771 Data written to the StreamRecoder is first decoded into an
772 intermediate format (depending on the "decode" codec) and then
773 written to the underlying stream using an instance of the provided
774 Writer class.
775
776 In the other direction, data is read from the underlying stream using
777 a Reader instance and then encoded and returned to the caller.
778
779 """
780 # Optional attributes set by the file wrappers below
781 data_encoding = 'unknown'
782 file_encoding = 'unknown'
783
784 def __init__(self, stream, encode, decode, Reader, Writer,
785 errors='strict'):
786
787 """ Creates a StreamRecoder instance which implements a two-way
788 conversion: encode and decode work on the frontend (the
789 data visible to .read() and .write()) while Reader and Writer
790 work on the backend (the data in stream).
791
792 You can use these objects to do transparent
793 transcodings from e.g. latin-1 to utf-8 and back.
794
795 stream must be a file-like object.
796
797 encode and decode must adhere to the Codec interface; Reader and
798 Writer must be factory functions or classes providing the
799 StreamReader and StreamWriter interfaces resp.
800
801 Error handling is done in the same way as defined for the
802 StreamWriter/Readers.
803
804 """
805 self.stream = stream
806 self.encode = encode
807 self.decode = decode
808 self.reader = Reader(stream, errors)
809 self.writer = Writer(stream, errors)
810 self.errors = errors
811
812 def read(self, size=-1):
813
814 data = self.reader.read(size)
815 data, bytesencoded = self.encode(data, self.errors)
816 return data
817
818 def readline(self, size=None):
819
820 if size is None:
821 data = self.reader.readline()

Callers 1

EncodedFileFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected