[pygobject/overrides-coverage] gdk overrides: improve coverage and remove overrides for GDK4



commit 04909ad442e1ab049394e801ada5b7436d3045cf
Author: Christoph Reiter <reiter christoph gmail com>
Date:   Mon Nov 19 19:15:02 2018 +0100

    gdk overrides: improve coverage and remove overrides for GDK4
    
    Take the chance and get rid of it. If someone misses them we can look into adding
    some of it back.

 gi/overrides/Gdk.py         | 141 +++++++++++++++++++++-----------------------
 tests/test_overrides_gdk.py |  93 +++++++++++++++++++++++++----
 2 files changed, 147 insertions(+), 87 deletions(-)
---
diff --git a/gi/overrides/Gdk.py b/gi/overrides/Gdk.py
index 44914db9..5aea65ff 100644
--- a/gi/overrides/Gdk.py
+++ b/gi/overrides/Gdk.py
@@ -27,6 +27,8 @@ from ..module import get_introspection_module
 from gi import PyGIDeprecationWarning, require_version
 
 Gdk = get_introspection_module('Gdk')
+GDK2 = Gdk._version == '2.0'
+GDK3 = Gdk._version == '3.0'
 
 __all__ = []
 
@@ -39,7 +41,7 @@ try:
 except (ValueError, ImportError):
     pass
 
-if hasattr(Gdk, 'Color'):
+if GDK2 or GDK3:
     # Gdk.Color was deprecated since 3.14 and dropped in Gtk+-4.0
     class Color(Gdk.Color):
         MAX_VALUE = 65535
@@ -81,7 +83,7 @@ if hasattr(Gdk, 'Color'):
     Color = override(Color)
     __all__.append('Color')
 
-if hasattr(Gdk, 'RGBA'):
+if GDK3:
     # Introduced since Gtk+-3.0
     class RGBA(Gdk.RGBA):
         def __init__(self, red=1.0, green=1.0, blue=1.0, alpha=1.0):
@@ -121,7 +123,7 @@ if hasattr(Gdk, 'RGBA'):
     RGBA = override(RGBA)
     __all__.append('RGBA')
 
-if Gdk._version == '2.0':
+if GDK2:
     class Rectangle(Gdk.Rectangle):
 
         def __init__(self, x, y, width, height):
@@ -136,7 +138,7 @@ if Gdk._version == '2.0':
 
     Rectangle = override(Rectangle)
     __all__.append('Rectangle')
-else:
+elif GDK3:
     # Newer GTK+/gobject-introspection (3.17.x) include GdkRectangle in the
     # typelib. See https://bugzilla.gnome.org/show_bug.cgi?id=748832 and
     # https://bugzilla.gnome.org/show_bug.cgi?id=748833
@@ -154,14 +156,14 @@ else:
         __all__.append('rectangle_intersect')
         __all__.append('rectangle_union')
 
-if Gdk._version == '2.0':
+if GDK2:
     class Drawable(Gdk.Drawable):
         def cairo_create(self):
             return Gdk.cairo_create(self)
 
     Drawable = override(Drawable)
     __all__.append('Drawable')
-elif Gdk._version == '3.0':
+elif GDK3:
     class Window(Gdk.Window):
         def __new__(cls, parent, attributes, attributes_mask):
             # Gdk.Window had to be made abstract,
@@ -177,7 +179,7 @@ elif Gdk._version == '3.0':
     Window = override(Window)
     __all__.append('Window')
 
-if Gdk._version in ("2.0", "3.0"):
+if GDK2 or GDK3:
     Gdk.EventType._2BUTTON_PRESS = getattr(Gdk.EventType, "2BUTTON_PRESS")
     Gdk.EventType._3BUTTON_PRESS = getattr(Gdk.EventType, "3BUTTON_PRESS")
 
@@ -215,7 +217,7 @@ if Gdk._version in ("2.0", "3.0"):
             Gdk.EventType.UNMAP: 'any',
         }
 
-        if Gdk._version == '2.0':
+        if GDK2:
             _UNION_MEMBERS[Gdk.EventType.NO_EXPOSE] = 'no_expose'
 
         if hasattr(Gdk.EventType, 'TOUCH_BEGIN'):
@@ -315,85 +317,76 @@ if Gdk._version in ("2.0", "3.0"):
     DragContext = override(DragContext)
     __all__.append('DragContext')
 
+    class Cursor(Gdk.Cursor):
+
+        def __new__(cls, *args, **kwds):
+            arg_len = len(args)
+            kwd_len = len(kwds)
+            total_len = arg_len + kwd_len
+
+            if total_len == 1:
+                # Since g_object_newv (super.__new__) does not seem valid for
+                # direct use with GdkCursor, we must assume usage of at least
+                # one of the C constructors to be valid.
+                return cls.new(*args, **kwds)
+
+            elif total_len == 2:
+                warnings.warn('Calling "Gdk.Cursor(display, cursor_type)" has been deprecated. '
+                              'Please use Gdk.Cursor.new_for_display(display, cursor_type). '
+                              'See: https://wiki.gnome.org/PyGObject/InitializerDeprecations',
+                              PyGIDeprecationWarning)
+                return cls.new_for_display(*args, **kwds)
+
+            elif total_len == 4:
+                warnings.warn('Calling "Gdk.Cursor(display, pixbuf, x, y)" has been deprecated. '
+                              'Please use Gdk.Cursor.new_from_pixbuf(display, pixbuf, x, y). '
+                              'See: https://wiki.gnome.org/PyGObject/InitializerDeprecations',
+                              PyGIDeprecationWarning)
+                return cls.new_from_pixbuf(*args, **kwds)
+
+            elif total_len == 6:
+                if not GDK2:
+                    # pixmaps don't exist in Gdk 3.0
+                    raise ValueError("Wrong number of parameters")
+
+                warnings.warn('Calling "Gdk.Cursor(source, mask, fg, bg, x, y)" has been deprecated. '
+                              'Please use Gdk.Cursor.new_from_pixmap(source, mask, fg, bg, x, y). '
+                              'See: https://wiki.gnome.org/PyGObject/InitializerDeprecations',
+                              PyGIDeprecationWarning)
+                return cls.new_from_pixmap(*args, **kwds)
 
-class Cursor(Gdk.Cursor):
-
-    def __new__(cls, *args, **kwds):
-        arg_len = len(args)
-        kwd_len = len(kwds)
-        total_len = arg_len + kwd_len
-
-        if total_len == 1:
-            if Gdk._version == "4.0":
-                raise ValueError("Wrong number of parameters")
-            # Since g_object_newv (super.__new__) does not seem valid for
-            # direct use with GdkCursor, we must assume usage of at least
-            # one of the C constructors to be valid.
-            return cls.new(*args, **kwds)
-
-        elif total_len == 2:
-            warnings.warn('Calling "Gdk.Cursor(display, cursor_type)" has been deprecated. '
-                          'Please use Gdk.Cursor.new_for_display(display, cursor_type). '
-                          'See: https://wiki.gnome.org/PyGObject/InitializerDeprecations',
-                          PyGIDeprecationWarning)
-            return cls.new_for_display(*args, **kwds)
-
-        elif total_len == 4:
-            warnings.warn('Calling "Gdk.Cursor(display, pixbuf, x, y)" has been deprecated. '
-                          'Please use Gdk.Cursor.new_from_pixbuf(display, pixbuf, x, y). '
-                          'See: https://wiki.gnome.org/PyGObject/InitializerDeprecations',
-                          PyGIDeprecationWarning)
-            return cls.new_from_pixbuf(*args, **kwds)
-
-        elif total_len == 6:
-            if Gdk._version != '2.0':
-                # pixmaps don't exist in Gdk 3.0
+            else:
                 raise ValueError("Wrong number of parameters")
 
-            warnings.warn('Calling "Gdk.Cursor(source, mask, fg, bg, x, y)" has been deprecated. '
-                          'Please use Gdk.Cursor.new_from_pixmap(source, mask, fg, bg, x, y). '
-                          'See: https://wiki.gnome.org/PyGObject/InitializerDeprecations',
-                          PyGIDeprecationWarning)
-            return cls.new_from_pixmap(*args, **kwds)
-
-        else:
-            raise ValueError("Wrong number of parameters")
-
+    Cursor = override(Cursor)
+    __all__.append('Cursor')
 
-Cursor = override(Cursor)
-__all__.append('Cursor')
-
-if hasattr(Gdk, 'color_parse'):
     # Gdk.Color was deprecated since 3.14 and dropped in Gtk+-4.0
     color_parse = strip_boolean_result(Gdk.color_parse)
     __all__.append('color_parse')
 
+    # Note, we cannot override the entire class as Gdk.Atom has no gtype, so just
+    # hack some individual methods
+    def _gdk_atom_str(atom):
+        n = atom.name()
+        if n:
+            return n
+        # fall back to atom index
+        return 'Gdk.Atom<%i>' % hash(atom)
+
+    def _gdk_atom_repr(atom):
+        n = atom.name()
+        if n:
+            return 'Gdk.Atom.intern("%s", False)' % n
+        # fall back to atom index
+        return '<Gdk.Atom(%i)>' % hash(atom)
 
-# Note, we cannot override the entire class as Gdk.Atom has no gtype, so just
-# hack some individual methods
-def _gdk_atom_str(atom):
-    n = atom.name()
-    if n:
-        return n
-    # fall back to atom index
-    return 'Gdk.Atom<%i>' % hash(atom)
-
-
-def _gdk_atom_repr(atom):
-    n = atom.name()
-    if n:
-        return 'Gdk.Atom.intern("%s", False)' % n
-    # fall back to atom index
-    return '<Gdk.Atom(%i)>' % hash(atom)
-
-
-if Gdk._version in ("2.0", "3.0"):
     Gdk.Atom.__str__ = _gdk_atom_str
     Gdk.Atom.__repr__ = _gdk_atom_repr
 
 
 # constants
-if Gdk._version == '3.0':
+if GDK3:
     SELECTION_PRIMARY = Gdk.atom_intern('PRIMARY', True)
     __all__.append('SELECTION_PRIMARY')
 
@@ -442,6 +435,6 @@ if Gdk._version == '3.0':
     SELECTION_TYPE_STRING = Gdk.atom_intern('STRING', True)
     __all__.append('SELECTION_TYPE_STRING')
 
-if Gdk._version in ('2.0', '3.0'):
+if GDK2 or GDK3:
     import sys
     initialized, argv = Gdk.init_check(sys.argv)
diff --git a/tests/test_overrides_gdk.py b/tests/test_overrides_gdk.py
index 9c366744..e327abe9 100644
--- a/tests/test_overrides_gdk.py
+++ b/tests/test_overrides_gdk.py
@@ -3,19 +3,28 @@
 
 from __future__ import absolute_import
 
+import re
 import os
 import sys
 import unittest
+import pytest
 
+import gi
 import gi.overrides
 from gi import PyGIDeprecationWarning
 
 try:
     from gi.repository import Gdk, GdkPixbuf, Gtk
-    Gdk_version = Gdk._version
+    GDK4 = Gdk._version == "4.0"
 except ImportError:
     Gdk = None
-    Gdk_version = None
+    GDK4 = False
+
+try:
+    gi.require_foreign('cairo')
+    has_cairo = True
+except ImportError:
+    has_cairo = False
 
 from .helper import capture_glib_deprecation_warnings
 
@@ -24,7 +33,7 @@ from .helper import capture_glib_deprecation_warnings
 class TestGdk(unittest.TestCase):
 
     @unittest.skipIf(sys.platform == "darwin" or os.name == "nt", "crashes")
-    @unittest.skipIf(Gdk_version == "4.0", "not in gdk4")
+    @unittest.skipIf(GDK4, "not in gdk4")
     def test_constructor(self):
         attribute = Gdk.WindowAttr()
         attribute.window_type = Gdk.WindowType.CHILD
@@ -33,7 +42,7 @@ class TestGdk(unittest.TestCase):
         window = Gdk.Window(None, attribute, attributes_mask)
         self.assertEqual(window.get_window_type(), Gdk.WindowType.CHILD)
 
-    @unittest.skipIf(Gdk_version == "4.0", "not in gdk4")
+    @unittest.skipIf(GDK4, "not in gdk4")
     def test_color(self):
         color = Gdk.Color(100, 200, 300)
         self.assertEqual(color.red, 100)
@@ -43,7 +52,7 @@ class TestGdk(unittest.TestCase):
             self.assertEqual(color, Gdk.Color(100, 200, 300))
         self.assertNotEqual(color, Gdk.Color(1, 2, 3))
 
-    @unittest.skipIf(Gdk_version == "4.0", "not in gdk4")
+    @unittest.skipIf(GDK4, "not in gdk4")
     def test_color_floats(self):
         self.assertEqual(Gdk.Color(13107, 21845, 65535),
                          Gdk.Color.from_floats(0.2, 1.0 / 3.0, 1.0))
@@ -57,6 +66,20 @@ class TestGdk(unittest.TestCase):
         self.assertEqual(Gdk.RGBA.from_color(Gdk.Color(13107, 21845, 65535)),
                          Gdk.RGBA(0.2, 1.0 / 3.0, 1.0, 1.0))
 
+    @unittest.skipIf(GDK4, "not in gdk4")
+    def test_color_to_floats_attrs(self):
+        color = Gdk.Color(13107, 21845, 65535)
+        assert color.red_float == 0.2
+        color.red_float = 0
+        assert color.red_float == 0
+        assert color.green_float == 1.0 / 3.0
+        color.green_float = 0
+        assert color.green_float == 0
+        assert color.blue_float == 1.0
+        color.blue_float = 0
+        assert color.blue_float == 0
+
+    @unittest.skipIf(GDK4, "not in gdk4")
     def test_rgba(self):
         self.assertEqual(Gdk.RGBA, gi.overrides.Gdk.RGBA)
         rgba = Gdk.RGBA(0.1, 0.2, 0.3, 0.4)
@@ -73,7 +96,25 @@ class TestGdk(unittest.TestCase):
         self.assertEqual(tuple(Gdk.RGBA(0.1, 0.2, 0.3, 0.4)),
                          (0.1, 0.2, 0.3, 0.4))
 
-    @unittest.skipIf(Gdk_version == "4.0", "not in gdk4")
+    @unittest.skipUnless(GDK4, "only in gdk4")
+    def test_rgba_gtk4(self):
+        c = Gdk.RGBA()
+        assert c.to_string() == "rgba(0,0,0,0)"
+
+    @unittest.skipIf(not has_cairo or GDK4, "not in gdk4")
+    def test_window(self):
+        w = Gtk.Window()
+        w.realize()
+        window = w.get_window()
+        assert window.cairo_create() is not None
+
+    @unittest.skipIf(GDK4, "not in gdk4")
+    def test_drag_context(self):
+        context = Gdk.DragContext()
+        # using it this way crashes..
+        assert hasattr(context, "finish")
+
+    @unittest.skipIf(GDK4, "not in gdk4")
     def test_event(self):
         event = Gdk.Event.new(Gdk.EventType.CONFIGURE)
         self.assertEqual(event.type, Gdk.EventType.CONFIGURE)
@@ -83,7 +124,7 @@ class TestGdk(unittest.TestCase):
         event.type = Gdk.EventType.SCROLL
         self.assertRaises(AttributeError, lambda: getattr(event, 'foo_bar'))
 
-    @unittest.skipIf(Gdk_version == "4.0", "not in gdk4")
+    @unittest.skipIf(GDK4, "not in gdk4")
     def test_event_touch(self):
         event = Gdk.Event.new(Gdk.EventType.TOUCH_BEGIN)
         self.assertEqual(event.type, Gdk.EventType.TOUCH_BEGIN)
@@ -96,7 +137,7 @@ class TestGdk(unittest.TestCase):
         self.assertTrue(event.emulating_pointer)
         self.assertTrue(event.touch.emulating_pointer)
 
-    @unittest.skipIf(Gdk_version == "4.0", "not in gdk4")
+    @unittest.skipIf(GDK4, "not in gdk4")
     def test_event_setattr(self):
         event = Gdk.Event.new(Gdk.EventType.DRAG_MOTION)
         event.x_root, event.y_root = 0, 5
@@ -109,12 +150,19 @@ class TestGdk(unittest.TestCase):
         self.assertFalse(hasattr(event, "foo_bar"))
         event.foo_bar = 42
 
-    @unittest.skipIf(Gdk_version == "4.0", "not in gdk4")
+        # unhandled type
+        event.type = Gdk.EventType.EVENT_LAST
+        with pytest.raises(AttributeError):
+            event.foo_bar
+        event.foo_bar = 42
+        assert event.foo_bar == 42
+
+    @unittest.skipIf(GDK4, "not in gdk4")
     def test_event_repr(self):
         event = Gdk.Event.new(Gdk.EventType.CONFIGURE)
         self.assertTrue("CONFIGURE" in repr(event))
 
-    @unittest.skipIf(Gdk_version == "4.0", "not in gdk4")
+    @unittest.skipIf(GDK4, "not in gdk4")
     def test_event_structures(self):
         def button_press_cb(button, event):
             self.assertTrue(isinstance(event, Gdk.EventButton))
@@ -138,7 +186,7 @@ class TestGdk(unittest.TestCase):
                                  Gdk.ModifierType.CONTROL_MASK,
                                  Gdk.EventType.BUTTON_PRESS)
 
-    @unittest.skipIf(Gdk_version == "4.0", "not in gdk4")
+    @unittest.skipIf(GDK4, "not in gdk4")
     def test_cursor(self):
         self.assertEqual(Gdk.Cursor, gi.overrides.Gdk.Cursor)
         with capture_glib_deprecation_warnings():
@@ -170,6 +218,18 @@ class TestGdk(unittest.TestCase):
 
         self.assertRaises(ValueError, Gdk.Cursor, 1, 2, 3)
 
+        with capture_glib_deprecation_warnings() as warn:
+            c = Gdk.Cursor(display, Gdk.CursorType.WATCH)
+        assert len(warn) == 1
+        assert c.props.cursor_type == Gdk.CursorType.WATCH
+        assert c.props.display == display
+
+    @unittest.skipUnless(GDK4, "only gdk4")
+    def test_cursor_gdk4(self):
+        Gdk.Cursor()
+        Gdk.Cursor(name="foo")
+        Gdk.Cursor(fallback=Gdk.Cursor())
+
     def test_flags(self):
         self.assertEqual(Gdk.ModifierType.META_MASK | 0, 0x10000000)
         self.assertEqual(hex(Gdk.ModifierType.META_MASK), '0x10000000')
@@ -185,7 +245,7 @@ class TestGdk(unittest.TestCase):
         self.assertEqual(str(Gdk.ModifierType.RELEASE_MASK | Gdk.ModifierType.META_MASK),
                          '<flags GDK_META_MASK | GDK_RELEASE_MASK of type Gdk.ModifierType>')
 
-    @unittest.skipIf(Gdk_version == "4.0", "not in gdk4")
+    @unittest.skipIf(GDK4, "not in gdk4")
     def test_color_parse(self):
         with capture_glib_deprecation_warnings():
             c = Gdk.color_parse('#00FF80')
@@ -194,7 +254,7 @@ class TestGdk(unittest.TestCase):
         self.assertEqual(c.blue, 32896)
         self.assertEqual(Gdk.color_parse('bogus'), None)
 
-    @unittest.skipIf(Gdk_version == "4.0", "not in gdk4")
+    @unittest.skipIf(GDK4, "not in gdk4")
     def test_color_representations(self):
         # __repr__ should generate a string which is parsable when possible
         # http://docs.python.org/2/reference/datamodel.html#object.__repr__
@@ -204,6 +264,7 @@ class TestGdk(unittest.TestCase):
         rgba = Gdk.RGBA(red=1.0, green=0.8, blue=0.6, alpha=0.4)
         self.assertEqual(eval(repr(rgba)), rgba)
 
+    @unittest.skipIf(GDK4, "not in gdk4")
     def test_rectangle_functions(self):
         # https://bugzilla.gnome.org/show_bug.cgi?id=756364
         a = Gdk.Rectangle()
@@ -212,3 +273,9 @@ class TestGdk(unittest.TestCase):
         intersect, rect = Gdk.rectangle_intersect(a, b)
         self.assertTrue(isinstance(rect, Gdk.Rectangle))
         self.assertTrue(isinstance(intersect, bool))
+
+    @unittest.skipIf(GDK4, "not in gdk4")
+    def test_atom_repr_str(self):
+        atom = Gdk.atom_intern("", True)
+        assert re.match(r"<Gdk.Atom\(\d+\)>", repr(atom))
+        assert re.match(r"Gdk.Atom<\d+>", str(atom))


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]