From 8627104e06b0f7c452ced6a9a384260bd08fa8d8 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Sat, 20 Jun 2020 08:33:35 +0200 Subject: [PATCH 16/21] python: return NULL if set_list_of_pygobject fails (#44287) --- bindings/python/lang.py | 4 ++-- bindings/python/tests/binding_tests.py | 8 ++++++++ bindings/python/wrapper_top.c | 13 +++++++++---- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/bindings/python/lang.py b/bindings/python/lang.py index 50b31db7..b3f9f764 100644 --- a/bindings/python/lang.py +++ b/bindings/python/lang.py @@ -822,7 +822,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): @@ -985,7 +985,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 5f71587d..0d2d53b4 100755 --- a/bindings/python/tests/binding_tests.py +++ b/bindings/python/tests/binding_tests.py @@ -334,6 +334,14 @@ 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') diff --git a/bindings/python/wrapper_top.c b/bindings/python/wrapper_top.c index 6687acce..005fb5c7 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 +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* -- 2.26.2