Skip to content

gh-118875: Update tarfile.extractall and friends to use filter="data" by default #118940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ New Modules
Improved Modules
================

tarfile
-------

* The default value of parameter *filter* of
:meth:`tarfile.TarFile.extract` and :meth:`tarfile.TarFile.extractall`
is now ``'data'`` to filter extracted tar archives and reject files or
modify their metadata by default.
The previous default was deprecated since Python 3.12.

shutil
------

* The default value of parameter *filter* of :func:`shutil.unpack_archive`
is now ``'data'`` to filter extracted tar archives and reject files or
modify their metadata by default.
The previous default was deprecated since Python 3.12.

Optimizations
=============
Expand Down
8 changes: 1 addition & 7 deletions Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2249,13 +2249,7 @@ def _get_filter_function(self, filter):
if filter is None:
filter = self.extraction_filter
if filter is None:
import warnings
warnings.warn(
'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, stacklevel=3)
return fully_trusted_filter
return data_filter
if isinstance(filter, str):
raise TypeError(
'String names are not supported for '
Expand Down
38 changes: 1 addition & 37 deletions Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,30 +736,6 @@ def test_extract_directory(self):
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"
Expand Down Expand Up @@ -3177,11 +3153,7 @@ def setUpClass(cls):
tar = tarfile.open(tarname, mode='r', encoding="iso8859-1")
cls.control_dir = pathlib.Path(TEMPDIR) / "extractall_ctrl"
tar.errorlevel = 0
with ExitStack() as cm:
if cls.extraction_filter is None:
cm.enter_context(warnings.catch_warnings(
action="ignore", category=DeprecationWarning))
tar.extractall(cls.control_dir, filter=cls.extraction_filter)
tar.extractall(cls.control_dir, filter=cls.extraction_filter)
tar.close()
cls.control_paths = set(
p.relative_to(cls.control_dir)
Expand Down Expand Up @@ -4009,14 +3981,6 @@ def test_data_filter(self):
self.assertIs(filtered.name, tarinfo.name)
self.assertIs(filtered.type, tarinfo.type)

def test_default_filter_warns(self):
"""Ensure the default filter warns"""
with ArchiveMaker() as arc:
arc.add('foo')
with warnings_helper.check_warnings(
('Python 3.14', DeprecationWarning)):
with self.check_context(arc.open(), None):
self.expect_file('foo')

def test_change_default_filter_on_instance(self):
tar = tarfile.TarFile(tarname, 'r')
Expand Down
Loading