diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2024-04-16 11:54:01 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-16 11:54:01 (GMT) |
commit | aa26dc3c4aa148d28eb7ea910642a6d012cd8921 (patch) | |
tree | 354ae24ca1186acf664547668fa4194bbf50cbb1 | |
parent | 2a58923687cbe102550b275ccf025a1b8d2b417e (diff) | |
download | cpython-aa26dc3c4aa148d28eb7ea910642a6d012cd8921.zip cpython-aa26dc3c4aa148d28eb7ea910642a6d012cd8921.tar.gz cpython-aa26dc3c4aa148d28eb7ea910642a6d012cd8921.tar.bz2 |
[3.12] gh-117691: Add an appropriate stacklevel for PEP-706 tarfile deprecation warnings (GH-117872) (GH-117930)
gh-117691: Add an appropriate stacklevel for PEP-706 tarfile deprecation warnings (GH-117872)
(cherry picked from commit cff0a2db00b6379f60fe273a9782f71773d0a4cb)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
-rwxr-xr-x | Lib/tarfile.py | 2 | ||||
-rw-r--r-- | Lib/test/test_tarfile.py | 25 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2024-04-14-15-59-28.gh-issue-117691.1mtREE.rst | 5 |
3 files changed, 31 insertions, 1 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py index 3bbbcaa..e1487e3 100755 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -2222,7 +2222,7 @@ class TarFile(object): 'Python 3.14 will, by default, filter extracted tar ' + 'archives and reject files or modify their metadata. ' + 'Use the filter argument to control this behavior.', - DeprecationWarning) + DeprecationWarning, stacklevel=3) return fully_trusted_filter if isinstance(filter, str): raise TypeError( diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index 9aa1726..414843e 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -718,6 +718,31 @@ class MiscReadTestBase(CommonReadTest): finally: os_helper.rmtree(DIR) + def test_deprecation_if_no_filter_passed_to_extractall(self): + DIR = pathlib.Path(TEMPDIR) / "extractall" + with ( + os_helper.temp_dir(DIR), + tarfile.open(tarname, encoding="iso8859-1") as tar + ): + directories = [t for t in tar if t.isdir()] + with self.assertWarnsRegex(DeprecationWarning, "Use the filter argument") as cm: + tar.extractall(DIR, directories) + # check that the stacklevel of the deprecation warning is correct: + self.assertEqual(cm.filename, __file__) + + def test_deprecation_if_no_filter_passed_to_extract(self): + dirtype = "ustar/dirtype" + DIR = pathlib.Path(TEMPDIR) / "extractall" + with ( + os_helper.temp_dir(DIR), + tarfile.open(tarname, encoding="iso8859-1") as tar + ): + tarinfo = tar.getmember(dirtype) + with self.assertWarnsRegex(DeprecationWarning, "Use the filter argument") as cm: + tar.extract(tarinfo, path=DIR) + # check that the stacklevel of the deprecation warning is correct: + self.assertEqual(cm.filename, __file__) + def test_extractall_pathlike_name(self): DIR = pathlib.Path(TEMPDIR) / "extractall" with os_helper.temp_dir(DIR), \ diff --git a/Misc/NEWS.d/next/Library/2024-04-14-15-59-28.gh-issue-117691.1mtREE.rst b/Misc/NEWS.d/next/Library/2024-04-14-15-59-28.gh-issue-117691.1mtREE.rst new file mode 100644 index 0000000..d90817a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-04-14-15-59-28.gh-issue-117691.1mtREE.rst @@ -0,0 +1,5 @@ +Improve the error messages emitted by :mod:`tarfile` deprecation warnings +relating to PEP 706. If a ``filter`` argument is not provided to +``extract()`` or ``extractall``, the deprecation warning now points to the +line in the user's code where the relevant function was called. Patch by +Alex Waygood. |