From 717a9275be75744ef1ab904c6d4815ad8ee43676 Mon Sep 17 00:00:00 2001 From: Maciej Urbanski Date: Sun, 24 Oct 2021 01:10:47 +0200 Subject: [PATCH 1/3] pbo-45593: make SpooledTemporaryFile.truncate return new file size --- Doc/library/tempfile.rst | 4 ++++ Lib/test/test_tempfile.py | 13 +++++++++++++ .../2021-10-24-01-28-07.bpo-45593.QjC6-D.rst | 1 + 3 files changed, 18 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2021-10-24-01-28-07.bpo-45593.QjC6-D.rst diff --git a/Doc/library/tempfile.rst b/Doc/library/tempfile.rst index fd4c294613fd31..c7f2af86fab166 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:: 3.12 + The truncate method returns new file size. + .. class:: TemporaryDirectory(suffix=None, prefix=None, dir=None, ignore_cleanup_errors=False, *, delete=True) @@ -388,6 +391,7 @@ Here are some examples of typical usage of the :mod:`tempfile` module:: >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) + 0 >>> fp.read() b'Hello world!' # close the file, it will be removed diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py index db08fb1c7f2a42..3e09b753a69a67 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -1470,6 +1470,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..1c20b8e19422ea --- /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()` to return new file size. From fa3b08ff10ae85d1e4c4046fb026c02d2e4b0a1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=22RooTer=22=20Urba=C5=84ski?= Date: Wed, 9 Aug 2023 22:47:31 +0200 Subject: [PATCH 2/3] fix versionchanged in Doc/library/tempfile.rst Co-authored-by: Inada Naoki --- Doc/library/tempfile.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/tempfile.rst b/Doc/library/tempfile.rst index c2ba237bbd8868..aae1f2fdc3e81d 100644 --- a/Doc/library/tempfile.rst +++ b/Doc/library/tempfile.rst @@ -172,7 +172,7 @@ The module defines the following user-callable items: :class:`io.TextIOBase` abstract base classes (depending on whether binary or text *mode* was specified). - .. versionchanged:: 3.12 + .. versionchanged:: 3.13 The truncate method returns new file size. From 1c40e9f4ecceba727944940a2eec91c18d8c662a Mon Sep 17 00:00:00 2001 From: Maciej Urbanski Date: Mon, 11 May 2026 12:25:38 +0200 Subject: [PATCH 3/3] fix SpooledTemporaryFile docs --- Doc/library/tempfile.rst | 5 ++++- .../next/Library/2021-10-24-01-28-07.bpo-45593.QjC6-D.rst | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Doc/library/tempfile.rst b/Doc/library/tempfile.rst index 7e8cb61367f21d..8a00701e37137b 100644 --- a/Doc/library/tempfile.rst +++ b/Doc/library/tempfile.rst @@ -172,7 +172,7 @@ The module defines the following user-callable items: :class:`io.TextIOBase` abstract base classes (depending on whether binary or text *mode* was specified). - .. versionchanged:: 3.13 + .. versionchanged:: next The truncate method returns new file size. @@ -394,6 +394,7 @@ 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 @@ -407,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/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 index 1c20b8e19422ea..8609b15cc78a13 100644 --- 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 @@ -1 +1 @@ -Make :meth:`tempfile.SpooledTemporaryFile.truncate()` to return new file size. +Make :meth:`!tempfile.SpooledTemporaryFile.truncate` return new file size.