[Python-Dev] Do I misunderstand how codecs.EncodedFile is supposed to work?

M.-A. Lemburg mal@lemburg.com
Wed, 07 Aug 2002 09:46:55 +0200

Martin v. Loewis wrote:
> Skip Montanaro <skip@pobox.com> writes:
>>I thought the whole purpose of the EncodedFile class was to provide
>>transparent encoding.  
>     """ Return a wrapped version of file which provides transparent
>         encoding translation.
>         Strings written to the wrapped file are interpreted according
>         to the given data_encoding and then written to the original
>         file as string using file_encoding. The intermediate encoding
>         will usually be Unicode but depends on the specified codecs.
>         Strings are read from the file using file_encoding and then
>         passed back to the caller as string using data_encoding.
>         If file_encoding is not given, it defaults to data_encoding.
>     """
> So, no. It provides transparent recoding: with a file encoding, and a
> data encoding.
> I never found this class useful.

It's not a class, just a helper for StreamRecoder. It's purpose
is to provide an easy way of saying "the inside world is encoding
X while the outside world uses Y":

     # Make stdout translate Latin-1 output into UTF-8 output
     sys.stdout = EncodedFile(sys.stdout, 'latin-1', 'utf-8')

     # Have stdin translate UTF-8 input into Latin-1 input
     sys.stdin = EncodedFile(sys.stdin, 'utf-8', 'latin-1')

Here the inside world uses Latin-1 while the outside world
uses UTF-8.

You could also use it to talk to a gzipped file or, provided
you have such a codec, to an encrypted file.

Marc-Andre Lemburg
CEO eGenix.com Software GmbH
eGenix.com -- Makers of the Python mx Extensions: mxDateTime,mxODBC,...
Python Consulting:                               http://www.egenix.com/
Python Software:                    http://www.egenix.com/files/python/