GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — TarFile filters fail in non-UTF-8 locales · Issue #133890 · python/cpython

TarFile filters fail in non-UTF-8 locales #133890

New issue
Copy link
New issue
Copy link
Closed
Closed
TarFile filters fail in non-UTF-8 locales#133890
Copy link
Assignees
serhiy-storchaka
Labels
3.12only security fixesonly security fixes3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@serhiy-storchaka
serhiy-storchaka
opened on May 11, 2025
Issue body actions

Bug report

test_tarfile files in non-UTF-8 locales. For example:

$ LC_ALL=uk_UA ./python -m test -vuall test_tarfile -m 'NoneInfoExtractTests_*' -m test_data_filter -m test_tar_filter
Details
======================================================================
ERROR: setUpClass (test.test_tarfile.NoneInfoExtractTests_Data)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/serhiy/py/cpython/Lib/test/test_tarfile.py", line 3264, in setUpClass
    tar.extractall(cls.control_dir, filter=cls.extraction_filter)
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/tarfile.py", line 2389, in extractall
    tarinfo = self._get_extract_tarinfo(member, filter_function, path)
  File "/home/serhiy/py/cpython/Lib/tarfile.py", line 2441, in _get_extract_tarinfo
    tarinfo = filter_function(tarinfo, path)
  File "/home/serhiy/py/cpython/Lib/tarfile.py", line 842, in data_filter
    new_attrs = _get_filtered_attrs(member, dest_path, True)
  File "/home/serhiy/py/cpython/Lib/tarfile.py", line 782, in _get_filtered_attrs
    target_path = os.path.realpath(os.path.join(dest_path, name))
  File "/home/serhiy/py/cpython/Lib/posixpath.py", line 405, in realpath
    return _realpath(filename, strict, sep, curdir, pardir, getcwd)
  File "/home/serhiy/py/cpython/Lib/posixpath.py", line 452, in _realpath
    st_mode = lstat(newpath).st_mode
              ~~~~~^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/encodings/koi8_u.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_table)
           ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'charmap' codec can't encode characters in position 112-118: character maps to <undefined>
encoding with 'koi8-u' codec failed

======================================================================
ERROR: setUpClass (test.test_tarfile.NoneInfoExtractTests_Default)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/serhiy/py/cpython/Lib/test/test_tarfile.py", line 3264, in setUpClass
    tar.extractall(cls.control_dir, filter=cls.extraction_filter)
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/tarfile.py", line 2389, in extractall
    tarinfo = self._get_extract_tarinfo(member, filter_function, path)
  File "/home/serhiy/py/cpython/Lib/tarfile.py", line 2441, in _get_extract_tarinfo
    tarinfo = filter_function(tarinfo, path)
  File "/home/serhiy/py/cpython/Lib/tarfile.py", line 842, in data_filter
    new_attrs = _get_filtered_attrs(member, dest_path, True)
  File "/home/serhiy/py/cpython/Lib/tarfile.py", line 782, in _get_filtered_attrs
    target_path = os.path.realpath(os.path.join(dest_path, name))
  File "/home/serhiy/py/cpython/Lib/posixpath.py", line 405, in realpath
    return _realpath(filename, strict, sep, curdir, pardir, getcwd)
  File "/home/serhiy/py/cpython/Lib/posixpath.py", line 452, in _realpath
    st_mode = lstat(newpath).st_mode
              ~~~~~^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/encodings/koi8_u.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_table)
           ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'charmap' codec can't encode characters in position 112-118: character maps to <undefined>
encoding with 'koi8-u' codec failed

======================================================================
ERROR: setUpClass (test.test_tarfile.NoneInfoExtractTests_FullyTrusted)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/serhiy/py/cpython/Lib/test/test_tarfile.py", line 3264, in setUpClass
    tar.extractall(cls.control_dir, filter=cls.extraction_filter)
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/tarfile.py", line 2397, in extractall
    self._extract_one(tarinfo, path, set_attrs=not tarinfo.isdir(),
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                      numeric_owner=numeric_owner)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/tarfile.py", line 2460, in _extract_one
    self._extract_member(tarinfo, os.path.join(path, tarinfo.name),
    ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                         set_attrs=set_attrs,
                         ^^^^^^^^^^^^^^^^^^^^
                         numeric_owner=numeric_owner)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/tarfile.py", line 2543, in _extract_member
    self.makefile(tarinfo, targetpath)
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/tarfile.py", line 2589, in makefile
    with bltn_open(targetpath, "wb") as target:
         ~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/encodings/koi8_u.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_table)
           ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'charmap' codec can't encode characters in position 112-118: character maps to <undefined>
encoding with 'koi8-u' codec failed

======================================================================
ERROR: setUpClass (test.test_tarfile.NoneInfoExtractTests_Tar)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/serhiy/py/cpython/Lib/test/test_tarfile.py", line 3264, in setUpClass
    tar.extractall(cls.control_dir, filter=cls.extraction_filter)
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/tarfile.py", line 2389, in extractall
    tarinfo = self._get_extract_tarinfo(member, filter_function, path)
  File "/home/serhiy/py/cpython/Lib/tarfile.py", line 2441, in _get_extract_tarinfo
    tarinfo = filter_function(tarinfo, path)
  File "/home/serhiy/py/cpython/Lib/tarfile.py", line 836, in tar_filter
    new_attrs = _get_filtered_attrs(member, dest_path, False)
  File "/home/serhiy/py/cpython/Lib/tarfile.py", line 782, in _get_filtered_attrs
    target_path = os.path.realpath(os.path.join(dest_path, name))
  File "/home/serhiy/py/cpython/Lib/posixpath.py", line 405, in realpath
    return _realpath(filename, strict, sep, curdir, pardir, getcwd)
  File "/home/serhiy/py/cpython/Lib/posixpath.py", line 452, in _realpath
    st_mode = lstat(newpath).st_mode
              ~~~~~^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/encodings/koi8_u.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_table)
           ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'charmap' codec can't encode characters in position 112-118: character maps to <undefined>
encoding with 'koi8-u' codec failed

======================================================================
ERROR: test_data_filter (test.test_tarfile.TestExtractionFilters.test_data_filter)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/serhiy/py/cpython/Lib/test/test_tarfile.py", line 4086, in test_data_filter
    filtered = tarfile.data_filter(tarinfo, '')
  File "/home/serhiy/py/cpython/Lib/tarfile.py", line 842, in data_filter
    new_attrs = _get_filtered_attrs(member, dest_path, True)
  File "/home/serhiy/py/cpython/Lib/tarfile.py", line 782, in _get_filtered_attrs
    target_path = os.path.realpath(os.path.join(dest_path, name))
  File "/home/serhiy/py/cpython/Lib/posixpath.py", line 405, in realpath
    return _realpath(filename, strict, sep, curdir, pardir, getcwd)
  File "/home/serhiy/py/cpython/Lib/posixpath.py", line 452, in _realpath
    st_mode = lstat(newpath).st_mode
              ~~~~~^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/encodings/koi8_u.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_table)
           ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'charmap' codec can't encode characters in position 69-75: character maps to <undefined>
encoding with 'koi8-u' codec failed

======================================================================
ERROR: test_tar_filter (test.test_tarfile.TestExtractionFilters.test_tar_filter)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/serhiy/py/cpython/Lib/test/test_tarfile.py", line 4076, in test_tar_filter
    filtered = tarfile.tar_filter(tarinfo, '')
  File "/home/serhiy/py/cpython/Lib/tarfile.py", line 836, in tar_filter
    new_attrs = _get_filtered_attrs(member, dest_path, False)
  File "/home/serhiy/py/cpython/Lib/tarfile.py", line 782, in _get_filtered_attrs
    target_path = os.path.realpath(os.path.join(dest_path, name))
  File "/home/serhiy/py/cpython/Lib/posixpath.py", line 405, in realpath
    return _realpath(filename, strict, sep, curdir, pardir, getcwd)
  File "/home/serhiy/py/cpython/Lib/posixpath.py", line 452, in _realpath
    st_mode = lstat(newpath).st_mode
              ~~~~~^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/encodings/koi8_u.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_table)
           ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'charmap' codec can't encode characters in position 69-75: character maps to <undefined>
encoding with 'koi8-u' codec failed

----------------------------------------------------------------------

This happens because they use os.path.realpath() for paths in a tar archive, which uses os.stat(), which fails with unexpected UnicodeEncodeError if the path in a tar archive can't be encoded in the current filesystem encoding. This error should be handled at some level, either in os.path.realpath() or in tarfile. os.stat() can also raise ValueError if the path contain null bytes. Don't know if this is relevant here, we should test.

Linked PRs

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

Activity

  1. serhiy-storchaka
    self-assigned this
    on May 11, 2025
  2. serhiy-storchaka
    added this to Tarfile issues 🗄️on May 11, 2025
  3. serhiy-storchaka
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    3.12only security fixesonly security fixes
    3.13only security fixesonly security fixes
    3.14bugs and security fixesbugs and security fixes
    on May 11, 2025
  4. serhiy-storchaka
    added a parent issue on May 11, 2025
    • Several tests fail when system encoding is latin-1 #133677
  5. picnixz
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on May 11, 2025
  6. serhiy-storchaka
    added 2 commits that reference this issue on May 17, 2025

    pythongh-133890: Handle UnicodeEncodeError in tarfile

    b74f7aa

    pythongh-133890: Handle UnicodeEncodeError in tarfile

    6772f2d
  7. serhiy-storchaka
    moved this to In Progress in Tarfile issues 🗄️on May 17, 2025
  8. serhiy-storchaka
    added a commit that references this issue on May 18, 2025

    gh-133890: Handle UnicodeEncodeError in tarfile (GH-134147)

    9983c7d
  9. miss-islington
    added a commit that references this issue on May 18, 2025

    pythongh-133890: Handle UnicodeEncodeError in tarfile (pythonGH-134147)

    f2184e3
  10. serhiy-storchaka
    added a commit that references this issue on May 18, 2025

    [3.13] pythongh-133890: Handle UnicodeEncodeError in tarfile (pythonG…

    5d07c55
  11. serhiy-storchaka
    closed this as completedon May 18, 2025
  12. github-project-automation
    moved this from In Progress to Done in Tarfile issues 🗄️on May 18, 2025
  13. serhiy-storchaka
    added 2 commits that reference this issue on May 19, 2025

    [3.13] gh-133890: Handle UnicodeEncodeError in tarfile (GH-134147) (G…

    06a8c06

    [3.14] gh-133890: Handle UnicodeEncodeError in tarfile (GH-134147) (G…

    c23bdc2
  14. Pranjal095
    added a commit that references this issue on Jul 12, 2025

    pythongh-133890: Handle UnicodeEncodeError in tarfile (pythonGH-134147)

    3d18b76
  15. taegyunkim
    added a commit that references this issue on Aug 4, 2025

    pythongh-133890: Handle UnicodeEncodeError in tarfile (pythonGH-134147)

    a425077
  16. mcepl
    added a commit that references this issue on Sep 19, 2025

    pythongh-133890: Handle UnicodeEncodeError in tarfile (pythonGH-134147)

    8f56b7d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @serhiy-storchaka
    serhiy-storchaka

Labels

3.12only security fixesonly security fixes3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Projects

  • Tarfile issues 🗄️
    Status
    Done
    Show more project fields

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.