Add helper to convert python file objects to rpmio FD_t type
authorPanu Matilainen <pmatilai@redhat.com>
Wed, 23 Sep 2009 08:09:19 +0000 (11:09 +0300)
committerPanu Matilainen <pmatilai@redhat.com>
Wed, 23 Sep 2009 08:09:19 +0000 (11:09 +0300)
python/Makefile.am
python/rpmfd-py.c [new file with mode: 0644]
python/rpmfd-py.h [new file with mode: 0644]

index 6f324e4..8d763ed 100644 (file)
@@ -21,6 +21,7 @@ _rpmmodule_la_LIBADD = \
 _rpmmodule_la_SOURCES = rpmmodule.c rpmsystem-py.h \
        header-py.c header-py.h \
        rpmds-py.c rpmds-py.h \
+       rpmfd-py.c rpmfd-py.h \
        rpmfi-py.c rpmfi-py.h \
        rpmmi-py.c rpmmi-py.h \
        rpmps-py.c rpmps-py.h \
diff --git a/python/rpmfd-py.c b/python/rpmfd-py.c
new file mode 100644 (file)
index 0000000..2deb067
--- /dev/null
@@ -0,0 +1,22 @@
+
+#include "rpmsystem-py.h"
+#include "rpmfd-py.h"
+
+FD_t rpmFdFromPyObject(PyObject *obj)
+{
+    FD_t fd = NULL;
+
+    if (PyInt_Check(obj)) {
+       fd = fdDup(PyInt_AsLong(obj));
+    } else if (PyFile_Check(obj)) {
+       FILE *fp = PyFile_AsFile(obj);
+       fd = fdDup(fileno(fp));
+    } else {
+       PyErr_SetString(PyExc_TypeError, "integer or file object expected");
+       return NULL;
+    }
+    if (fd == NULL || Ferror(fd)) {
+       PyErr_SetFromErrno(PyExc_IOError);
+    }
+    return fd;
+}
diff --git a/python/rpmfd-py.h b/python/rpmfd-py.h
new file mode 100644 (file)
index 0000000..2c7a51c
--- /dev/null
@@ -0,0 +1,8 @@
+#ifndef H_RPMFD_PY
+#define H_RPMFD_PY
+
+#include <rpm/rpmio.h>
+
+FD_t rpmFdFromPyObject(PyObject *obj);
+
+#endif