libgit2 / libgit2/pygit2

Index.__getitem__ breaks with bytes on Python 3

Aperta
#931 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Python
Stelle
1.7k
Fork
408
Merge medio
2g 57m
PR unite (30g)
7

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia in pygit2/index.py, in Index.getitem, e riproduci l’esempio di bytes dell’issue. Traccia il modo in cui vengono classificate le chiavi bytes, quindi aggiungi una copertura di regressione mirata per la ricerca con bytes; il lavoro è completo quando i percorsi bytes vengono confrontati con l’indice senza arrivare al confronto tra interi e i percorsi mancanti mantengono il comportamento di errore previsto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.