Skip to content

zipfile.Path.read_text & .open methods with a positional encoding arg causes a TypeError #101144

Closed
@gpshead

Description

@gpshead

This is a regression from 3.9 behavior seen in Python 3.10.

zipfile.Path.read_text passes *args and **kwargs to it's own open() method, but in 3.10+ is also blindly sets kwargs["encodings"] to a value. So code that was previously passing an encoding as a positional argument now sees:

  File "/<stdlib>/zipfile.py", line 2362, in read_text
    with self.open('r', *args, **kwargs) as strm:
  File "/<stdlib>/zipfile.py", line 2350, in open
    return io.TextIOWrapper(stream, *args, **kwargs)
TypeError: Failed to construct dataset imagenet2012: argument for TextIOWrapper() given by name ('encoding') and position (2)

3.10's Lib/zipfile.py (and main's Lib/zipfile/_path.py) contain:

    def read_text(self, *args, **kwargs):
        kwargs["encoding"] = io.text_encoding(kwargs.get("encoding"))  # <-- new in 3.10, source of bug
        with self.open('r', *args, **kwargs) as strm:
            return strm.read()

As this is a regression, we should avoid setting that value in kwargs when "encodings" not in kwargs to match 3.9 and earlier behavior.

TODO list:

Linked PRs

Metadata

Metadata

Assignees

Labels

3.10only security fixes3.11only security fixestriagedThe issue has been accepted as valid by a triager.type-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions