Index.__getitem__ breaks with bytes on Python 3
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 1.7k
- Forks
- 408
- Ø Merge
- 2 T. 57 Min.
- Gemergte PRs (30 T.)
- 7
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne in pygit2/index.py bei Index.getitem und reproduziere das bytes-Beispiel aus dem Issue. Verfolge, wie Bytes-Schlüssel klassifiziert werden, und füge anschließend eine gezielte Regressionstestabdeckung für die Suche nach bytes hinzu; fertig ist es, wenn bytes-Pfade gegen den Index abgeglichen werden, ohne den Ganzzahlvergleich zu erreichen, und fehlende Pfade das erwartete Fehlerverhalten beibehalten.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 45/100