Support 'tree[name]' where name is a text string
This commit is contained in:
parent
500a148398
commit
8137dc84b5
90
pygit2.c
90
pygit2.c
@ -318,6 +318,31 @@ git_oid_to_py_str(const git_oid *oid)
|
||||
return PyUnicode_DecodeASCII(hex, GIT_OID_HEXSZ, "strict");
|
||||
}
|
||||
|
||||
char *
|
||||
py_str_to_c_str(PyObject *value)
|
||||
{
|
||||
char *c_str;
|
||||
|
||||
/* Case 1: byte string */
|
||||
if (PyString_Check(value))
|
||||
return PyString_AsString(value);
|
||||
|
||||
/* Case 2: text string */
|
||||
if (PyUnicode_Check(value)) {
|
||||
value = PyUnicode_AsUTF8String(value);
|
||||
if (value == NULL)
|
||||
return NULL;
|
||||
c_str = PyString_AsString(value);
|
||||
Py_DECREF(value);
|
||||
return c_str;
|
||||
}
|
||||
|
||||
/* Type error */
|
||||
PyErr_Format(PyExc_TypeError, "unexpected %.200s",
|
||||
Py_TYPE(value)->tp_name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int
|
||||
Repository_init(Repository *self, PyObject *args, PyObject *kwds)
|
||||
{
|
||||
@ -1273,7 +1298,7 @@ Tree_contains(Tree *self, PyObject *py_name)
|
||||
{
|
||||
char *name;
|
||||
|
||||
name = PyString_AsString(py_name);
|
||||
name = py_str_to_c_str(py_name);
|
||||
if (name == NULL)
|
||||
return -1;
|
||||
|
||||
@ -1294,21 +1319,6 @@ wrap_tree_entry(const git_tree_entry *entry, Tree *tree)
|
||||
return py_entry;
|
||||
}
|
||||
|
||||
static TreeEntry *
|
||||
Tree_getitem_by_name(Tree *self, PyObject *py_name)
|
||||
{
|
||||
char *name;
|
||||
const git_tree_entry *entry;
|
||||
|
||||
name = PyString_AS_STRING(py_name);
|
||||
entry = git_tree_entry_byname(self->tree, name);
|
||||
if (!entry) {
|
||||
PyErr_SetObject(PyExc_KeyError, py_name);
|
||||
return NULL;
|
||||
}
|
||||
return wrap_tree_entry(entry, self);
|
||||
}
|
||||
|
||||
static int
|
||||
Tree_fix_index(Tree *self, PyObject *py_index)
|
||||
{
|
||||
@ -1375,18 +1385,23 @@ Tree_getitem_by_index(Tree *self, PyObject *py_index)
|
||||
static TreeEntry *
|
||||
Tree_getitem(Tree *self, PyObject *value)
|
||||
{
|
||||
if (PyString_Check(value)) {
|
||||
return Tree_getitem_by_name(self, value);
|
||||
}
|
||||
else if (PyInt_Check(value)) {
|
||||
char *name;
|
||||
const git_tree_entry *entry;
|
||||
|
||||
/* Case 1: integer */
|
||||
if (PyInt_Check(value))
|
||||
return Tree_getitem_by_index(self, value);
|
||||
}
|
||||
else {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"Tree entry index must be int or str, not %.200s",
|
||||
Py_TYPE(value)->tp_name);
|
||||
|
||||
/* Case 2: byte or text string */
|
||||
name = py_str_to_c_str(value);
|
||||
if (name == NULL)
|
||||
return NULL;
|
||||
entry = git_tree_entry_byname(self->tree, name);
|
||||
if (!entry) {
|
||||
PyErr_SetObject(PyExc_KeyError, value);
|
||||
return NULL;
|
||||
}
|
||||
return wrap_tree_entry(entry, self);
|
||||
}
|
||||
|
||||
static PySequenceMethods Tree_as_sequence = {
|
||||
@ -1747,31 +1762,6 @@ Index_write(Index *self)
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
char *
|
||||
py_str_to_c_str(PyObject *value)
|
||||
{
|
||||
char *c_str;
|
||||
|
||||
/* Case 1: byte string */
|
||||
if (PyString_Check(value))
|
||||
return PyString_AsString(value);
|
||||
|
||||
/* Case 2: text string */
|
||||
if (PyUnicode_Check(value)) {
|
||||
value = PyUnicode_AsUTF8String(value);
|
||||
if (value == NULL)
|
||||
return NULL;
|
||||
c_str = PyString_AsString(value);
|
||||
Py_DECREF(value);
|
||||
return c_str;
|
||||
}
|
||||
|
||||
/* Type error */
|
||||
PyErr_Format(PyExc_TypeError, "unexpected %.200s",
|
||||
Py_TYPE(value)->tp_name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* This is an internal function, used by Index_getitem and Index_setitem */
|
||||
static int
|
||||
Index_get_position(Index *self, PyObject *value)
|
||||
|
Loading…
x
Reference in New Issue
Block a user