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 — Add name and mode attributes to compressed file-like objects · Issue #115961 · python/cpython

Add name and mode attributes to compressed file-like objects #115961

New issue
Copy link
New issue
Copy link
Closed
Closed
Add name and mode attributes to compressed file-like objects#115961
Copy link
Assignees
serhiy-storchaka
Labels
type-featureA feature request or enhancementA feature request or enhancement

Description

@serhiy-storchaka
serhiy-storchaka
opened on Feb 26, 2024
Issue body actions

Feature or enhancement

Regular file objects returned by the open() building have name and name and mode attributes. Some code may use these optional attributes for different purposes, for example to distinguish readable files from writable files, binary files from text files, or just for formatting the repr or error messages. For example:

  • cpython/Lib/tarfile.py

    Line 1666 in 5a83292

    if (name is None and hasattr(fileobj, "name") and
  • cpython/Lib/tarfile.py

    Line 1669 in 5a83292

    if hasattr(fileobj, "mode"):
  • cpython/Lib/socket.py

    Line 455 in 5a83292

    if 'b' not in getattr(file, 'mode', 'b'):
  • cpython/Lib/gzip.py

    Line 201 in 5a83292

    mode = getattr(fileobj, 'mode', 'rb')
  • cpython/Lib/asyncio/base_events.py

    Line 982 in 5a83292

    if 'b' not in getattr(file, 'mode', 'b'):
  • cpython/Lib/wave.py

    Line 654 in 5a83292

    if hasattr(f, 'mode'):

There are several file-like objects in compressing modules gzip, bz2 and lama and archiving modules zipfile and tarfile. They usually implement the raw or buffered file protocols from io, but not always have name and mode attributes, and when they have, they not always have common semantic.

GzipFile, unlike to BZ2File and LZMAFile, has name and mode attributes, but mode is an integer, that confuses tarfile (see #62775).

ZipExtFile has the mode attribute which is always 'r'. It is a legacy from Python 2, when it could also be 'U' or 'rU' for files with universal newlines. But this mode was removed long time ago, and since it is a binary file, its mode should be 'rb'.

See also #68446, #91373, #91374. I opened this issue because consider it all the parts of larger image.

Linked PRs

  • gh-115961: Improve tests for compressed file-like objects #115963
  • [3.12] gh-115961: Improve tests for compressed file-like objects (GH-115963) #116032
  • gh-115961: Add name and mode attributes for compressed file-like objects #116036
  • [3.11] gh-115961: Improve tests for compressed file-like objects (GH-115963) #116039
Reacted by Gregory P. Smith

Activity

  1. serhiy-storchaka
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Feb 26, 2024
  2. serhiy-storchaka
    self-assigned this
    on Feb 26, 2024
  3. serhiy-storchaka
    added this to Compression issues 🗜, Tarfile issues 🗄️and Zipfile issues 📦on Feb 26, 2024
  4. serhiy-storchaka
    added a commit that references this issue on Feb 26, 2024

    pythongh-115961: Improve tests for compressed file-like objects

    f5955ba
  5. serhiy-storchaka commented on Feb 26, 2024

    @serhiy-storchaka
    serhiy-storchaka
    on Feb 26, 2024
    MemberAuthor
    More actions

    The first step is to add tests for existing attributes and methods for file-like objects created in different way, in different modes and states: #115963. It also fixes some bugs in tests.

  6. serhiy-storchaka
    moved this to In Progress in Tarfile issues 🗄️on Feb 26, 2024
  7. serhiy-storchaka
    moved this to In Progress in Compression issues 🗜on Feb 26, 2024
  8. serhiy-storchaka
    moved this to In Progress in Zipfile issues 📦on Feb 26, 2024
  9. serhiy-storchaka
    added a commit that references this issue on Feb 28, 2024

    gh-115961: Improve tests for compressed file-like objects (GH-115963)

    e72576c
  10. miss-islington
    added a commit that references this issue on Feb 28, 2024

    pythongh-115961: Improve tests for compressed file-like objects (pyth…

    86718b5
  11. serhiy-storchaka
    added a commit that references this issue on Feb 28, 2024

    pythongh-115961: Add name and mode attributes for compressed file-lik…

    223fd79
  12. serhiy-storchaka commented on Feb 28, 2024

    @serhiy-storchaka
    serhiy-storchaka
    on Feb 28, 2024
    MemberAuthor
    More actions

    And the second step -- add new attributes and change the existing attributes to match the general rule.

    It includes two minor breaking changes:

    • GzipFile.mode is now 'rb' or 'wb' instead of 1 or 2.
    • ZipExtFile.mode is now 'rb' instead of 'r'.

    These changes look rather as bugfixes (see #62775), but I do not think that it is worth to backport them.

  13. serhiy-storchaka
    added 4 commits that reference this issue on Feb 28, 2024

    pythongh-115961: Add name and mode attributes for compressed file-lik…

    874c8eb

    [3.12] gh-115961: Improve tests for compressed file-like objects (GH-…

    e5e9862

    [3.11] pythongh-115961: Improve tests for compressed file-like objects (

    7f57079

    [3.11] gh-115961: Improve tests for compressed file-like objects (GH-…

    becc615
  14. woodruffw
    added a commit that references this issue on Mar 4, 2024

    pythongh-115961: Improve tests for compressed file-like objects (pyth…

    d280d6e
  15. serhiy-storchaka commented on Mar 13, 2024

    @serhiy-storchaka
    serhiy-storchaka
    on Mar 13, 2024
    MemberAuthor
    More actions

    Open questions:

    • Should name be available for closed streams? Currently it is available for gzip, but for bz2 and lzma it raises a ValueError.
    • Should mode be always "wb" for writable streams, or let it be "ab" or "xb" if the stream was opened with the corresponding mode argument?
  16. adorilson
    added a commit that references this issue on Mar 25, 2024

    pythongh-115961: Improve tests for compressed file-like objects (pyth…

    44286f7
  17. diegorusso
    added a commit that references this issue on Apr 17, 2024

    pythongh-115961: Improve tests for compressed file-like objects (pyth…

    f8da870
  18. serhiy-storchaka
    added a commit that references this issue on Apr 21, 2024

    gh-115961: Add name and mode attributes for compressed file-like obje…

    51ef89c
  19. serhiy-storchaka
    closed this as completedon Apr 21, 2024
  20. github-project-automation
    moved this from In Progress to Done in Zipfile issues 📦on Apr 21, 2024
  21. github-project-automation
    moved this from In Progress to Done in Compression issues 🗜on Apr 21, 2024
  22. github-project-automation
    moved this from In Progress to Done in Tarfile issues 🗄️on Apr 21, 2024
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

type-featureA feature request or enhancementA feature request or enhancement

Projects

  • Zipfile issues 📦
    Status
    Done
    Show more project fields
  • Tarfile issues 🗄️
    Status
    Done
    Show more project fields
  • Compression 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.