From 945bbf49f52c50b53f0d407e91ec46febd56d948 Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Wed, 30 Sep 2009 10:33:52 +0300 Subject: [PATCH] Sanitize getattr behavior of header objects - when generic getattr fails, only try retrieving header tag as attribute if it's a valid tag - clear any python errors if generic getattr failed - split generating a python object from header + tag to an internal helper --- python/header-py.c | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/python/header-py.c b/python/header-py.c index cdf86c9..af7c1cd 100644 --- a/python/header-py.c +++ b/python/header-py.c @@ -427,28 +427,36 @@ int tagNumFromPyObject (PyObject *item, rpmTag *tagp) return 1; } -static PyObject * hdr_subscript(hdrObject * s, PyObject * item) +static PyObject * hdrGetTag(Header h, rpmTag tag) { - rpmTag tag = RPMTAG_NOT_FOUND; - struct rpmtd_s td; PyObject *res = NULL; + struct rpmtd_s td; - if (!tagNumFromPyObject(item, &tag)) return NULL; - /* rpmtd_AsPyObj() knows how to handle empty containers and all */ - (void) headerGet(s->h, tag, &td, HEADERGET_EXT); + (void) headerGet(h, tag, &td, HEADERGET_EXT); res = rpmtd_AsPyobj(&td); rpmtdFreeData(&td); - return res; } -static PyObject * hdr_getattro(PyObject * o, PyObject * n) +static PyObject * hdr_subscript(hdrObject * s, PyObject * item) +{ + rpmTag tag; + + if (!tagNumFromPyObject(item, &tag)) return NULL; + return hdrGetTag(s->h, tag); +} + +static PyObject * hdr_getattro(hdrObject * s, PyObject * n) { - PyObject * res; - res = PyObject_GenericGetAttr(o, n); - if (res == NULL) - res = hdr_subscript((hdrObject *)o, n); + PyObject *res = PyObject_GenericGetAttr((PyObject *) s, n); + if (res == NULL) { + rpmTag tag; + if (tagNumFromPyObject(n, &tag)) { + PyErr_Clear(); + res = hdrGetTag(s->h, tag); + } + } return res; } -- 2.7.4