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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
/** \ingroup py_c
* \file python/rpmtd-py.c
*/
#include "rpmsystem-py.h"
#include <rpm/rpmtd.h>
#include <rpm/header.h>
#include "rpmtd-py.h"
#include "header-py.h"
/*
* Convert single tag data item to python object of suitable type
*/
PyObject * rpmtd_ItemAsPyobj(rpmtd td, rpmTagClass tclass)
{
PyObject *res = NULL;
switch (tclass) {
case RPM_STRING_CLASS:
res = utf8FromString(rpmtdGetString(td));
break;
case RPM_NUMERIC_CLASS:
res = PyLong_FromLongLong(rpmtdGetNumber(td));
break;
case RPM_BINARY_CLASS:
res = PyBytes_FromStringAndSize(td->data, td->count);
break;
default:
PyErr_SetString(PyExc_KeyError, "unknown data type");
break;
}
return res;
}
PyObject *rpmtd_AsPyobj(rpmtd td)
{
PyObject *res = NULL;
int array = (rpmTagGetReturnType(td->tag) == RPM_ARRAY_RETURN_TYPE);
rpmTagClass tclass = rpmtdClass(td);
int r;
if (!array && rpmtdCount(td) < 1) {
Py_RETURN_NONE;
}
if (array) {
int ix;
res = PyList_New(rpmtdCount(td));
if (!res) {
return NULL;
}
while ((ix = rpmtdNext(td)) >= 0) {
PyObject *item = rpmtd_ItemAsPyobj(td, tclass);
if (!item) {
Py_DECREF(res);
return NULL;
}
r = PyList_SetItem(res, ix, item);
if (r < 0) {
Py_DECREF(res);
return NULL;
}
}
} else {
res = rpmtd_ItemAsPyobj(td, tclass);
}
return res;
}
|