summaryrefslogtreecommitdiffstats
path: root/Lib/tempfile.py
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2019-11-28 05:23:14 (GMT)
committerGitHub <noreply@github.com>2019-11-28 05:23:14 (GMT)
commitd21b8e82dda3caf0989bb39bc0e0ce2bfef97081 (patch)
tree3c175fd009be267bcbf60d7e193c58f6ba24bcc4 /Lib/tempfile.py
parent0f9c9d53283420a570850aa92869d032b40d4fba (diff)
downloadcpython-d21b8e82dda3caf0989bb39bc0e0ce2bfef97081.zip
cpython-d21b8e82dda3caf0989bb39bc0e0ce2bfef97081.tar.gz
cpython-d21b8e82dda3caf0989bb39bc0e0ce2bfef97081.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> (cherry picked from commit ea9835c5d154ab6a54eed627958473b6768b28cc) Co-authored-by: Inada Naoki <songofacandy@gmail.com>
Diffstat (limited to 'Lib/tempfile.py')
-rw-r--r--Lib/tempfile.py15
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