Index.__getitem__ breaks with bytes on Python 3
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 1.7k
- Fork
- 408
- Merge trung bình
- 2 ngày 57 phút
- Pull request đã merge (30 ngày)
- 7
Mô tả
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.
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu trong pygit2/index.py tại Index.getitem và tái hiện ví dụ bytes từ issue. Theo dõi cách các khóa bytes được phân loại, sau đó thêm phạm vi kiểm thử hồi quy tập trung cho việc tra cứu bằng bytes; hoàn thành khi các đường dẫn bytes được đối chiếu với index mà không đi đến phép so sánh số nguyên và các đường dẫn không tồn tại vẫn giữ nguyên hành vi lỗi như mong đợi.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- devtools
- Loại issue
- Lỗi
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 45/100