8000 gh-117691: Add an appropriate stacklevel for PEP-706 tarfile deprecat… · python/cpython@cff0a2d · GitHub
[go: up one dir, main page]

Skip to content

Commit cff0a2d

Browse files
authored
gh-117691: Add an appropriate stacklevel for PEP-706 tarfile deprecation warnings (GH-117872)
1 parent c520bf9 commit cff0a2d

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

Lib/tarfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2247,7 +2247,7 @@ def _get_filter_function(self, filter):
22472247
'Python 3.14 will, by default, filter extracted tar '
22482248
+ 'archives and reject files or modify their metadata. '
22492249
+ 'Use the filter argument to control this behavior.',
2250-
DeprecationWarning)
2250+
DeprecationWarning, stacklevel=3)
22512251
return fully_trusted_filter
22522252
if isinstance(filter, str):
22532253
raise TypeError(

Lib/test/test_tarfile.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,31 @@ def test_extract_directory(self):
738738
finally:
739739
os_helper.rmtree(DIR)
740740

741+
def test_deprecation_if_no_filter_passed_to_extractall(self):
742+
DIR = pathlib.Path(TEMPDIR) / "extractall"
743+
with (
744+
os_helper.temp_dir(DIR),
745+
tarfile.open(tarname, encoding="iso8859-1") as tar
746+
):
747+
directories = [t for t in tar if t.isdir()]
748+
with self.assertWarnsRegex(DeprecationWarning, "Use the filter argument") as cm:
749+
tar.extractall(DIR, directories)
750+
# check that the stacklevel of the deprecation warning is correct:
751+
self.assertEqual(cm.filename, __file__)
752+
753+
def test_deprecation_if_no_filter_passed_to_extract(self):
754+
dirtype = "ustar/dirtype"
755+
DIR = pathlib.Path(TEMPDIR) / "extractall"
756+
with (
757+
os_helper.temp_dir(DIR),
758+
tarfile.open(tarname, encoding="iso8859-1") as tar
759+
):
760+
tarinfo = tar.getmember(dirtype)
761+
with self.assertWarnsRegex(DeprecationWarning, "Use the filter argument") as cm:
762+
tar.extract(tarinfo, path=DIR)
763+
# check that the stacklevel of the deprecation warning is correct:
764+
self.assertEqual(cm.filename, __file__)
765+
741766
def test_extractall_pathlike_name(self):
742767
DIR = pathlib.Path(TEMPDIR) / "extractall"
743768
with os_helper.temp_dir(DIR), \
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Improve the error messages emitted by :mod:`tarfile` deprecation warnings
2+
relating to PEP 706. If a ``filter`` argument is not provided to
3+
``extract()`` or ``extractall``, the deprecation warning now points to the
4+
line in the user's code where the relevant function was called. Patch by
5+
Alex Waygood.

0 commit comments

Comments
 (0)
0