From 4cf6bd4ff7d68f4b19bc7260e1d04532412c82f1 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Sat, 20 Jun 2020 08:33:35 +0200 Subject: [PATCH] python: return NULL if set_list_of_pygobject fails (#44287) --- bindings/python/lang.py | 4 ++-- bindings/python/tests/binding_tests.py | 9 +++++++++ bindings/python/wrapper_top.c | 13 +++++++++---- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/bindings/python/lang.py b/bindings/python/lang.py index 2f580bd3..c44d9d6b 100644 --- a/bindings/python/lang.py +++ b/bindings/python/lang.py @@ -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): diff --git a/bindings/python/tests/binding_tests.py b/bindings/python/tests/binding_tests.py index 9a801be2..a0e39c85 100755 --- a/bindings/python/tests/binding_tests.py +++ b/bindings/python/tests/binding_tests.py @@ -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, )) diff --git a/bindings/python/wrapper_top.c b/bindings/python/wrapper_top.c index 84d25650..b24f4b2b 100644 --- a/bindings/python/wrapper_top.c +++ b/bindings/python/wrapper_top.c @@ -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*