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

Skip to content

Commit aa26dc3

Browse files
[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 cff0a2d) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
1 parent 2a58923 commit aa26dc3

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
@@ -2222,7 +2222,7 @@ def _get_filter_function(self, filter):
22222222
'Python 3.14 will, by default, filter extracted tar '
22232223
+ 'archives and reject files or modify their metadata. '
22242224
+ 'Use the filter argument to control this behavior.',
2225-
DeprecationWarning)
2225+
DeprecationWarning, stacklevel=3)
22262226
return fully_trusted_filter
22272227
if isinstance(filter, str):
22282228
raise TypeError(

Lib/test/test_tarfile.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,31 @@ def test_extract_directory(self):
718718
finally:
719719
os_helper.rmtree(DIR)
720720

721+
def test_deprecation_if_no_filter_passed_to_extractall(self):
722+
DIR = pathlib.Path(TEMPDIR) / "extractall"
723+
with (
724+
os_helper.temp_dir(DIR),
725+
tarfile.open(tarname, encoding="iso8859-1") as tar
726+
):
727+
directories = [t for t in tar if t.isdir()]
728+
with self.assertWarnsRegex(DeprecationWarning, "Use the filter argument") as cm:
729+
tar.extractall(DIR, directories)
730+
# check that the stacklevel of the deprecation warning is correct:
731+
self.assertEqual(cm.filename, __file__)
732+
733+
def test_deprecation_if_no_filter_passed_to_extract(self):
734+
dirtype = "ustar/dirtype"
735+
DIR = pathlib.Path(TEMPDIR) / "extractall"
736+
with (
737+
os_helper.temp_dir(DIR),
738+
tarfile.open(tarname, encoding="iso8859-1") as tar
739+
):
740+
tarinfo = tar.getmember(dirtype)
741+
with self.assertWarnsRegex(DeprecationWarning, "Use the filter argument") as cm:
742+
tar.extract(tarinfo, path=DIR)
743+
# check that the stacklevel of the deprecation warning is correct:
744+
self.assertEqual(cm.filename, __file__)
745+
721746
def test_extractall_pathlike_name(self):
722747
DIR = pathlib.Path(TEMPDIR) / "extractall"
723748
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