From 44f38421d7c4f195704a8e28959359355482c776 Mon Sep 17 00:00:00 2001 From: "Luiz Fernando N. Capitulino" Date: Fri, 13 Jul 2007 21:16:49 -0300 Subject: [PATCH] libgit-thin: pygit: Uses python's 2.2 attribute handling Signed-off-by: Luiz Fernando N. Capitulino --- libgit-thin/pygit/pygit.c | 58 ++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/libgit-thin/pygit/pygit.c b/libgit-thin/pygit/pygit.c index 3adf397fa9..53e1b9c614 100644 --- a/libgit-thin/pygit/pygit.c +++ b/libgit-thin/pygit/pygit.c @@ -61,34 +61,17 @@ static PyMethodDef git_repo_methods[] = { {NULL, NULL, 0, NULL} }; -static PyObject * -git_repo_getattr(GitRepoObject *self, char *name) -{ - PyObject *ret; - - ret = Py_FindMethod(git_repo_methods, (PyObject *) self, name); - if (ret) - return ret; - - PyErr_Clear(); - - /* - * Attributes - */ - if (!strncmp(name, "path", 4)) { - Py_INCREF(self->path); - return self->path; - } - - PyErr_SetString(PyExc_AttributeError, name); - return NULL; -} +static PyMemberDef git_repo_members[] = { + {"path", T_OBJECT_EX, offsetof(GitRepoObject, path), 0, + "repository's path"}, + {NULL, 0, 0, 0, NULL} +}; static void git_repo_dealloc(GitRepoObject *self) { Py_XDECREF(self->path); - PyObject_DEL(self); + self->ob_type->tp_free((PyObject*) self); } static PyTypeObject Git_Repo_Type = { @@ -99,7 +82,7 @@ static PyTypeObject Git_Repo_Type = { 0, /* tp_itemsize */ (destructor)git_repo_dealloc, /* tp_dealloc */ 0, /* tp_print */ - (getattrfunc)git_repo_getattr, /* tp_getattr */ + 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_compare */ 0, /* tp_repr */ @@ -117,11 +100,26 @@ static PyTypeObject Git_Repo_Type = { 0, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + git_repo_methods, /* tp_methods */ + git_repo_members, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + 0, /* tp_new */ }; static PyObject * pygit_open(PyObject *self, PyObject *args) { + int err; const char *dir; GitRepoObject *m; @@ -130,7 +128,8 @@ pygit_open(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "s", &dir)) return NULL; - if (git_repo_open(dir)) { + err = git_repo_open(dir); + if (err) { PyErr_SetFromErrno(PyExc_IOError); return NULL; } @@ -139,6 +138,12 @@ pygit_open(PyObject *self, PyObject *args) if (!m) return NULL; + err = PyType_Ready(&Git_Repo_Type); + if (err) { + PyObject_DEL(m); + return NULL; + } + m->path = PyString_FromString(dir); if (!m->path) { PyObject_DEL(m); @@ -156,8 +161,5 @@ static PyMethodDef pygit_methods[] = { PyMODINIT_FUNC initpygit(void) { - /* Patch object type */ - Git_Repo_Type.ob_type = &PyType_Type; - (void) Py_InitModule("pygit", pygit_methods); } -- 2.11.4.GIT