MCPcopy Index your code
hub / github.com/python/cpython / StreamRecoder

Class StreamRecoder

Lib/codecs.py:767–882  ·  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 on the

Source from the content-addressed store, hash-verified

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

Callers 1

EncodedFileFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…