From 7ff6f6efb7f1a0854e0666eb548e7f1782e8a67a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Tue, 7 Jul 2015 14:06:35 +0200 Subject: [PATCH] Add repr output for TreeEntry When iterating over a tree, its entries show up as objects with an address, which makes it hard to distinguish. Add a method to handle repr and make it easier to play around with them in the console. --- src/tree.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/tree.c b/src/tree.c index e51e1397c..535b1f31d 100644 --- a/src/tree.c +++ b/src/tree.c @@ -154,6 +154,16 @@ TreeEntry_hex__get__(TreeEntry *self) return git_oid_to_py_str(git_tree_entry_id(self->entry)); } +PyObject * +TreeEntry_repr(TreeEntry *self) +{ + char str[GIT_OID_HEXSZ + 1] = { 0 }; + const char *typename; + + typename = git_object_type2string(git_tree_entry_type(self->entry)); + git_oid_fmt(str, git_tree_entry_id(self->entry)); + return PyString_FromFormat("pygit2.TreeEntry('%s', %s, %s)", git_tree_entry_name(self->entry), typename, str); +} PyGetSetDef TreeEntry_getseters[] = { GETTER(TreeEntry, filemode), @@ -176,7 +186,7 @@ PyTypeObject TreeEntryType = { 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_compare */ - 0, /* tp_repr */ + (reprfunc)TreeEntry_repr, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ 0, /* tp_as_mapping */