[pygobject/coverage-gobject-overrides] wip
- From: Christoph Reiter <creiter src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [pygobject/coverage-gobject-overrides] wip
- Date: Sat, 24 Nov 2018 16:26:02 +0000 (UTC)
commit b77c5c1f4ef9299accb85546facaac7e706051ea
Author: Christoph Reiter <reiter christoph gmail com>
Date: Fri Nov 23 20:54:25 2018 +0100
wip
gi/overrides/GObject.py | 29 ++---
tests/test_gobject.py | 9 ++
tests/test_overrides_gobject.py | 265 ++++++++++++++++++++++++++++++++++++++++
3 files changed, 286 insertions(+), 17 deletions(-)
---
diff --git a/gi/overrides/GObject.py b/gi/overrides/GObject.py
index 26e9f25b..44eaa185 100644
--- a/gi/overrides/GObject.py
+++ b/gi/overrides/GObject.py
@@ -260,14 +260,16 @@ class Value(GObjectModule.Value):
if isinstance(py_value, text_type):
py_value = py_value.encode('UTF-8')
else:
- raise ValueError("Expected string or unicode but got %s%s" %
- (py_value, type(py_value)))
+ raise TypeError("Expected string or unicode but got %s%s" %
+ (py_value, type(py_value)))
else:
- raise ValueError("Expected string but got %s%s" %
- (py_value, type(py_value)))
+ raise TypeError("Expected string but got %s%s" %
+ (py_value, type(py_value)))
self.set_string(py_value)
elif gtype == TYPE_PARAM:
self.set_param(py_value)
+ elif gtype == TYPE_PYOBJECT:
+ self.set_boxed(py_value)
elif gtype.is_a(TYPE_ENUM):
self.set_enum(py_value)
elif gtype.is_a(TYPE_FLAGS):
@@ -278,16 +280,10 @@ class Value(GObjectModule.Value):
self.set_pointer(py_value)
elif gtype.is_a(TYPE_OBJECT):
self.set_object(py_value)
- elif gtype == TYPE_UNICHAR:
- self.set_uint(int(py_value))
- # elif gtype == TYPE_OVERRIDE:
- # pass
elif gtype == TYPE_GTYPE:
self.set_gtype(py_value)
elif gtype == TYPE_VARIANT:
self.set_variant(py_value)
- elif gtype == TYPE_PYOBJECT:
- self.set_boxed(py_value)
else:
raise TypeError("Unknown value type %s" % gtype)
@@ -318,6 +314,8 @@ class Value(GObjectModule.Value):
return self.get_double()
elif gtype == TYPE_STRING:
return self.get_string()
+ elif gtype == TYPE_PYOBJECT:
+ return self.get_boxed()
elif gtype == TYPE_PARAM:
return self.get_param()
elif gtype.is_a(TYPE_ENUM):
@@ -330,16 +328,14 @@ class Value(GObjectModule.Value):
return self.get_pointer()
elif gtype.is_a(TYPE_OBJECT):
return self.get_object()
- elif gtype == TYPE_UNICHAR:
- return self.get_uint()
elif gtype == TYPE_GTYPE:
return self.get_gtype()
elif gtype == TYPE_VARIANT:
return self.get_variant()
- elif gtype == TYPE_PYOBJECT:
- pass
- else:
+ elif gtype == _gi.TYPE_INVALID:
return None
+ else:
+ raise TypeError("Unknown value type %s" % gtype)
def __repr__(self):
return '<Value (%s) %s>' % (self.g_type.name, self.get_value())
@@ -415,8 +411,7 @@ def signal_query(id_or_name, type_=None):
id_or_name = signal_lookup(id_or_name, type_)
res = GObjectModule.signal_query(id_or_name)
- if res is None:
- return None
+ assert res is not None
if res.signal_id == 0:
return None
diff --git a/tests/test_gobject.py b/tests/test_gobject.py
index bef3e9b5..9b4656b4 100644
--- a/tests/test_gobject.py
+++ b/tests/test_gobject.py
@@ -15,6 +15,7 @@ from gi.module import get_introspection_module
from gi import _gi
import testhelper
+from .helper import capture_glib_deprecation_warnings
class TestGObjectAPI(unittest.TestCase):
@@ -463,6 +464,14 @@ class TestPropertyBindings(unittest.TestCase):
self.assertEqual(self.source.int_prop, 1)
self.assertEqual(self.target.int_prop, 2)
+ def test_call_binding(self):
+ binding = self.source.bind_property('int_prop', self.target, 'int_prop',
+ GObject.BindingFlags.DEFAULT)
+ with capture_glib_deprecation_warnings() as warn:
+ result = binding()
+ assert len(warn)
+ assert result is binding
+
def test_bidirectional_binding(self):
binding = self.source.bind_property('int_prop', self.target, 'int_prop',
GObject.BindingFlags.BIDIRECTIONAL)
diff --git a/tests/test_overrides_gobject.py b/tests/test_overrides_gobject.py
new file mode 100644
index 00000000..3c7cc04b
--- /dev/null
+++ b/tests/test_overrides_gobject.py
@@ -0,0 +1,265 @@
+# -*- coding: utf-8 -*-
+
+from __future__ import absolute_import
+
+import pytest
+
+from gi import PyGIDeprecationWarning
+from gi.repository import GObject, GLib
+
+from gi._compat import PY2
+from .helper import ignore_gi_deprecation_warnings
+
+
+def test_stop_emission_deprec():
+ class TestObject(GObject.GObject):
+ int_prop = GObject.Property(default=0, type=int)
+
+ obj = TestObject()
+
+ def notify_callback(obj, *args):
+ with pytest.warns(PyGIDeprecationWarning):
+ obj.stop_emission("notify::int-prop")
+
+ with pytest.warns(PyGIDeprecationWarning):
+ obj.emit_stop_by_name("notify::int-prop")
+
+ obj.stop_emission_by_name("notify::int-prop")
+
+ obj.connect("notify::int-prop", notify_callback)
+ obj.notify("int-prop")
+
+
+def test_signal_parse_name():
+ obj = GObject.GObject()
+ assert GObject.signal_parse_name("notify", obj, True) == (1, 0)
+
+ with pytest.raises(ValueError):
+ GObject.signal_parse_name("foobar", obj, True)
+
+
+def test_signal_query():
+ obj = GObject.GObject()
+ res = GObject.signal_query("notify", obj)
+ assert res.signal_name == "notify"
+ assert res.itype == obj.__gtype__
+
+ res = GObject.signal_query("foobar", obj)
+ assert res is None
+
+
+def test_value_repr():
+ v = GObject.Value()
+ assert repr(v) == "<Value (invalid) None>"
+
+ v = GObject.Value(int, 0)
+ assert repr(v) == "<Value (gint) 0>"
+
+
+def test_value_no_init():
+ v = GObject.Value()
+ with pytest.raises(TypeError):
+ v.set_value(0)
+ v.init(GObject.TYPE_LONG)
+ assert v.get_value() == 0
+ v.set_value(0)
+
+
+def test_value_invalid_type():
+ # FIXME: this complains that the value isn't a GType
+ # GObject.Value(GObject.TYPE_INVALID)
+ pass
+
+
+def test_value_long():
+ v = GObject.Value(GObject.TYPE_LONG)
+ assert v.get_value() == 0
+ v.set_value(0)
+ assert v.get_value() == 0
+
+ v.set_value(GLib.MAXLONG)
+ assert v.get_value() == GLib.MAXLONG
+
+ v.set_value(GLib.MINLONG)
+ assert v.get_value() == GLib.MINLONG
+
+ with pytest.raises(OverflowError):
+ v.set_value(GLib.MAXLONG + 1)
+
+ with pytest.raises(OverflowError):
+ v.set_value(GLib.MINLONG - 1)
+
+
+def test_value_ulong():
+ v = GObject.Value(GObject.TYPE_ULONG)
+ assert v.get_value() == 0
+ v.set_value(0)
+ assert v.get_value() == 0
+
+ v.set_value(GLib.MAXULONG)
+ assert v.get_value() == GLib.MAXULONG
+
+ with pytest.raises(OverflowError):
+ v.set_value(GLib.MAXULONG + 1)
+
+ with pytest.raises(OverflowError):
+ v.set_value(-1)
+
+
+def test_value_uint64():
+ v = GObject.Value(GObject.TYPE_UINT64)
+ assert v.get_value() == 0
+ v.set_value(0)
+ assert v.get_value() == 0
+
+ v.set_value(GLib.MAXUINT64)
+ assert v.get_value() == GLib.MAXUINT64
+
+ with pytest.raises(OverflowError):
+ v.set_value(GLib.MAXUINT64 + 1)
+
+ with pytest.raises(OverflowError):
+ v.set_value(-1)
+
+
+def test_value_int64():
+ v = GObject.Value(GObject.TYPE_INT64)
+ assert v.get_value() == 0
+ v.set_value(0)
+ assert v.get_value() == 0
+
+ v.set_value(GLib.MAXINT64)
+ assert v.get_value() == GLib.MAXINT64
+ v.set_value(GLib.MININT64)
+ assert v.get_value() == GLib.MININT64
+
+ with pytest.raises(OverflowError):
+ v.set_value(GLib.MAXINT64 + 1)
+
+ with pytest.raises(OverflowError):
+ v.set_value(GLib.MININT64 - 1)
+
+
+def test_value_pointer():
+ v = GObject.Value(GObject.TYPE_POINTER)
+ assert v.get_value() == 0
+ v.set_value(42)
+ assert v.get_value() == 42
+ v.set_value(0)
+ assert v.get_value() == 0
+
+
+def test_value_unichar():
+ assert GObject.TYPE_UNICHAR == GObject.TYPE_UINT
+
+ v = GObject.Value(GObject.TYPE_UNICHAR)
+ assert v.get_value() == 0
+ v.set_value(42)
+ assert v.get_value() == 42
+
+ v.set_value(GLib.MAXUINT)
+ assert v.get_value() == GLib.MAXUINT
+
+
+def test_value_gtype():
+ class TestObject(GObject.GObject):
+ pass
+
+ v = GObject.Value(GObject.TYPE_GTYPE)
+ assert v.get_value() == GObject.TYPE_INVALID
+ v.set_value(TestObject.__gtype__)
+ assert v.get_value() == TestObject.__gtype__
+ v.set_value(TestObject)
+ assert v.get_value() == TestObject.__gtype__
+
+ with pytest.raises(TypeError):
+ v.set_value(None)
+
+
+def test_value_variant():
+ v = GObject.Value(GObject.TYPE_VARIANT)
+ assert v.get_value() is None
+ variant = GLib.Variant('i', 42)
+ v.set_value(variant)
+
+ # FIXME: triggers an assert
+ # assert v.get_value() == variant
+
+ v.set_value(None)
+ assert v.get_value() is None
+
+
+def test_value_param():
+ # FIXME: set_value and get_value trigger a critical
+ # GObject.Value(GObject.TYPE_PARAM)
+ pass
+
+
+def test_value_string():
+ v = GObject.Value(GObject.TYPE_STRING)
+ assert v.get_value() is None
+
+ if PY2:
+ v.set_value(b"bar")
+ assert v.get_value() == b"bar"
+
+ v.set_value(u"öäü")
+ assert v.get_value().decode("utf-8") == u"öäü"
+ else:
+ with pytest.raises(TypeError):
+ v.set_value(b"bar")
+
+ v.set_value(u"quux")
+ assert v.get_value() == u"quux"
+ assert isinstance(v.get_value(), str)
+
+ with pytest.raises(TypeError):
+ v.set_value(None)
+
+
+def test_value_pyobject():
+ class Foo(object):
+ pass
+
+ v = GObject.Value(GObject.TYPE_PYOBJECT)
+ assert v.get_value() is None
+ for obj in [Foo(), None, 42, "foo"]:
+ v.set_value(obj)
+ assert v.get_value() == obj
+
+
+@ignore_gi_deprecation_warnings
+def test_value_char():
+ v = GObject.Value(GObject.TYPE_CHAR)
+ assert v.get_value() == 0
+ v.set_value(42)
+ assert v.get_value() == 42
+ v.set_value(-1)
+ assert v.get_value() == -1
+ v.set_value(b"a")
+ assert v.get_value() == 97
+ v.set_value(b"\x00")
+ assert v.get_value() == 0
+
+ with pytest.raises(TypeError):
+ v.set_value(u"a")
+
+ with pytest.raises(OverflowError):
+ v.set_value(128)
+
+
+def test_value_uchar():
+ v = GObject.Value(GObject.TYPE_UCHAR)
+ assert v.get_value() == 0
+ v.set_value(200)
+ assert v.get_value() == 200
+ v.set_value(b"a")
+ assert v.get_value() == 97
+ v.set_value(b"\x00")
+ assert v.get_value() == 0
+
+ with pytest.raises(TypeError):
+ v.set_value(u"a")
+
+ with pytest.raises(OverflowError):
+ v.set_value(256)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]