#6077: on Windows, fix truncation of a tempfile.TemporaryFile opened … · wrongnull/cpython@7d0bddd · GitHub
Skip to content

Commit 7d0bddd

Browse files
committed
python#6077: on Windows, fix truncation of a tempfile.TemporaryFile opened in "wt+" mode:
files opened with os.open() stop on the first \x1a (Ctrl-Z) unless os.O_BINARY is used. Will backport to 3.1
1 parent acafc2d commit 7d0bddd

3 files changed

Lines changed: 16 additions & 12 deletions

File tree

Lib/tempfile.py

Lines changed: 3 additions & 10 deletions

Lib/test/test_tempfile.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,12 @@ def test_textmode(self):
309309
if not has_textmode:
310310
return # ugh, can't use SkipTest.
311311

312-
self.do_create(bin=0).write(b"blat\n")
313-
# XXX should test that the file really is a text file
312+
# A text file is truncated at the first Ctrl+Z byte
313+
f = self.do_create(bin=0)
314+
f.write(b"blat\x1a")
315+
f.write(b"extra\n")
316+
os.lseek(f.fd, 0, os.SEEK_SET)
317+
self.assertEquals(os.read(f.fd, 20), b"blat")
314318

315319
test_classes.append(test__mkstemp_inner)
316320

@@ -761,6 +765,10 @@ def test_text_mode(self):
761765
f.write("xyzzy\n")
762766
f.seek(0)
763767
self.assertEqual(f.read(), "abc\ndef\nxyzzy\n")
768+
# Check that Ctrl+Z doesn't truncate the file
769+
f.write("foo\x1abar\n")
770+
f.seek(0)
771+
self.assertEqual(f.read(), "abc\ndef\nxyzzy\nfoo\x1abar\n")
764772

765773
def test_text_newline_and_encoding(self):
766774
f = tempfile.SpooledTemporaryFile(mode='w+', max_size=10,

Misc/NEWS

Lines changed: 3 additions & 0 deletions

0 commit comments

Comments
 (0)