Index.__getitem__ breaks with bytes on Python 3
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 1.7k
- 派生
- 408
- 平均合并
- 2 天 57 分钟
- 30 天内合并 PR
- 7
描述
Index.__getitem__ fails on Python when you pass it bytes.
>>> import pygit2
>>> index = pygit2.Index()
>>> index['foo']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.7/site-packages/pygit2/index.py", line 94, in __getitem__
raise KeyError(key)
KeyError: 'foo'
>>> index[b'foo']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.7/site-packages/pygit2/index.py", line 88, in __getitem__
elif not key >= 0:
TypeError: '>=' not supported between instances of 'bytes' and 'int'
This is because it calls is_string, which on Python 2 checks if it's basestring and on Python 3 checks if it's str. So both text and bytes work on Python 2, but on Python 3, bytes will fall through to elif not key >= 0:, which fails because you can't compare bytes and int like that. Here's an excerpt of the relevant code.
...
if is_string(key):
centry = C.git_index_get_bypath(self._index, to_bytes(key), 0)
elif not key >= 0:
raise ValueError(key)
...
The way I would expect this to work is that if you pass it bytes, it would match directly against the raw bytes of the path in the index. After all, the data in the index file is binary. So passing in bytes should be more explicit than passing in text so it should be allowed.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 pygit2/index.py 中的 Index.getitem 开始,复现 issue 中的 bytes 示例。跟踪 bytes 键是如何分类的,然后为 bytes 查找添加有针对性的回归覆盖;完成的标准是 bytes 路径能够与索引进行匹配而不会进入整数比较,并且缺失路径保留预期的错误行为。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- devtools
- Issue 类型
- 缺陷
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 45/100