Commit 306b792a authored by Tomeu Vizoso's avatar Tomeu Vizoso Committed by John (J5) Palmieri
Browse files

Add override for GLib.Variant.new_tuple

* gi/gimodule.c: Add _wrap_pyg_variant_new_tuple
* gi/overrides/GLib.py: Override Variant.new_tuple and Variant.get_string
* gi/pygi-type.[hc]: split _pygi_type_import_by_name out from
  _pygi_type_import_by_gi_info
* gi/types.py: Never override gobject.TYPE_NONE
* tests/test_everything.py: Add tests for GVariant tuples

https://bugzilla.gnome.org/show_bug.cgi?id=625050
parent 22e53aa2
......@@ -235,6 +235,43 @@ _wrap_pyg_hook_up_vfunc_implementation (PyObject *self, PyObject *args)
Py_RETURN_NONE;
}
static PyObject *
_wrap_pyg_variant_new_tuple (PyObject *self, PyObject *args)
{
PyObject *py_values;
GVariant **values = NULL;
GVariant *variant = NULL;
PyObject *py_variant = NULL;
PyObject *py_type;
gssize i;
if (!PyArg_ParseTuple (args, "O!:variant_new_tuple",
&PyTuple_Type, &py_values)) {
return NULL;
}
py_type = _pygi_type_import_by_name ("GLib", "Variant");
values = g_newa (GVariant*, PyTuple_Size (py_values));
for (i = 0; i < PyTuple_Size (py_values); i++) {
PyObject *value = PyTuple_GET_ITEM (py_values, i);
if (!PyObject_IsInstance (value, py_type)) {
PyErr_Format (PyExc_TypeError, "argument %d is not a GLib.Variant", i);
return NULL;
}
values[i] = (GVariant *) ( (PyGPointer *) value)->pointer;
}
variant = g_variant_new_tuple (values, PyTuple_Size (py_values));
py_variant = _pygi_struct_new ( (PyTypeObject *) py_type, variant, FALSE);
return py_variant;
}
static PyMethodDef _gi_functions[] = {
{ "enum_add", (PyCFunction) _wrap_pyg_enum_add, METH_VARARGS | METH_KEYWORDS },
{ "flags_add", (PyCFunction) _wrap_pyg_flags_add, METH_VARARGS | METH_KEYWORDS },
......@@ -242,6 +279,7 @@ static PyMethodDef _gi_functions[] = {
{ "set_object_has_new_constructor", (PyCFunction) _wrap_pyg_set_object_has_new_constructor, METH_VARARGS | METH_KEYWORDS },
{ "register_interface_info", (PyCFunction) _wrap_pyg_register_interface_info, METH_VARARGS },
{ "hook_up_vfunc_implementation", (PyCFunction) _wrap_pyg_hook_up_vfunc_implementation, METH_VARARGS },
{ "variant_new_tuple", (PyCFunction) _wrap_pyg_variant_new_tuple, METH_VARARGS },
{ NULL, NULL, 0 }
};
......
......@@ -4,6 +4,7 @@ pkgpyexecdir = $(pyexecdir)/gtk-2.0/gi
pygioverridesdir = $(pkgpyexecdir)/overrides
pygioverrides_PYTHON = \
GLib.py \
Gtk.py \
Gdk.py \
GIMarshallingTests.py \
......
......@@ -25,17 +25,13 @@
PyObject *
_pygi_type_import_by_gi_info (GIBaseInfo *info)
_pygi_type_import_by_name (const char *namespace_,
const char *name)
{
const gchar *namespace_;
const gchar *name;
gchar *module_name;
PyObject *py_module;
PyObject *py_object;
namespace_ = g_base_info_get_namespace (info);
name = g_base_info_get_name (info);
module_name = g_strconcat ("gi.repository.", namespace_, NULL);
py_module = PyImport_ImportModule (module_name);
......@@ -73,6 +69,13 @@ pygi_type_import_by_g_type_real (GType g_type)
return type;
}
PyObject *
_pygi_type_import_by_gi_info (GIBaseInfo *info)
{
return _pygi_type_import_by_name (g_base_info_get_namespace (info),
g_base_info_get_name (info));
}
PyObject *
_pygi_type_get_from_g_type (GType g_type)
{
......
......@@ -33,6 +33,8 @@ PyObject *pygi_type_import_by_g_type_real (GType g_type);
/* Private */
PyObject *_pygi_type_import_by_name (const char *namespace_, const char *name);
PyObject *_pygi_type_import_by_gi_info (GIBaseInfo *info);
PyObject *_pygi_type_get_from_g_type (GType g_type);
......
......@@ -165,6 +165,7 @@ class StructMeta(type, MetaClassHelper):
def override(type_):
g_type = type_.__info__.get_g_type()
assert g_type != gobject.TYPE_NONE
if g_type != gobject.TYPE_INVALID:
g_type.pytype = type_
return type_
......
......@@ -50,11 +50,20 @@ class TestEverything(unittest.TestCase):
def test_gvariant(self):
variant = GLib.Variant.new_int32(42);
self.assertTrue(isinstance(variant, GLib.Variant))
self.assertEquals(variant.get_int32(), 42)
variant = GLib.Variant.new_strv(['mec', 'mac']);
self.assertTrue(isinstance(variant, GLib.Variant))
self.assertEquals(variant.get_strv(), ['mec', 'mac'])
variant = GLib.Variant.new_tuple(GLib.Variant.new_string('mec'), GLib.Variant.new_string('mac'))
self.assertTrue(isinstance(variant, GLib.Variant))
self.assertTrue(isinstance(variant.get_child_value(0), GLib.Variant))
self.assertTrue(isinstance(variant.get_child_value(1), GLib.Variant))
self.assertEquals(variant.get_child_value(0).get_string(), 'mec')
self.assertEquals(variant.get_child_value(1).get_string(), 'mac')
def test_floating(self):
Everything.TestFloating()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment