[pygobject] Support defining GType properties from Python
- From: Martin Pitt <martinpitt src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [pygobject] Support defining GType properties from Python
- Date: Fri, 20 Apr 2012 13:10:11 +0000 (UTC)
commit 71246ca0568bf3e9b81e88dd13b6d29e9417e313
Author: Martin Pitt <martinpitt gnome org>
Date: Thu Apr 19 13:11:56 2012 +0200
Support defining GType properties from Python
Commit 84e3471 fixed the handling of GType properties for properties that are
defined in the C library already. Add the missing support for defining such
properties in Python as well.
https://bugzilla.gnome.org/show_bug.cgi?id=674351
gi/_gobject/gobjectmodule.c | 5 +++-
gi/_gobject/propertyhelper.py | 9 +++++--
tests/test_properties.py | 42 ++++++++++++++++++++++++++++++++++++++--
3 files changed, 49 insertions(+), 7 deletions(-)
---
diff --git a/gi/_gobject/gobjectmodule.c b/gi/_gobject/gobjectmodule.c
index cacd46b..0d75b7e 100644
--- a/gi/_gobject/gobjectmodule.c
+++ b/gi/_gobject/gobjectmodule.c
@@ -719,7 +719,10 @@ create_property (const gchar *prop_name,
case G_TYPE_POINTER:
if (!PyArg_ParseTuple(args, ""))
return NULL;
- pspec = g_param_spec_pointer (prop_name, nick, blurb, flags);
+ if (prop_type == G_TYPE_GTYPE)
+ pspec = g_param_spec_gtype (prop_name, nick, blurb, G_TYPE_NONE, flags);
+ else
+ pspec = g_param_spec_pointer (prop_name, nick, blurb, flags);
break;
case G_TYPE_OBJECT:
if (!PyArg_ParseTuple(args, ""))
diff --git a/gi/_gobject/propertyhelper.py b/gi/_gobject/propertyhelper.py
index ceb6570..dff1d79 100644
--- a/gi/_gobject/propertyhelper.py
+++ b/gi/_gobject/propertyhelper.py
@@ -29,7 +29,7 @@ from .constants import \
TYPE_ULONG, TYPE_INT64, TYPE_UINT64, TYPE_ENUM, TYPE_FLAGS, \
TYPE_FLOAT, TYPE_DOUBLE, TYPE_STRING, \
TYPE_POINTER, TYPE_BOXED, TYPE_PARAM, TYPE_OBJECT, \
- TYPE_PYOBJECT
+ TYPE_PYOBJECT, TYPE_GTYPE
from .constants import \
G_MAXFLOAT, G_MAXDOUBLE, \
G_MININT, G_MAXINT, G_MAXUINT, G_MINLONG, G_MAXLONG, \
@@ -226,7 +226,7 @@ class Property(object):
TYPE_ULONG, TYPE_INT64, TYPE_UINT64,
TYPE_FLOAT, TYPE_DOUBLE, TYPE_POINTER,
TYPE_BOXED, TYPE_PARAM, TYPE_OBJECT, TYPE_STRING,
- TYPE_PYOBJECT]:
+ TYPE_PYOBJECT, TYPE_GTYPE]:
return type_
else:
raise TypeError("Unsupported type: %r" % (type_,))
@@ -255,6 +255,9 @@ class Property(object):
elif ptype == TYPE_PYOBJECT:
if default is not None:
raise TypeError("object types does not have default values")
+ elif ptype == TYPE_GTYPE:
+ if default is not None:
+ raise TypeError("GType types does not have default values")
elif _gobject.type_is_a(ptype, TYPE_ENUM):
if default is None:
raise TypeError("enum properties needs a default value")
@@ -335,7 +338,7 @@ class Property(object):
elif (ptype == TYPE_STRING or ptype == TYPE_BOOLEAN or
ptype.is_a(TYPE_ENUM) or ptype.is_a(TYPE_FLAGS)):
args = (self.default,)
- elif ptype == TYPE_PYOBJECT:
+ elif ptype in [TYPE_PYOBJECT, TYPE_GTYPE]:
args = ()
elif ptype.is_a(TYPE_OBJECT) or ptype.is_a(TYPE_BOXED):
args = ()
diff --git a/tests/test_properties.py b/tests/test_properties.py
index fc2a497..ba55634 100644
--- a/tests/test_properties.py
+++ b/tests/test_properties.py
@@ -8,8 +8,8 @@ from gi.repository import GObject
from gi.repository.GObject import GType, new, PARAM_READWRITE, \
PARAM_CONSTRUCT, PARAM_READABLE, PARAM_WRITABLE, PARAM_CONSTRUCT_ONLY
from gi.repository.GObject import \
- TYPE_INT, TYPE_UINT, TYPE_LONG, \
- TYPE_ULONG, TYPE_INT64, TYPE_UINT64
+ TYPE_INT, TYPE_UINT, TYPE_LONG, TYPE_ULONG, TYPE_INT64, \
+ TYPE_UINT64, TYPE_GTYPE, TYPE_INVALID, TYPE_NONE
from gi.repository.GObject import \
G_MININT, G_MAXINT, G_MAXUINT, G_MINLONG, G_MAXLONG, \
G_MAXULONG
@@ -49,6 +49,9 @@ class PropertyObject(GObject.GObject):
type=GIMarshallingTests.Flags, flags=PARAM_READWRITE | PARAM_CONSTRUCT,
default=GIMarshallingTests.Flags.VALUE1)
+ gtype = GObject.Property(
+ type=TYPE_GTYPE, flags=PARAM_READWRITE | PARAM_CONSTRUCT)
+
class TestProperties(unittest.TestCase):
def testGetSet(self):
@@ -80,8 +83,9 @@ class TestProperties(unittest.TestCase):
'uint64',
'enum',
'flags',
+ 'gtype',
'boxed'])
- self.assertEqual(len(obj), 7)
+ self.assertEqual(len(obj), 8)
def testNormal(self):
obj = new(PropertyObject, normal="123")
@@ -194,6 +198,38 @@ class TestProperties(unittest.TestCase):
self.assertRaises(TypeError, GObject.Property,
type=GIMarshallingTests.Flags, default=None)
+ def testGType(self):
+ obj = new(PropertyObject)
+
+ self.assertEqual(obj.props.gtype, TYPE_NONE)
+ self.assertEqual(obj.gtype, TYPE_NONE)
+
+ obj.gtype = TYPE_UINT64
+ self.assertEqual(obj.props.gtype, TYPE_UINT64)
+ self.assertEqual(obj.gtype, TYPE_UINT64)
+
+ obj.gtype = TYPE_INVALID
+ self.assertEqual(obj.props.gtype, TYPE_INVALID)
+ self.assertEqual(obj.gtype, TYPE_INVALID)
+
+ # GType parameters do not support defaults in GLib
+ self.assertRaises(TypeError, GObject.Property, type=TYPE_GTYPE,
+ default=TYPE_INT)
+
+ # incompatible type
+ self.assertRaises(TypeError, setattr, obj, 'gtype', 'foo')
+ self.assertRaises(TypeError, setattr, obj, 'gtype', object())
+
+ self.assertRaises(TypeError, GObject.Property, type=TYPE_GTYPE,
+ default='foo')
+ self.assertRaises(TypeError, GObject.Property, type=TYPE_GTYPE,
+ default=object())
+
+ # set in constructor
+ obj = new(PropertyObject, gtype=TYPE_UINT)
+ self.assertEqual(obj.props.gtype, TYPE_UINT)
+ self.assertEqual(obj.gtype, TYPE_UINT)
+
def textBoxed(self):
obj = new(PropertyObject)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]