diff --git a/Doc/library/tempfile.rst b/Doc/library/tempfile.rst index bf9198e175a0e1..8a00701e37137b 100644 --- a/Doc/library/tempfile.rst +++ b/Doc/library/tempfile.rst @@ -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) @@ -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 >>> fp.read() b'Hello world!' # close the file, it will be removed @@ -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 diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py index b2b5390af33b00..c5d5e4aa572e59 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -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) diff --git a/Misc/NEWS.d/next/Library/2021-10-24-01-28-07.bpo-45593.QjC6-D.rst b/Misc/NEWS.d/next/Library/2021-10-24-01-28-07.bpo-45593.QjC6-D.rst new file mode 100644 index 00000000000000..8609b15cc78a13 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-10-24-01-28-07.bpo-45593.QjC6-D.rst @@ -0,0 +1 @@ +Make :meth:`!tempfile.SpooledTemporaryFile.truncate` return new file size.