gh-117691: Add an appropriate stacklevel for PEP-706 tarfile deprecation warnings (GH-117872)

This commit is contained in:
Alex Waygood 2024-04-16 12:36:00 +01:00 committed by GitHub
parent c520bf9bdf
commit cff0a2db00
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 31 additions and 1 deletions

View file

@ -2247,7 +2247,7 @@ class TarFile(object):
'Python 3.14 will, by default, filter extracted tar ' 'Python 3.14 will, by default, filter extracted tar '
+ 'archives and reject files or modify their metadata. ' + 'archives and reject files or modify their metadata. '
+ 'Use the filter argument to control this behavior.', + 'Use the filter argument to control this behavior.',
DeprecationWarning) DeprecationWarning, stacklevel=3)
return fully_trusted_filter return fully_trusted_filter
if isinstance(filter, str): if isinstance(filter, str):
raise TypeError( raise TypeError(

View file

@ -738,6 +738,31 @@ class MiscReadTestBase(CommonReadTest):
finally: finally:
os_helper.rmtree(DIR) 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): def test_extractall_pathlike_name(self):
DIR = pathlib.Path(TEMPDIR) / "extractall" DIR = pathlib.Path(TEMPDIR) / "extractall"
with os_helper.temp_dir(DIR), \ with os_helper.temp_dir(DIR), \

View file

@ -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.