[pygobject] gtktemplate: Don't error out when loading a resource that is only available in an overlay. Fixes #23



commit c78557c5185bb8f50f4a7b0ddf1581a7107f7ca5
Author: Christoph Reiter <reiter christoph gmail com>
Date:   Fri Jul 20 10:24:10 2018 +0200

    gtktemplate: Don't error out when loading a resource that is only available in an overlay. Fixes #230
    
    g_resources_get_info() doesn't do overlays but we use it to verify that the resource
    exists. See https://gitlab.gnome.org/GNOME/glib/issues/1445
    
    In case g_resources_get_info() errors out check g_resources_lookup_data() before giving up.
    
    Because glib reads the overlay paths only once at start this is hard to add tests for.

 gi/_gtktemplate.py | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
---
diff --git a/gi/_gtktemplate.py b/gi/_gtktemplate.py
index a345f5d2..efaca339 100644
--- a/gi/_gtktemplate.py
+++ b/gi/_gtktemplate.py
@@ -145,6 +145,18 @@ class Callback(object):
         return CallThing(self._name, func)
 
 
+def validate_resource_path(path):
+    """Raises GLib.Error in case the resource doesn't exist"""
+
+    try:
+        Gio.resources_get_info(path, Gio.ResourceLookupFlags.NONE)
+    except GLib.Error:
+        # resources_get_info() doesn't handle overlays but we keep using it
+        # as a fast path.
+        # https://gitlab.gnome.org/GNOME/pygobject/issues/230
+        Gio.resources_lookup_data(path, Gio.ResourceLookupFlags.NONE)
+
+
 class Template(object):
 
     def __init__(self, **kwargs):
@@ -204,8 +216,7 @@ class Template(object):
             register_template(cls)
             return cls
         elif self.resource_path is not None:
-            Gio.resources_get_info(
-                self.resource_path, Gio.ResourceLookupFlags.NONE)
+            validate_resource_path(self.resource_path)
             cls.set_template_from_resource(self.resource_path)
             register_template(cls)
             return cls


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