classification
Title: codecs.StreamWriter.reset contract not fulfilled
Type: behavior Stage: patch review
Components: Unicode Versions: Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: lemburg Nosy List: Jim.Jewett, cheryl.sabella, ezio.melotti, lemburg, martin.panter
Priority: normal Keywords: patch

Created on 2012-02-14 18:01 by Jim.Jewett, last changed 2020-01-15 21:55 by vstinner.

Files
File name Uploaded Description Edit
Issue14014.diff BreamoreBoy, 2014-07-03 13:50 patch file with Jim's suggestion. review
Pull Requests
URL Status Linked Edit
PR 13716 open berker.peksag, 2019-06-01 04:42
Messages (8)
msg153354 - (view) Author: Jim Jewett (Jim.Jewett) (Python triager) Date: 2012-02-14 18:01
def reset(self):

        """ Flushes and resets the codec buffers used for keeping state.

            Calling this method should ensure that the data on the
            output is put into a clean state, that allows appending
            of new fresh data without having to rescan the whole
            stream to recover state.

        """
        pass

This does not ensure that the stream is flushed, as the docstring promises.  I believe the following would work better.


    def reset(self):
        """ Flushes and resets the codec buffers used for keeping state.

            Calling this method should ensure that the data on the
            output is put into a clean state, that allows appending
            of new fresh data without having to rescan the whole
            stream to recover state.

        """
        if hasattr(self.stream, "flush"): self.stream.flush()
msg222178 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-07-03 13:50
On Windows 7 206 codecs tests passed and 4 skipped with the patch included.
msg233988 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-01-14 01:21
I don’t think this is appropriate. If you want to flush the underlying stream, then call its flush() method after calling reset(). The docstring only says it flushes the _codec’s_ buffers, not any buffers of the underlying stream, and it should not be the codec’s business to worry about lower level buffers.
msg233994 - (view) Author: Jim Jewett (Jim.Jewett) (Python triager) Date: 2015-01-14 01:37
That sounds like a bug magnet to me; my mental model is that the codec
is my output; flushing it will push things out, and resetting it will
erase anything pending.  I don't care if some implementation detail
means that some other object technically owns the buffer.

An alternative (inferior, but better than nothing) would be to add an
explicit note warning users that reset won't really finish the job,
and they have to also get the codec's underlying stream and flush
that.  (Of course, if the stream is really an abstraction over the
real stream ... at what point does the delegation start to work on its
own?)

On Tue, Jan 13, 2015 at 8:21 PM, Martin Panter <report@bugs.python.org> wrote:
>
> Martin Panter added the comment:
>
> I don’t think this is appropriate. If you want to flush the underlying stream, then call its flush() method after calling reset(). The docstring only says it flushes the _codec’s_ buffers, not any buffers of the underlying stream, and it should not be the codec’s business to worry about lower level buffers.
>
> ----------
> nosy: +vadmium
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue14014>
> _______________________________________
msg233999 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-01-14 02:26
Maybe it would be better to redefine the docstring to say it flushes the codec as well as calling flush() on the underlying stream.

But if you really want to finish the job you should probably be closing the underlying stream, which would flush if necessary. See Issue 460474, for adding a close() method which invokes reset().
msg234014 - (view) Author: Marc-Andre Lemburg (lemburg) * (Python committer) Date: 2015-01-14 08:34
On 14.01.2015 02:21, Martin Panter wrote:
> 
> Martin Panter added the comment:
> 
> I don’t think this is appropriate. If you want to flush the underlying stream, then call its flush() method after calling reset(). The docstring only says it flushes the _codec’s_ buffers, not any buffers of the underlying stream, and it should not be the codec’s business to worry about lower level buffers.

Correct. That's the reason why the method is called .reset() and
not .flush().
msg234015 - (view) Author: Marc-Andre Lemburg (lemburg) * (Python committer) Date: 2015-01-14 08:41
Adding a note to the documentation is fine.

The .reset() method doesn't have anything to do with the underlying
stream. It's only meant to work at the codec level and needed for
codecs that keep internal state.
msg359849 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2020-01-12 14:21
@lemburg, when you get a chance, please review the PR.  Thanks!
History
Date User Action Args
2020-01-15 21:55:07vstinnersetnosy: - vstinner
2020-01-12 14:21:05cheryl.sabellasetversions: + Python 3.8, Python 3.9, - Python 3.4, Python 3.5
nosy: + cheryl.sabella

messages: + msg359849

assignee: lemburg
2019-06-01 04:42:29berker.peksagsetstage: patch review
pull_requests: + pull_request13603
2019-04-26 17:22:45BreamoreBoysetnosy: - BreamoreBoy
2015-01-14 08:41:11lemburgsetmessages: + msg234015
2015-01-14 08:34:47lemburgsetnosy: + lemburg
messages: + msg234014
2015-01-14 02:26:56martin.pantersetmessages: + msg233999
2015-01-14 01:37:23Jim.Jewettsetmessages: + msg233994
2015-01-14 01:21:18martin.pantersetnosy: + martin.panter
messages: + msg233988
2014-07-03 13:50:55BreamoreBoysetfiles: + Issue14014.diff
versions: + Python 3.4, Python 3.5, - Python 3.2, Python 3.3
nosy: + BreamoreBoy

messages: + msg222178

keywords: + patch
2012-02-15 00:13:19pitrousetnosy: + vstinner

versions: + Python 3.3
2012-02-14 18:01:53Jim.Jewettcreate