libgit2 / libgit2/pygit2

Index.__getitem__ breaks with bytes on Python 3

Abierto
#931 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Lenguaje dominante
Python
Estrellas
1.7k
Forks
408
Merge medio
2 d 57 min
PR fusionados (30 d)
7

Descripción

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.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza en pygit2/index.py, en Index.getitem, y reproduce el ejemplo de bytes del issue. Rastrea cómo se clasifican las claves bytes y, a continuación, añade cobertura de regresión específica para la búsqueda con bytes; estará terminado cuando las rutas bytes se comparen con el índice sin llegar a la comparación de enteros y las rutas inexistentes conserven el comportamiento de error esperado.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
devtools
Tipo de issue
Error
Dificultad
2/5
Tiempo estimado
1-3 horas
Estado de actividad
Estancado
Claridad
Bien especificado
Aptitud para principiantes
45/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.