python: return NULL if set_list_of_pygobject fails (#44287)

This commit is contained in:
Benjamin Dauvergne 2020-06-20 08:33:35 +02:00
parent c844abd8e4
commit 4cf6bd4ff7
3 changed files with 20 additions and 6 deletions

View File

@ -825,7 +825,7 @@ register_constants(PyObject *d)
elif is_xml_node(el_type):
print_(' RETURN_IF_FAIL(set_list_of_xml_nodes(&this->%s, cvt_value));' % name, file=fd)
elif is_object(el_type):
print_(' set_list_of_pygobject(&this->%s, cvt_value);' % name, file=fd)
print_(' RETURN_IF_FAIL(set_list_of_pygobject(&this->%s, cvt_value));' % name, file=fd)
else:
raise Exception('Unsupported setter for %s' % (m,))
elif is_hashtable(m):
@ -988,7 +988,7 @@ register_constants(PyObject *d)
elif is_xml_node(qualifier):
print_(' EXIT_IF_FAIL(set_list_of_xml_nodes(&%s, cvt_%s));' % (arg[1], arg[1]), file=fd)
elif isinstance(qualifier, str) and qualifier.startswith('Lasso'):
print_(' set_list_of_pygobject(&%s, cvt_%s);' % (arg[1], arg[1]), file=fd)
print_(' EXIT_IF_FAIL(set_list_of_pygobject(&%s, cvt_%s));' % (arg[1], arg[1]), file=fd)
else:
print_('E: unqualified GList argument in', name, qualifier, arg, file=sys.stderr)
elif is_xml_node(arg):

View File

@ -344,6 +344,15 @@ class BindingTestCase(unittest.TestCase):
with self.assertRaises(TypeError, msg='value should be a tuple of strings'):
node.authnContextClassRef = [None]
def test_set_list_of_pygobject(self):
node = lasso.Saml2Attribute()
class A:
_cptr = None
value = [A()]
with self.assertRaises(TypeError, msg='value should be a tuple of PyGobject'):
node.attributeValue = value
bindingSuite = unittest.makeSuite(BindingTestCase, 'test')
allTests = unittest.TestSuite((bindingSuite, ))

View File

@ -93,7 +93,7 @@ G_GNUC_UNUSED static int set_hashtable_of_pygobject(GHashTable *a_hash, PyObject
G_GNUC_UNUSED static int set_hashtable_of_strings(GHashTable *a_hash, PyObject *dict);
G_GNUC_UNUSED static int set_list_of_strings(GList **a_list, PyObject *seq);
G_GNUC_UNUSED static int set_list_of_xml_nodes(GList **a_list, PyObject *seq);
G_GNUC_UNUSED static void set_list_of_pygobject(GList **a_list, PyObject *seq);
G_GNUC_UNUSED static int set_list_of_pygobject(GList **a_list, PyObject *seq);
G_GNUC_UNUSED static PyObject *get_list_of_strings(const GList *a_list);
G_GNUC_UNUSED static PyObject *get_list_of_xml_nodes(const GList *a_list);
G_GNUC_UNUSED static PyObject *get_list_of_pygobject(const GList *a_list);
@ -401,12 +401,16 @@ failure:
/** Set the GList* pointer, pointed by a_list, to a pointer on a new GList
* created by converting the python seq into a GList of GObject*.
*/
static void
static int
set_list_of_pygobject(GList **a_list, PyObject *seq) {
GList *list = NULL;
int l = 0,i;
lasso_return_if_fail(valid_seq(seq));
if (!valid_seq(seq)) {
PyErr_SetString(PyExc_TypeError,
"value should be a tuple of PyGobject");
return 0;
}
if (seq != Py_None) {
l = PySequence_Length(seq);
}
@ -423,9 +427,10 @@ set_list_of_pygobject(GList **a_list, PyObject *seq) {
}
free_list(a_list, (GFunc)g_object_unref);
*a_list = list;
return;
return 1;
failure:
free_list(&list, (GFunc)g_object_unref);
return 0;
}
static xmlNode*