Repository navigation
str(zipfile.Path()) not working when root attribute is io.BytesIO #100609
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Dec 29, 2022 This is mentioned in the
zipfile.Pathdocstring (though the exception type is wrong):Lines 216 to 226 in 79c10b7
At the root, ``name``, ``filename``, and ``parent`` resolve to the zipfile. Note these attributes are not valid and will raise a ``ValueError`` if the zipfile has no filename. >>> root.name 'abcde.zip' >>> str(root.filename).replace(os.sep, posixpath.sep) 'mem/abcde.zip' >>> str(root.parent) 'mem' This feature feels fine within the third-party zipp package -- where
zipfile.Pathoriginates -- but is perhaps overwrought within a CPython context. IMO it would suffice to make the originalZipFileobject available from path objects via an attribute likemy_zip_path.zipfileand leave it there.We may have an opportunity to revise this when we introduce a fully pathlib-compatible implementation of
ZipPath. More here: https://discuss.python.org/t/make-pathlib-extensible/3428but is perhaps overwrought within a CPython context
I'm not sure I understand the distinction.
zippmeans to provide the same behavior (just earlier, to older Pythons), so the two implementations should be kept in sync.IMO it would suffice to make the original
ZipFileavailable...And it does currently through
.root, which has its own issues (the name conflicts with the meaning ofpathlib.Path.root).I don't like the implementation of
Path.filename. I'm tempted to deprecate it altogether.__str__provides something similar but different..name,.parentand newly.suffix,.suffixes, and.stemall are ill-defined when the zipfile has no filename, so I believe an exception is appropriate in those cases.But you're right, it's not great to not have a good
__str__representation if the zipfile has no name. I'll work on it.Sorry for not being clear. It would suffice to make the original
ZipFileavailable via an attribute. The behaviour offilename,name, andparentsat the root is slightly too surprising for a standard library module IMO.- addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on Nov 24, 2023 In jaraco/zipp#107, I determined that it's not going to be straightforward to support these attributes, and the best recommendation is for users to provide a filename on the ZipFile object.
At this point, I'm thinking the best option might be to document these limitations and recommendations.
- marked
zipfile.Pathhas only partial compatibilityio.BytesIO#130120 as a duplicate of this issueon Feb 27, 2025 In #130120 and the linked PR, @yngvem is exploring this issue. As you can see above, I'd previously concluded:
Users affected by this issue should supply a filename for their zipfile, even if it's just "".
And
the best option might be to document these limitations and recommendations.
@yngvem - what do you think of the recommendation for the caller to set the zipfile
.filename? Would that work for your use-case?What do you think about the following approach:
- Document that unnamed zip files will raise TypeErrors when rendered using
strand that callers should pass zipfiles with string filenames if they expect forstrto work on thePath. - Update the tests to capture this expectation (that
strraises a TypeError when the zipfile has no filename and uses the filename even on a memory buffered zipfile). - Maybe also capture the expected
reprwhile we're at it.
- Document that unnamed zip files will raise TypeErrors when rendered using
@jaraco, I think that approach seems very reasonable! Though, maybe it would be nice if the TypeError is raised explicitly in
Path.__str__,Path.filenameandPath._baseso it can contain short text explaining whyfilenameisNone?I would be happy to provide all those changes :)
Are there other parts of the stdlib where
__str__()raises? That in particular seems weird to me and probably worth fixing somehow. I'm less bothered byfilename,nameandparent- I think documenting the workaround would be fine there.@barneygale, I also think it seems weird for
__str__to fail, but there are no obvious string representation for unnamed zipfiles (see #130381).I updated #130381 which makes
__str__work (but parent and name still raises errors) based the review comments. I'll make a PR where it has a better exception message as well so you can chose whichever implementation you prefer :)I like the idea of using
:memory:personally!:fileobj:might be another option, given the file object might not be backed by memory :)Reacted by Yngve Mardal MoeA similar-ish case in pathlib comes to mind -
pathlib.Path.is_mount()used to unconditionally raiseNotImplementedErroron Windows. In theory someone could have been relying on that behaviour, but we added Windows support in 3.12 nonetheless.Reacted by Yngve Mardal Moe- changed the title
[-]zipfile.Path.filename not working when root attribute is io.BytesIO[/-][+]str(zipfile.Path()) not working when root attribute is io.BytesIO[/+]on Mar 28, 2025 I've logged this upstream issue: jaraco/zipp#134
Reacted by Yngve Mardal MoeAnd an upstream PR: jaraco/zipp#135
- marked ZipFile.Path print failed when built zip file is based on a bytes stream #109200 as a duplicate of this issue
on May 5, 2025
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsNo status
Using
zipfile.Pathwith a ZIP-file "in memory" asio.BytesIOinstance. In that case attributes like.filename()or.__str__()do not work because they try to instanciate apathlib.Path()with aio.BytesIOobject.I can confirm this with Python 3.11 (run in Docker), Python 3.10.2 (Windows) and Python 3.9.2 (Debian stable). See output below for detailed version infos.
Code to reproduce
Output: Python 3.11 on Linux/Docker
Output: Python 3.10 on Windows
Output: Python 3.9.2 on Debian stable (11)