Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[3.13] gh-133890: Handle UnicodeEncodeError in tarfile (GH-134147)#134196

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall 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
4 changes: 2 additions & 2 deletionsLib/tarfile.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2376,7 +2376,7 @@ def _get_extract_tarinfo(self, member, filter_function, path):
unfiltered = tarinfo
try:
tarinfo = filter_function(tarinfo, path)
except (OSError, FilterError) as e:
except (OSError,UnicodeEncodeError,FilterError) as e:
self._handle_fatal_error(e)
except ExtractError as e:
self._handle_nonfatal_error(e)
Expand All@@ -2397,7 +2397,7 @@ def _extract_one(self, tarinfo, path, set_attrs, numeric_owner):
self._extract_member(tarinfo, os.path.join(path, tarinfo.name),
set_attrs=set_attrs,
numeric_owner=numeric_owner)
except OSError as e:
except(OSError, UnicodeEncodeError) as e:
self._handle_fatal_error(e)
except ExtractError as e:
self._handle_nonfatal_error(e)
Expand Down
49 changes: 45 additions & 4 deletionsLib/test/test_tarfile.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3457,11 +3457,12 @@ class ArchiveMaker:
with t.open() as tar:
... # `tar` is now a TarFile with 'filename' in it!
"""
def __init__(self):
def __init__(self, **kwargs):
self.bio = io.BytesIO()
self.tar_kwargs = dict(kwargs)

def __enter__(self):
self.tar_w = tarfile.TarFile(mode='w', fileobj=self.bio)
self.tar_w = tarfile.TarFile(mode='w', fileobj=self.bio, **self.tar_kwargs)
return self

def __exit__(self, *exc):
Expand DownExpand Up@@ -4040,7 +4041,10 @@ def test_tar_filter(self):
# that in the test archive.)
with tarfile.TarFile.open(tarname) as tar:
for tarinfo in tar.getmembers():
filtered = tarfile.tar_filter(tarinfo, '')
try:
filtered = tarfile.tar_filter(tarinfo, '')
except UnicodeEncodeError:
continue
self.assertIs(filtered.name, tarinfo.name)
self.assertIs(filtered.type, tarinfo.type)

Expand All@@ -4051,11 +4055,48 @@ def test_data_filter(self):
for tarinfo in tar.getmembers():
try:
filtered = tarfile.data_filter(tarinfo, '')
except tarfile.FilterError:
except(tarfile.FilterError, UnicodeEncodeError):
continue
self.assertIs(filtered.name, tarinfo.name)
self.assertIs(filtered.type, tarinfo.type)

@unittest.skipIf(sys.platform == 'win32', 'requires native bytes paths')
def test_filter_unencodable(self):
# Sanity check using a valid path.
tarinfo = tarfile.TarInfo(os_helper.TESTFN)
filtered = tarfile.tar_filter(tarinfo, '')
self.assertIs(filtered.name, tarinfo.name)
filtered = tarfile.data_filter(tarinfo, '')
self.assertIs(filtered.name, tarinfo.name)

tarinfo = tarfile.TarInfo('test\x00')
self.assertRaises(ValueError, tarfile.tar_filter, tarinfo, '')
self.assertRaises(ValueError, tarfile.data_filter, tarinfo, '')
tarinfo = tarfile.TarInfo('\ud800')
self.assertRaises(UnicodeEncodeError, tarfile.tar_filter, tarinfo, '')
self.assertRaises(UnicodeEncodeError, tarfile.data_filter, tarinfo, '')

@unittest.skipIf(sys.platform == 'win32', 'requires native bytes paths')
def test_extract_unencodable(self):
# Create a member with name \xed\xa0\x80 which is UTF-8 encoded
# lone surrogate \ud800.
with ArchiveMaker(encoding='ascii', errors='surrogateescape') as arc:
arc.add('\udced\udca0\udc80')
with os_helper.temp_cwd() as tmp:
tar = arc.open(encoding='utf-8', errors='surrogatepass',
errorlevel=1)
self.assertEqual(tar.getnames(), ['\ud800'])
with self.assertRaises(UnicodeEncodeError):
tar.extractall(filter=tarfile.tar_filter)
self.assertEqual(os.listdir(), [])

tar = arc.open(encoding='utf-8', errors='surrogatepass',
errorlevel=0, debug=1)
with support.captured_stderr() as stderr:
tar.extractall(filter=tarfile.tar_filter)
self.assertEqual(os.listdir(), [])
self.assertIn('tarfile: UnicodeEncodeError ', stderr.getvalue())

def test_default_filter_warns(self):
"""Ensure the default filter warns"""
with ArchiveMaker() as arc:
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
The:mod:`tarfile` module now handles:exc:`UnicodeEncodeError` in the same
way as:exc:`OSError` when cannot extract a member.
Loading

[8]ページ先頭

©2009-2025 Movatter.jp