From 66b268ae3dad115086a86a1f484aa26ec898f396 Mon Sep 17 00:00:00 2001 From: Andrzej Mateja Date: Wed, 2 Jun 2021 17:14:55 +0200 Subject: [PATCH 1/4] bpo-44289: Keep argument file object's current position in tarfile.is_tarifle function. --- Lib/tarfile.py | 2 ++ Lib/test/test_tarfile.py | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/Lib/tarfile.py b/Lib/tarfile.py index 18d415adf54418..331e24adf6388e 100755 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -2484,7 +2484,9 @@ def is_tarfile(name): """ try: if hasattr(name, "read"): + pos = name.tell() t = open(fileobj=name) + name.seek(pos) else: t = open(name) t.close() diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index 817e6f17997121..0928b5ad8e2e48 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -352,6 +352,18 @@ def test_is_tarfile_valid(self): with open(self.tarname, "rb") as fobj: self.assertTrue(tarfile.is_tarfile(io.BytesIO(fobj.read()))) + def test_is_tarfile_keeps_position(self): + # Test for issue44289: tarfile.is_tarfile() modifies + # file object's current position + with open(self.tarname, "rb") as fobj: + tarfile.is_tarfile(fobj) + self.assertEqual(fobj.tell(), 0) + + with open(self.tarname, "rb") as fobj: + file_like = io.BytesIO(fobj.read()) + tarfile.is_tarfile(file_like) + self.assertEqual(file_like.tell(), 0) + def test_empty_tarfile(self): # Test for issue6123: Allow opening empty archives. # This test checks if tarfile.open() is able to open an empty tar From bdb1ff92d62dbf8d5c927efe631f4c09ca8afd9b Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 2 Jun 2021 19:47:47 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2021-06-02-19-47-46.bpo-44289.xC5kuV.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2021-06-02-19-47-46.bpo-44289.xC5kuV.rst diff --git a/Misc/NEWS.d/next/Library/2021-06-02-19-47-46.bpo-44289.xC5kuV.rst b/Misc/NEWS.d/next/Library/2021-06-02-19-47-46.bpo-44289.xC5kuV.rst new file mode 100644 index 00000000000000..d2feab9bda49b2 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-06-02-19-47-46.bpo-44289.xC5kuV.rst @@ -0,0 +1 @@ +Keep argument file object's current position in tarfile.is_tarifle method. \ No newline at end of file From 7be28192b481afd2fb88bea77d702be198da391c Mon Sep 17 00:00:00 2001 From: Andrzej Mateja Date: Mon, 7 Feb 2022 22:20:29 +0100 Subject: [PATCH 3/4] More descriptive news entry. --- .../next/Library/2021-06-02-19-47-46.bpo-44289.xC5kuV.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2021-06-02-19-47-46.bpo-44289.xC5kuV.rst b/Misc/NEWS.d/next/Library/2021-06-02-19-47-46.bpo-44289.xC5kuV.rst index d2feab9bda49b2..66cca80d509b94 100644 --- a/Misc/NEWS.d/next/Library/2021-06-02-19-47-46.bpo-44289.xC5kuV.rst +++ b/Misc/NEWS.d/next/Library/2021-06-02-19-47-46.bpo-44289.xC5kuV.rst @@ -1 +1 @@ -Keep argument file object's current position in tarfile.is_tarifle method. \ No newline at end of file +Fix an issue with :meth:`~tarfile.is_tarfile` method when using _fileobj_ argument: position in the _fileobj_ was advanced forward which made it unreadable with :meth:`tarfile.TarFile.open`. From 628289eb0abecc5fe46f6854e17e5cefd5a8155e Mon Sep 17 00:00:00 2001 From: Andrzej Mateja Date: Wed, 9 Feb 2022 05:51:38 +0100 Subject: [PATCH 4/4] bpo-44289: Follow the documentation guidelines for parameter names. --- .../next/Library/2021-06-02-19-47-46.bpo-44289.xC5kuV.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2021-06-02-19-47-46.bpo-44289.xC5kuV.rst b/Misc/NEWS.d/next/Library/2021-06-02-19-47-46.bpo-44289.xC5kuV.rst index 66cca80d509b94..164138f47ae3bd 100644 --- a/Misc/NEWS.d/next/Library/2021-06-02-19-47-46.bpo-44289.xC5kuV.rst +++ b/Misc/NEWS.d/next/Library/2021-06-02-19-47-46.bpo-44289.xC5kuV.rst @@ -1 +1 @@ -Fix an issue with :meth:`~tarfile.is_tarfile` method when using _fileobj_ argument: position in the _fileobj_ was advanced forward which made it unreadable with :meth:`tarfile.TarFile.open`. +Fix an issue with :meth:`~tarfile.is_tarfile` method when using *fileobj* argument: position in the *fileobj* was advanced forward which made it unreadable with :meth:`tarfile.TarFile.open`.