Skip to content

Commit 233eda9

Browse files
pbo-45593: make SpooledTemporaryFile.truncate return new file size
1 parent 7d2deaf commit 233eda9

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

Doc/library/tempfile.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ The module defines the following user-callable items:
172172
:class:`io.TextIOBase` abstract base classes (depending on whether binary
173173
or text *mode* was specified).
174174

175+
.. versionchanged:: 3.12
176+
The truncate method returns new file size.
177+
175178

176179
.. class:: TemporaryDirectory(suffix=None, prefix=None, dir=None, ignore_cleanup_errors=False, *, delete=True)
177180

@@ -388,6 +391,7 @@ Here are some examples of typical usage of the :mod:`tempfile` module::
388391
>>> fp.write(b'Hello world!')
389392
# read data from file
390393
>>> fp.seek(0)
394+
0
391395
>>> fp.read()
392396
b'Hello world!'
393397
# close the file, it will be removed

Lib/test/test_tempfile.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,6 +1470,19 @@ def test_truncate_with_size_parameter(self):
14701470
self.assertTrue(f._rolled)
14711471
self.assertEqual(os.fstat(f.fileno()).st_size, 20)
14721472

1473+
def test_truncate_return_size(self):
1474+
"SpooledTemporaryFile truncate should return new position"
1475+
f = tempfile.SpooledTemporaryFile(max_size=10)
1476+
f.write(b'abcdef')
1477+
self.assertEqual(f.truncate(3), 3)
1478+
1479+
def test_seek_return_position(self):
1480+
"SpooledTemporaryFile seek should return file position"
1481+
f = tempfile.SpooledTemporaryFile(max_size=10)
1482+
f.write(b'abcdef')
1483+
self.assertEqual(f.seek(3), 3)
1484+
self.assertEqual(f.seek(0, 2), 6)
1485+
14731486
def test_class_getitem(self):
14741487
self.assertIsInstance(tempfile.SpooledTemporaryFile[bytes],
14751488
types.GenericAlias)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make :meth:`tempfile.SpooledTemporaryFile.truncate()` to return new file size.

0 commit comments

Comments
 (0)