Implement rpm.readHeaderFromFD() in python instead of C
authorPanu Matilainen <pmatilai@redhat.com>
Mon, 28 Sep 2009 13:19:22 +0000 (16:19 +0300)
committerPanu Matilainen <pmatilai@redhat.com>
Mon, 28 Sep 2009 13:19:22 +0000 (16:19 +0300)
python/header-py.c
python/rpm/__init__.py
python/rpmmodule.c

index 8467cfd..cdf86c9 100644 (file)
@@ -617,50 +617,6 @@ rpmMergeHeadersFromFD(PyObject * self, PyObject * args, PyObject * kwds)
     Py_RETURN_NONE;
 }
 
-PyObject *
-rpmSingleHeaderFromFD(PyObject * self, PyObject * args, PyObject * kwds)
-{
-    FD_t fd;
-    int fileno;
-    off_t offset;
-    PyObject * tuple;
-    Header h;
-    char * kwlist[] = {"fd", NULL};
-
-    if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &fileno))
-       return NULL;
-
-    offset = lseek(fileno, 0, SEEK_CUR);
-
-    fd = fdDup(fileno);
-
-    if (!fd) {
-       PyErr_SetFromErrno(pyrpmError);
-       return NULL;
-    }
-
-    Py_BEGIN_ALLOW_THREADS
-    h = headerRead(fd, HEADER_MAGIC_YES);
-    Py_END_ALLOW_THREADS
-
-    Fclose(fd);
-
-    tuple = PyTuple_New(2);
-
-    if (h && tuple) {
-       PyTuple_SET_ITEM(tuple, 0, hdr_Wrap(&hdr_Type, h));
-       PyTuple_SET_ITEM(tuple, 1, PyLong_FromLong(offset));
-       h = headerFree(h);
-    } else {
-       Py_INCREF(Py_None);
-       Py_INCREF(Py_None);
-       PyTuple_SET_ITEM(tuple, 0, Py_None);
-       PyTuple_SET_ITEM(tuple, 1, Py_None);
-    }
-
-    return tuple;
-}
-
 PyObject * versionCompare (PyObject * self, PyObject * args, PyObject * kwds)
 {
     hdrObject * h1, * h2;
index f59dffa..ca501c6 100644 (file)
@@ -5,6 +5,7 @@ This module enables you to manipulate rpms and the rpm database.
 """
 
 import warnings
+import os
 from _rpm import *
 
 import _rpm
@@ -17,12 +18,14 @@ def headerLoad(*args, **kwds):
     warnings.warn("Use rpm.hdr() instead.", DeprecationWarning, stacklevel=2)
     return hdr(*args, **kwds)
 
-def readHeaderListFromFD(fd, retrofit = True):
+def _fdno(fd):
     if hasattr(fd, "fileno"):
-        fdno = fd.fileno()
+        return fd.fileno()
     else:
-        fdno = fd
+        return fd
 
+def readHeaderListFromFD(fd, retrofit = True):
+    fdno = _fdno(fd)
     hlist = []
     while 1:
         try:
@@ -41,3 +44,13 @@ def readHeaderListFromFile(path):
     f.close()
     return hlist
     
+def readHeaderFromFD(fd):
+    fdno = _fdno(fd)
+    offset = os.lseek(fdno, 0, os.SEEK_CUR)
+    try:
+        h = hdr(fdno)
+    except _rpm.error:
+        h = None
+        offset = None
+
+    return (h, offset)
index 63e2e65..ce3d848 100644 (file)
@@ -156,8 +156,6 @@ static PyMethodDef rpmModuleMethods[] = {
 
     { "mergeHeaderListFromFD", (PyCFunction) rpmMergeHeadersFromFD, METH_VARARGS|METH_KEYWORDS,
        NULL },
-    { "readHeaderFromFD", (PyCFunction) rpmSingleHeaderFromFD, METH_VARARGS|METH_KEYWORDS,
-       NULL },
 
     { "setLogFile", (PyCFunction) setLogFile, METH_VARARGS|METH_KEYWORDS,
        NULL },