classification
Title: SpooledTemporaryFile.__iter__ is not transparent to rollover
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: jinoh.kang.kr
Priority: normal Keywords:

Created on 2021-01-08 14:46 by jinoh.kang.kr, last changed 2021-01-08 14:46 by jinoh.kang.kr.

Messages (1)
msg384674 - (view) Author: Jinoh Kang (jinoh.kang.kr) Date: 2021-01-08 14:46
In tempfile, SpooledTemporaryFile.__iter__ is defined as follows:

    # file protocol
    def __iter__(self):
        return self._file.__iter__()

A rollover would switch the underlying _file object from a BytesIO to a TemporaryFile, thereby leaving the original iterator stale.

This may be fixed by:

    def __iter__(self):
        while True:
            line = self._file.readline()
            if not line:
                break
            yield line

Or perhaps:

    def __iter__(self):
        while True:
            file = self._file
            for line in file:
                yield line
                if file is not self._file:
                    break
            else:
                break
History
Date User Action Args
2021-01-08 14:46:35jinoh.kang.krcreate