libgit2 / libgit2/pygit2

Index.__getitem__ breaks with bytes on Python 3

Open
#931 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.7k
Forks
408
Avg merge
2d 57m
Merged PRs (30d)
7

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in pygit2/index.py at Index.getitem and reproduce the bytes example from the issue. Trace how bytes keys are classified, then add focused regression coverage for bytes lookup; done means bytes paths are matched against the index without reaching the integer comparison and missing paths retain the expected error behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.