libgit2 / libgit2/pygit2

Index.__getitem__ breaks with bytes on Python 3

オープン
#931 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
Python
スター
1.7k
フォーク
408
平均マージ
2日 57分
マージ済み PR(30日)
7

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

pygit2/index.py の Index.getitem から始め、issue の bytes の例を再現します。bytes キーがどのように分類されるかを追跡し、その後 bytes の検索に対する焦点を絞った回帰カバレッジを追加します。bytes パスが整数比較に到達せずにインデックスと照合され、存在しないパスが期待されるエラー動作を維持すれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
devtools
issue の種類
バグ
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
45/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。