From b12a59606860ea2d88cee519ef5a6bb975ee6ecb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20David=20Ib=C3=A1=C3=B1ez?= Date: Thu, 2 May 2013 20:28:14 +0200 Subject: [PATCH] Remove "del index[xxx]" from the API Use "index.remove(xxx)" instead. --- src/index.c | 81 +++++++++++++--------------------------------- test/test_index.py | 7 ++-- 2 files changed, 25 insertions(+), 63 deletions(-) diff --git a/src/index.c b/src/index.c index 67defb74e..9176564f8 100644 --- a/src/index.c +++ b/src/index.c @@ -227,43 +227,6 @@ Index_write(Index *self) Py_RETURN_NONE; } -/* This is an internal function, used by Index_getitem and Index_setitem */ -size_t -Index_get_position(Index *self, PyObject *value) -{ - char *path; - size_t idx; - int err; - - /* Case 1: integer */ - if (PyLong_Check(value)) { - err = (int)PyLong_AsLong(value); - if (err == -1 && PyErr_Occurred()) - return -1; - if (err < 0) { - PyErr_SetObject(PyExc_ValueError, value); - return -1; - } - return err; - } - - /* Case 2: byte or text string */ - path = py_path_to_c_str(value); - if (!path) - return -1; - - err = git_index_find(&idx, self->index, path); - if (err < 0) { - Error_set_str(err, path); - free(path); - return -1; - } - - free(path); - - return idx; -} - int Index_contains(Index *self, PyObject *value) { @@ -322,19 +285,34 @@ wrap_index_entry(const git_index_entry *entry, Index *index) PyObject * Index_getitem(Index *self, PyObject *value) { - size_t idx; + long idx; + char *path; const git_index_entry *index_entry; - idx = Index_get_position(self, value); - if (idx == -1) - return NULL; + /* Case 1: integer */ + if (PyLong_Check(value)) { + idx = PyLong_AsLong(value); + if (idx == -1 && PyErr_Occurred()) + return NULL; + if (idx < 0) { + PyErr_SetObject(PyExc_ValueError, value); + return NULL; + } + index_entry = git_index_get_byindex(self->index, (size_t)idx); + /* Case 2: byte or text string */ + } else { + path = py_path_to_c_str(value); + if (!path) + return NULL; + + index_entry = git_index_get_bypath(self->index, path, 0); + free(path); + } - index_entry = git_index_get_byindex(self->index, idx); if (!index_entry) { PyErr_SetObject(PyExc_KeyError, value); return NULL; } - return wrap_index_entry(index_entry, self); } @@ -362,21 +340,6 @@ Index_remove(Index *self, PyObject *args) Py_RETURN_NONE; } -int -Index_setitem(Index *self, PyObject *key, PyObject *value) -{ - if (value != NULL) { - PyErr_SetString(PyExc_NotImplementedError, - "set item on index not yet implemented"); - return -1; - } - - if (Index_remove(self, Py_BuildValue("(N)", key)) == NULL) - return -1; - - return 0; -} - PyDoc_STRVAR(Index_read_tree__doc__, "read_tree(tree)\n" @@ -453,7 +416,7 @@ PySequenceMethods Index_as_sequence = { PyMappingMethods Index_as_mapping = { (lenfunc)Index_len, /* mp_length */ (binaryfunc)Index_getitem, /* mp_subscript */ - (objobjargproc)Index_setitem, /* mp_ass_subscript */ + NULL, /* mp_ass_subscript */ }; PyDoc_STRVAR(Index__doc__, "Index file."); diff --git a/test/test_index.py b/test/test_index.py index b58137345..39f41b8e8 100644 --- a/test/test_index.py +++ b/test/test_index.py @@ -133,13 +133,12 @@ def test_bare_index(self): self.assertRaises(pygit2.GitError, lambda: index.add('bye.txt')) - def test_del(self): - index = self.repo.index - del index['hello.txt'] - def test_remove(self): index = self.repo.index + self.assertTrue('hello.txt' in index) index.remove('hello.txt') + self.assertFalse('hello.txt' in index) + if __name__ == '__main__': unittest.main()