summaryrefslogtreecommitdiff
path: root/python/rpmfd-py.c
blob: 868c293da5b5187ea03798898d020e680cbe804f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#include "rpmsystem-py.h"
#include "rpmfd-py.h"

int rpmFdFromPyObject(PyObject *obj, FD_t *fdp)
{
    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 0;
    }
    if (fd == NULL || Ferror(fd)) {
	PyErr_SetFromErrno(PyExc_IOError);
	Fclose(fd);
	return 0;
    }
    *fdp = fd;
    return 1;
}