Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Doc/library/tempfile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ The module defines the following user-callable items:
:class:`io.TextIOBase` abstract base classes (depending on whether binary
or text *mode* was specified).

.. versionchanged:: next
The truncate method returns new file size.


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

Expand Down Expand Up @@ -391,8 +394,10 @@ Here are some examples of typical usage of the :mod:`!tempfile` module::
# create a temporary file and write some data to it
>>> fp = tempfile.TemporaryFile()
>>> fp.write(b'Hello world!')
12
# read data from file
>>> fp.seek(0)
0
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we add 0 here, we should add 12 after fp.write(b'Hello world!') too.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And later,

    >>> with tempfile.TemporaryFile() as fp:
    ...     fp.write(b'Hello world!')
    ...     fp.seek(0)
    ...     fp.read()
    b'Hello world!'

This output should be:

12
0
b'Hello world!'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems this file is not tested by doctest.

Please leave it. Otherwise, we need to fix all output and remove all comments in sample code.

>>> fp.read()
b'Hello world!'
# close the file, it will be removed
Expand All @@ -403,6 +408,8 @@ Here are some examples of typical usage of the :mod:`!tempfile` module::
... fp.write(b'Hello world!')
... fp.seek(0)
... fp.read()
12
0
b'Hello world!'
>>>
# file is now closed and removed
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1532,6 +1532,19 @@ def test_truncate_with_size_parameter(self):
self.assertTrue(f._rolled)
self.assertEqual(os.fstat(f.fileno()).st_size, 20)

def test_truncate_return_size(self):
"SpooledTemporaryFile truncate should return new position"
f = tempfile.SpooledTemporaryFile(max_size=10)
f.write(b'abcdef')
self.assertEqual(f.truncate(3), 3)

def test_seek_return_position(self):
"SpooledTemporaryFile seek should return file position"
f = tempfile.SpooledTemporaryFile(max_size=10)
f.write(b'abcdef')
self.assertEqual(f.seek(3), 3)
self.assertEqual(f.seek(0, 2), 6)

def test_class_getitem(self):
self.assertIsInstance(tempfile.SpooledTemporaryFile[bytes],
types.GenericAlias)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make :meth:`!tempfile.SpooledTemporaryFile.truncate` return new file size.
Loading