cpython: 5a6911930bad (original) (raw)
Mercurial > cpython
changeset 73737:5a6911930bad
Issue #9957: SpooledTemporaryFile.truncate() now accepts an optional size parameter, as other file-like objects. Patch by Ryan Kelly. [#9957]
Antoine Pitrou solipsis@pitrou.net | |
---|---|
date | Fri, 25 Nov 2011 18:03:09 +0100 |
parents | 905b6f1eca74 |
children | e1dbc72bd97f |
files | Lib/tempfile.py Lib/test/test_tempfile.py Misc/NEWS |
diffstat | 3 files changed, 31 insertions(+), 3 deletions(-)[+] [-] Lib/tempfile.py 9 Lib/test/test_tempfile.py 21 Misc/NEWS 4 |
line wrap: on
line diff
--- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -578,8 +578,13 @@ class SpooledTemporaryFile: def tell(self): return self._file.tell()
- def truncate(self, size=None):
if size is None:[](#l1.10)
self._file.truncate()[](#l1.11)
else:[](#l1.12)
if size > self._max_size:[](#l1.13)
self.rollover()[](#l1.14)
self._file.truncate(size)[](#l1.15)
def write(self, s): file = self._file
--- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -846,6 +846,27 @@ class test_SpooledTemporaryFile(TC): pass self.assertRaises(ValueError, use_closed)
- def test_truncate_with_size_parameter(self):
# A SpooledTemporaryFile can be truncated to zero size[](#l2.8)
f = tempfile.SpooledTemporaryFile(max_size=10)[](#l2.9)
f.write(b'abcdefg\n')[](#l2.10)
f.seek(0)[](#l2.11)
f.truncate()[](#l2.12)
self.assertFalse(f._rolled)[](#l2.13)
self.assertEqual(f._file.getvalue(), b'')[](#l2.14)
# A SpooledTemporaryFile can be truncated to a specific size[](#l2.15)
f = tempfile.SpooledTemporaryFile(max_size=10)[](#l2.16)
f.write(b'abcdefg\n')[](#l2.17)
f.truncate(4)[](#l2.18)
self.assertFalse(f._rolled)[](#l2.19)
self.assertEqual(f._file.getvalue(), b'abcd')[](#l2.20)
# A SpooledTemporaryFile rolls over if truncated to large size[](#l2.21)
f = tempfile.SpooledTemporaryFile(max_size=10)[](#l2.22)
f.write(b'abcdefg\n')[](#l2.23)
f.truncate(20)[](#l2.24)
self.assertTrue(f._rolled)[](#l2.25)
if has_stat:[](#l2.26)
self.assertEqual(os.fstat(f.fileno()).st_size, 20)[](#l2.27)
test_classes.append(test_SpooledTemporaryFile)
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -386,10 +386,12 @@ Core and Builtins