From 07710f3033b277ccb46decd84f994e88d5e6869a Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Mon, 28 Sep 2009 16:07:09 +0300 Subject: [PATCH] Implement rpm.readHeaderListFromFD() in python instead of C --- python/header-py.c | 52 -------------------------------------------------- python/header-py.h | 3 --- python/rpm/__init__.py | 18 +++++++++++++++++ python/rpmmodule.c | 2 -- 4 files changed, 18 insertions(+), 57 deletions(-) diff --git a/python/header-py.c b/python/header-py.c index 755252e..8467cfd 100644 --- a/python/header-py.c +++ b/python/header-py.c @@ -524,58 +524,6 @@ Header hdrGetHeader(hdrObject * s) return s->h; } -PyObject * rpmReadHeaders (FD_t fd) -{ - PyObject * list; - Header h; - PyObject * hdr; - - if (!fd) { - PyErr_SetFromErrno(pyrpmError); - return NULL; - } - - list = PyList_New(0); - Py_BEGIN_ALLOW_THREADS - h = headerRead(fd, HEADER_MAGIC_YES); - Py_END_ALLOW_THREADS - - while (h) { - headerConvert(h, HEADERCONV_RETROFIT_V3); - hdr = hdr_Wrap(&hdr_Type, h); - if (PyList_Append(list, (PyObject *) hdr)) { - Py_DECREF(list); - Py_DECREF(hdr); - return NULL; - } - Py_DECREF(hdr); - - h = headerFree(h); /* XXX ref held by hdr */ - - Py_BEGIN_ALLOW_THREADS - h = headerRead(fd, HEADER_MAGIC_YES); - Py_END_ALLOW_THREADS - } - - return list; -} - -PyObject * rpmHeaderFromFD(PyObject * self, PyObject * args, PyObject * kwds) -{ - FD_t fd; - PyObject * list; - char * kwlist[] = {"fd", NULL}; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&", kwlist, - rpmFdFromPyObject, &fd)) - return NULL; - - list = rpmReadHeaders (fd); - Fclose(fd); - - return list; -} - /** * This assumes the order of list matches the order of the new headers, and * throws an exception if that isn't true. diff --git a/python/header-py.h b/python/header-py.h index 4c3d8ab..faebc48 100644 --- a/python/header-py.h +++ b/python/header-py.h @@ -26,10 +26,7 @@ PyObject * labelCompare (PyObject * self, PyObject * args); PyObject * versionCompare (PyObject * self, PyObject * args, PyObject * kwds); PyObject * rpmMergeHeadersFromFD(PyObject * self, PyObject * args, PyObject * kwds); int rpmMergeHeaders(PyObject * list, FD_t fd, int matchTag); -PyObject * rpmHeaderFromFile(PyObject * self, PyObject * args, PyObject * kwds); -PyObject * rpmHeaderFromFD(PyObject * self, PyObject * args, PyObject * kwds); PyObject * rpmSingleHeaderFromFD(PyObject * self, PyObject * args, PyObject * kwds); -PyObject * rpmReadHeaders (FD_t fd); PyObject * hdrLoad(PyObject * self, PyObject * args, PyObject * kwds); #endif diff --git a/python/rpm/__init__.py b/python/rpm/__init__.py index bbce236..f59dffa 100644 --- a/python/rpm/__init__.py +++ b/python/rpm/__init__.py @@ -17,6 +17,24 @@ def headerLoad(*args, **kwds): warnings.warn("Use rpm.hdr() instead.", DeprecationWarning, stacklevel=2) return hdr(*args, **kwds) +def readHeaderListFromFD(fd, retrofit = True): + if hasattr(fd, "fileno"): + fdno = fd.fileno() + else: + fdno = fd + + hlist = [] + while 1: + try: + h = hdr(fdno) + if retrofit: + h.convert(HEADERCONV_RETROFIT_V3) + hlist.append(h) + except _rpm.error: + break + + return hlist + def readHeaderListFromFile(path): f = open(path) hlist = readHeaderListFromFD(f) diff --git a/python/rpmmodule.c b/python/rpmmodule.c index 4328fd4..63e2e65 100644 --- a/python/rpmmodule.c +++ b/python/rpmmodule.c @@ -156,8 +156,6 @@ static PyMethodDef rpmModuleMethods[] = { { "mergeHeaderListFromFD", (PyCFunction) rpmMergeHeadersFromFD, METH_VARARGS|METH_KEYWORDS, NULL }, - { "readHeaderListFromFD", (PyCFunction) rpmHeaderFromFD, METH_VARARGS|METH_KEYWORDS, - NULL }, { "readHeaderFromFD", (PyCFunction) rpmSingleHeaderFromFD, METH_VARARGS|METH_KEYWORDS, NULL }, -- 2.7.4