From: Panu Matilainen Date: Wed, 23 Sep 2009 09:05:02 +0000 (+0300) Subject: Turn rpmFdFromPyObject() into an object converter interface X-Git-Tag: rpm-4.8.0-beta1~199 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ac219c129d04952fbd205c536533723d582d1ad2;p=platform%2Fupstream%2Frpm.git Turn rpmFdFromPyObject() into an object converter interface - permits direct validation and conversion from arg parsing --- diff --git a/python/header-py.c b/python/header-py.c index ec6a325..bf734d1 100644 --- a/python/header-py.c +++ b/python/header-py.c @@ -298,17 +298,15 @@ static PyObject *hdrConvert(hdrObject *self, PyObject *args, PyObject *kwds) static PyObject * hdrWrite(hdrObject *s, PyObject *args, PyObject *kwds) { - PyObject *fo = NULL; char *kwlist[] = { "file", "magic", NULL }; int magic = 1; FD_t fd = NULL; int rc; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i", kwlist, &fo, &magic)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|i", kwlist, + rpmFdFromPyObject, &fd, &magic)) return NULL; - if ((fd = rpmFdFromPyObject(fo)) == NULL) return NULL; - Py_BEGIN_ALLOW_THREADS; rc = headerWrite(fd, s->h, magic ? HEADER_MAGIC_YES : HEADER_MAGIC_NO); Py_END_ALLOW_THREADS; @@ -388,7 +386,7 @@ static PyObject *hdr_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds) h = headerCopy(hdrGetHeader((hdrObject*) obj)); } else if (PyString_Check(obj)) { h = headerCopyLoad(PyString_AsString(obj)); - } else if ((fd = rpmFdFromPyObject(obj)) != NULL) { + } else if (rpmFdFromPyObject(obj, &fd)) { Py_BEGIN_ALLOW_THREADS; h = headerRead(fd, HEADER_MAGIC_YES); Fclose(fd); @@ -673,15 +671,13 @@ PyObject * rpmReadHeaders (FD_t fd) PyObject * rpmHeaderFromFD(PyObject * self, PyObject * args, PyObject * kwds) { FD_t fd; - PyObject *fo; PyObject * list; char * kwlist[] = {"fd", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist, &fo)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&", kwlist, + rpmFdFromPyObject, &fd)) return NULL; - if ((fd = rpmFdFromPyObject(fo)) == NULL) return NULL; - list = rpmReadHeaders (fd); Fclose(fd); diff --git a/python/rpmfd-py.c b/python/rpmfd-py.c index 2deb067..868c293 100644 --- a/python/rpmfd-py.c +++ b/python/rpmfd-py.c @@ -2,7 +2,7 @@ #include "rpmsystem-py.h" #include "rpmfd-py.h" -FD_t rpmFdFromPyObject(PyObject *obj) +int rpmFdFromPyObject(PyObject *obj, FD_t *fdp) { FD_t fd = NULL; @@ -13,10 +13,13 @@ FD_t rpmFdFromPyObject(PyObject *obj) fd = fdDup(fileno(fp)); } else { PyErr_SetString(PyExc_TypeError, "integer or file object expected"); - return NULL; + return 0; } if (fd == NULL || Ferror(fd)) { PyErr_SetFromErrno(PyExc_IOError); + Fclose(fd); + return 0; } - return fd; + *fdp = fd; + return 1; } diff --git a/python/rpmfd-py.h b/python/rpmfd-py.h index 2c7a51c..2063fbf 100644 --- a/python/rpmfd-py.h +++ b/python/rpmfd-py.h @@ -3,6 +3,6 @@ #include -FD_t rpmFdFromPyObject(PyObject *obj); +int rpmFdFromPyObject(PyObject *obj, FD_t *fdp); #endif diff --git a/python/rpmts-py.c b/python/rpmts-py.c index 7824ba8..4f163c4 100644 --- a/python/rpmts-py.c +++ b/python/rpmts-py.c @@ -455,16 +455,15 @@ static PyObject * rpmts_HdrFromFdno(rpmtsObject * s, PyObject * args, PyObject * kwds) { PyObject * result = NULL; - PyObject * fo = NULL; Header h; FD_t fd; rpmRC rpmrc; char * kwlist[] = {"fd", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:HdrFromFdno", kwlist, &fo)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&:HdrFromFdno", kwlist, + rpmFdFromPyObject, &fd)) return NULL; - if ((fd = rpmFdFromPyObject(fo)) == NULL) return NULL; rpmrc = rpmReadPackageFile(s->ts, fd, "rpmts_HdrFromFdno", &h); Fclose(fd);