Commit 48f80f8b authored by Christoph Reiter's avatar Christoph Reiter 🐍

GObject.Value: add a static helper for fetching the GType

The field marshalling code is slow and doesn't do any caching at all
which in turn makes accessing Value.g_type slow.

Add a static helper function for fetching the GType and use that instead.
This reduced the time for creating a GValue + setting it by ~30%
parent 7fcc843c
......@@ -2291,6 +2291,8 @@ static PyMethodDef _gi_functions[] = {
(PyCFunction)pyg__install_metaclass, METH_O },
{ "_gvalue_get",
(PyCFunction)pyg__gvalue_get, METH_O },
{ "_gvalue_get_type",
(PyCFunction)pyg__gvalue_get_type, METH_O },
{ "_gvalue_set",
(PyCFunction)pyg__gvalue_set, METH_VARARGS },
{ NULL, NULL, 0 }
......
......@@ -209,8 +209,14 @@ class Value(GObjectModule.Value):
if py_value is not None:
self.set_value(py_value)
@property
def __g_type(self):
# XXX: This is the same as self.g_type, but the field marshalling
# code is currently very slow.
return _gi._gvalue_get_type(self)
def set_boxed(self, boxed):
if not self.g_type.is_a(TYPE_BOXED):
if not self.__g_type.is_a(TYPE_BOXED):
warnings.warn('Calling set_boxed() on a non-boxed type deprecated',
PyGIDeprecationWarning, stacklevel=2)
# Workaround the introspection marshalers inability to know
......@@ -219,13 +225,13 @@ class Value(GObjectModule.Value):
_gi._gvalue_set(self, boxed)
def get_boxed(self):
if not self.g_type.is_a(TYPE_BOXED):
if not self.__g_type.is_a(TYPE_BOXED):
warnings.warn('Calling get_boxed() on a non-boxed type deprecated',
PyGIDeprecationWarning, stacklevel=2)
return _gi._gvalue_get(self)
def set_value(self, py_value):
gtype = self.g_type
gtype = self.__g_type
if gtype == _gi.TYPE_INVALID:
raise TypeError("GObject.Value needs to be initialized first")
......@@ -288,7 +294,7 @@ class Value(GObjectModule.Value):
_gi._gvalue_set(self, py_value)
def get_value(self):
gtype = self.g_type
gtype = self.__g_type
if gtype == TYPE_BOOLEAN:
return self.get_boolean()
......@@ -340,7 +346,7 @@ class Value(GObjectModule.Value):
return _gi._gvalue_get(self)
def __repr__(self):
return '<Value (%s) %s>' % (self.g_type.name, self.get_value())
return '<Value (%s) %s>' % (self.__g_type.name, self.get_value())
Value = override(Value)
......
......@@ -886,6 +886,22 @@ pyg__gvalue_get(PyObject *module, PyObject *pygvalue)
/*copy_boxed=*/ TRUE);
}
PyObject *
pyg__gvalue_get_type(PyObject *module, PyObject *pygvalue)
{
GValue *value;
GType type;
if (!pyg_boxed_check (pygvalue, G_TYPE_VALUE)) {
PyErr_SetString (PyExc_TypeError, "Expected GValue argument.");
return NULL;
}
value = pyg_boxed_get (pygvalue, GValue);
type = G_VALUE_TYPE (value);
return pyg_type_wrapper_new (type);
}
PyObject *
pyg__gvalue_set(PyObject *module, PyObject *args)
{
......
......@@ -45,6 +45,7 @@ PyObject *pygi_value_to_py_basic_type (const GValue *value,
PyObject *pyg__gvalue_get(PyObject *module, PyObject *pygvalue);
PyObject *pyg__gvalue_set(PyObject *module, PyObject *args);
PyObject *pyg__gvalue_get_type(PyObject *module, PyObject *pygvalue);
G_END_DECLS
......
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