diff options
author | Inada Naoki <songofacandy@gmail.com> | 2019-11-27 13:22:06 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-27 13:22:06 (GMT) |
commit | ea9835c5d154ab6a54eed627958473b6768b28cc (patch) | |
tree | e65f74eab9ca75f936121582111232d970aa159c /Lib/tempfile.py | |
parent | 1bddf890e595a865414645c6041733043c4081f8 (diff) | |
download | cpython-ea9835c5d154ab6a54eed627958473b6768b28cc.zip cpython-ea9835c5d154ab6a54eed627958473b6768b28cc.tar.gz cpython-ea9835c5d154ab6a54eed627958473b6768b28cc.tar.bz2 |
bpo-26730: Fix SpooledTemporaryFile data corruption (GH-17400)
SpooledTemporaryFile.rollback() might cause data corruption
when it is in text mode.
Co-Authored-By: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Lib/tempfile.py')
-rw-r--r-- | Lib/tempfile.py | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/Lib/tempfile.py b/Lib/tempfile.py index 45709cb..6287554 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -633,10 +633,9 @@ class SpooledTemporaryFile: if 'b' in mode: self._file = _io.BytesIO() else: - # Setting newline="\n" avoids newline translation; - # this is important because otherwise on Windows we'd - # get double newline translation upon rollover(). - self._file = _io.StringIO(newline="\n") + self._file = _io.TextIOWrapper(_io.BytesIO(), + encoding=encoding, errors=errors, + newline=newline) self._max_size = max_size self._rolled = False self._TemporaryFileArgs = {'mode': mode, 'buffering': buffering, @@ -656,8 +655,12 @@ class SpooledTemporaryFile: newfile = self._file = TemporaryFile(**self._TemporaryFileArgs) del self._TemporaryFileArgs - newfile.write(file.getvalue()) - newfile.seek(file.tell(), 0) + pos = file.tell() + if hasattr(newfile, 'buffer'): + newfile.buffer.write(file.detach().getvalue()) + else: + newfile.write(file.getvalue()) + newfile.seek(pos, 0) self._rolled = True |