python / python/cpython

`zipfile`: `zipfile.Path`’s `glob()` and `rglob()` are not documented

未关闭
#133,360 11 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

docs
主要语言
Python
星标
77.2k
派生
36k
PR 合并指标
PR 指标待抓取

描述

Documentation

It seems documentation for zipfile.Path’s glob() and rglob() is missing (or maybe they're meant to be private but wrongly "exported"?!

Also, the behaviour of rglob() seems a bit unexpected:

Assume the following test zip file:

1/
1/a
1/b
1/c
1/1/
1/1/loop
1/2/
1/2/a
1/2/b
1/2/c
1/3/
2/
2/f
3/
3/g
.dir/
.dir/a/
.dir/a/1
.dir/.1
.dir/.d
.file

where pathnames ending in / are directories and loop is a symlink to ../../1.

With:

p = zipfile.Path("test.zip")

glob() does not allow an empty pattern:

i = p.glob("")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.13/zipfile/_path/__init__.py", line 419, in glob
    raise ValueError(f"Unacceptable pattern: {pattern!r}")
ValueError: Unacceptable pattern: ''

But rglob() does:

>>> for i in  p.rglob(""):
...     print(i)
test.zip/1/
test.zip/1/1/
test.zip/1/2/
test.zip/1/3/
test.zip/2/
test.zip/3/
test.zip/.dir/
test.zip/.dir/a/

in which case it seems to list only directories.

If using * as pattern:

>>> for i in  p.rglob("*"):
...     print(i)
test.zip/1/a
test.zip/1/b
test.zip/1/c
test.zip/1/1/
test.zip/1/1/loop
test.zip/1/2/
test.zip/1/2/a
test.zip/1/2/b
test.zip/1/2/c
test.zip/1/3/
test.zip/2/f
test.zip/3/g
test.zip/.dir/a/
test.zip/.dir/a/1
test.zip/.dir/.1
test.zip/.dir/.d

the results are also a bit... unexpected...

  • . files are included (which is at least not compatible with POSIX pattern matching notation on pathnames)
  • some but not all directories are given, e.g. we have 1/1/ but not 1/, which we did get when the pattern is the empty string.
  • documentation should also note, that the zip file name is for some reason prepended.

It's also not really clear whether these two functions are safe at all,... e.g. could they be used to break out of traversing the ZIP's contents with some tricky .. (cause at some point the docs warn about Path not doing any such sanitisations).

Cheers,
Chris.

Linked PRs
  • gh-144460

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 zipfile.Path 以及 glob() 和 rglob() 入口点开始,包括显示为 zipfile/_path/init.py 的实现路径。检查已报告的空模式和 '*' 示例,然后查看关联的 PR gh-144460 及讨论。文档所述行为、路径前缀、目录结果、点文件和安全注意事项均已确定并涵盖后,即可视为完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
documentation
Issue 类型
文档
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。