[pygobject] Allow overrides in other directories than gi itself



commit b1a9848a7a7255e6b1ccd98712dd62b1514078b9
Author: Thibault Saunier <thibault saunier collabora com>
Date:   Tue Aug 21 07:54:09 2012 +0200

    Allow overrides in other directories than gi itself
    
    Use pkgutil.extend_path() for the gi and gi.overrides modules, so that
    libraries can install overrides in a path that is different from the one that
    pygobject installs itself into. These overrides need to put this into their
    __init__.py at the top:
    
        from pkgutil import extend_path
        __path__ = extend_path(__path__, __name__)
    
    and put themselves somewhere into the default PYTHONPATH.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=680913
    
    Co-Authored-By: Martin Pitt <martinpitt gnome org>
    Co-Authored-By: Simon Feltman <s feltman gmail com>

 gi/__init__.py                 |    4 ++++
 gi/overrides/__init__.py       |    4 ++++
 tests/gi/__init__.py           |    2 ++
 tests/gi/overrides/Regress.py  |   26 ++++++++++++++++++++++++++
 tests/gi/overrides/__init__.py |    2 ++
 tests/test_overrides.py        |    6 ++++++
 6 files changed, 44 insertions(+), 0 deletions(-)
---
diff --git a/gi/__init__.py b/gi/__init__.py
index b816612..6ebff8e 100644
--- a/gi/__init__.py
+++ b/gi/__init__.py
@@ -20,6 +20,10 @@
 
 from __future__ import absolute_import
 
+# support overrides in different directories than our gi module
+from pkgutil import extend_path
+__path__ = extend_path(__path__, __name__)
+
 from ._gi import _API, Repository
 
 # Force loading the GObject typelib so we have available the wrappers for
diff --git a/gi/overrides/__init__.py b/gi/overrides/__init__.py
index bdabb37..0e4c489 100644
--- a/gi/overrides/__init__.py
+++ b/gi/overrides/__init__.py
@@ -2,6 +2,10 @@ import types
 
 from gi import _gobject
 
+# support overrides in different directories than our gi module
+from pkgutil import extend_path
+__path__ = extend_path(__path__, __name__)
+
 registry = None
 
 
diff --git a/tests/gi/__init__.py b/tests/gi/__init__.py
new file mode 100644
index 0000000..3ad9513
--- /dev/null
+++ b/tests/gi/__init__.py
@@ -0,0 +1,2 @@
+from pkgutil import extend_path
+__path__ = extend_path(__path__, __name__)
diff --git a/tests/gi/overrides/Regress.py b/tests/gi/overrides/Regress.py
new file mode 100644
index 0000000..cb38631
--- /dev/null
+++ b/tests/gi/overrides/Regress.py
@@ -0,0 +1,26 @@
+# -*- Mode: Python; py-indent-offset: 4 -*-
+# vim: tabstop=4 shiftwidth=4 expandtab
+#
+# Copyright (C) 2012 Martin Pitt <martinpitt gnome org>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
+# USA
+
+from ..importer import modules
+
+Regress = modules['Regress']._introspection_module
+
+REGRESS_OVERRIDE = 42
+__all__ = ['REGRESS_OVERRIDE']
diff --git a/tests/gi/overrides/__init__.py b/tests/gi/overrides/__init__.py
new file mode 100644
index 0000000..3ad9513
--- /dev/null
+++ b/tests/gi/overrides/__init__.py
@@ -0,0 +1,2 @@
+from pkgutil import extend_path
+__path__ = extend_path(__path__, __name__)
diff --git a/tests/test_overrides.py b/tests/test_overrides.py
index 0ad8295..f6fcf5a 100644
--- a/tests/test_overrides.py
+++ b/tests/test_overrides.py
@@ -10,6 +10,7 @@ import gi.types
 from gi.repository import GLib
 from gi.repository import GObject
 from gi.repository import Gio
+from gi.repository import Regress
 
 try:
     from gi.repository import GdkPixbuf
@@ -45,6 +46,11 @@ class TestRegistry(unittest.TestCase):
         except TypeError as e:
             self.assertTrue('Can not override a type MyClass' in str(e))
 
+    def test_separate_path(self):
+        # Regress override is in tests/gi/overrides, separate from gi/overrides
+        # https://bugzilla.gnome.org/show_bug.cgi?id=680913
+        self.assertEqual(Regress.REGRESS_OVERRIDE, 42)
+
 
 class TestGLib(unittest.TestCase):
 



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