[Python-ideas] changing sys.stdout encoding

Amaury Forgeot d'Arc amauryfa at gmail.com
Tue Jun 5 23:22:27 CEST 2012

2012/6/5 Stephen J. Turnbull <stephen at xemacs.org>

> I wouldn't object to a method with the semantics of reinitialization,
> but it should have a name implying reinitialization.  It probably
> should also error if the stream is open and has been written to.

What do you think of the following method TextIOWrapper.reset_encoding?
(the assert statements should certainly be replaced by some IOError)

    def reset_encoding(self, encoding, errors='strict'):
        if self._decoder:
            # No decoded chars awaiting read
            assert self._decoded_chars_used == len(self._decoded_chars)
            # Nothing in the input buffer
            buf, flag = self._decoder.getstate()
            assert buf == b''
        if self._encoder:
            # Nothing in the output buffer
            buf = self._encoder.encode('', final=True)
            assert buf == b''
        # Reset the decoders
        self._decoder = None
        self._encoder = None
        # Now change the encoding
        self._encoding = encoding
        self._errors = errors

Amaury Forgeot d'Arc
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120605/457ab1b7/attachment.html>

More information about the Python-ideas mailing list