From cfc341170cdd5b324a3b90eb1b13620fd39298b4 Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Wed, 23 Sep 2009 08:51:12 +0300 Subject: [PATCH] Add support for creating new header objects - without arguments, rpm.hdr() creates an empty header - with another header as argument, make a copy - string argument is considered an unloaded header blob and copy-loaded --- python/header-py.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/python/header-py.c b/python/header-py.c index e8b7bd9..98d76d3 100644 --- a/python/header-py.c +++ b/python/header-py.c @@ -301,6 +301,35 @@ static struct PyMethodDef hdr_methods[] = { {NULL, NULL} /* sentinel */ }; +static PyObject *hdr_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds) +{ + PyObject *obj = NULL; + Header h = NULL; + char *kwlist[] = { "obj", NULL }; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, &obj)) { + return NULL; + } + + if (obj == NULL) { + h = headerNew(); + } else if (hdrObject_Check(obj)) { + h = headerCopy(hdrGetHeader((hdrObject*) obj)); + } else if (PyString_Check(obj)) { + h = headerCopyLoad(PyString_AsString(obj)); + } else { + PyErr_SetNone(PyExc_TypeError); + return NULL; + } + + if (h == NULL) { + PyErr_SetString(pyrpmError, "bad header"); + return NULL; + } + + return hdr_Wrap(h); +} + static void hdr_dealloc(hdrObject * s) { if (s->h) headerFree(s->h); @@ -504,7 +533,7 @@ PyTypeObject hdr_Type = { 0, /* tp_dictoffset */ 0, /* tp_init */ 0, /* tp_alloc */ - 0, /* tp_new */ + hdr_new, /* tp_new */ 0, /* tp_free */ 0, /* tp_is_gc */ }; -- 2.7.4