| 1 |
import sys |
| 2 |
import unittest |
| 3 |
|
| 4 |
import gi.overrides |
| 5 |
import gi.module |
| 6 |
import gi.importer |
| 7 |
|
| 8 |
from gi.repository import Regress |
| 9 |
|
| 10 |
|
| 11 |
class TestOverrides(unittest.TestCase): |
| 12 |
def test_non_gi(self): |
| 13 |
class MyClass: |
| 14 |
pass |
| 15 |
|
| 16 |
try: |
| 17 |
gi.overrides.override(MyClass) |
| 18 |
self.fail("unexpected success of overriding non-GI class") |
| 19 |
except TypeError as e: |
| 20 |
self.assertTrue("Can not override a type MyClass" in str(e)) |
| 21 |
|
| 22 |
def test_separate_path(self): |
| 23 |
# Regress override is in tests/gi/overrides, separate from gi/overrides |
| 24 |
# https://bugzilla.gnome.org/show_bug.cgi?id=680913 |
| 25 |
self.assertEqual(Regress.REGRESS_OVERRIDE, 42) |
| 26 |
|
| 27 |
def test_load_overrides(self): |
| 28 |
mod = gi.module.get_introspection_module("GIMarshallingTests") |
| 29 |
mod_override = gi.overrides.load_overrides(mod) |
| 30 |
self.assertTrue(mod_override is not mod) |
| 31 |
self.assertTrue(mod_override._introspection_module is mod) |
| 32 |
self.assertEqual(mod_override.OVERRIDES_CONSTANT, 7) |
| 33 |
self.assertEqual(mod.OVERRIDES_CONSTANT, 42) |
| 34 |
|
| 35 |
def test_load_no_overrides(self): |
| 36 |
mod_key = "gi.overrides.GIMarshallingTests" |
| 37 |
had_mod = mod_key in sys.modules |
| 38 |
old_mod = sys.modules.get(mod_key) |
| 39 |
try: |
| 40 |
# this makes override import fail |
| 41 |
sys.modules[mod_key] = None |
| 42 |
mod = gi.module.get_introspection_module("GIMarshallingTests") |
| 43 |
mod_override = gi.overrides.load_overrides(mod) |
| 44 |
self.assertTrue(mod_override is mod) |
| 45 |
finally: |
| 46 |
del sys.modules[mod_key] |
| 47 |
if had_mod: |
| 48 |
sys.modules[mod_key] = old_mod |
| 49 |
|
| 50 |
|
| 51 |
class TestModule(unittest.TestCase): |
| 52 |
# Tests for gi.module |
| 53 |
|
| 54 |
def test_get_introspection_module_caching(self): |
| 55 |
# This test attempts to minimize side effects by |
| 56 |
# using a DynamicModule directly instead of going though: |
| 57 |
# from gi.repository import Foo |
| 58 |
|
| 59 |
# Clear out introspection module cache before running this test. |
| 60 |
old_modules = gi.module._introspection_modules |
| 61 |
gi.module._introspection_modules = {} |
| 62 |
|
| 63 |
mod_name = "GIMarshallingTests" |
| 64 |
mod1 = gi.module.get_introspection_module(mod_name) |
| 65 |
mod2 = gi.module.get_introspection_module(mod_name) |
| 66 |
self.assertTrue(mod1 is mod2) |
| 67 |
|
| 68 |
# Restore the previous cache |
| 69 |
gi.module._introspection_modules = old_modules |
| 70 |
|
| 71 |
def test_module_dependency_loading(self): |
| 72 |
# Difficult to because this generally need to run in isolation to make |
| 73 |
# sure GIMarshallingTests has not yet been loaded. But we can do this with: |
| 74 |
# make check TEST_NAMES=test_import_machinery.TestModule.test_module_dependency_loading |
| 75 |
if "gi.repository.Gio" in sys.modules: |
| 76 |
return |
| 77 |
|
| 78 |
from gi.repository import GIMarshallingTests |
| 79 |
|
| 80 |
GIMarshallingTests |
| 81 |
|
| 82 |
self.assertIn("gi.repository.Gio", sys.modules) |
| 83 |
self.assertIn("gi.repository.GIMarshallingTests", sys.modules) |
| 84 |
|
| 85 |
def test_static_binding_protection(self): |
| 86 |
# Importing old static bindings once gi has been imported should not |
| 87 |
# crash but instead give back a dummy module which produces RuntimeErrors |
| 88 |
# on access. |
| 89 |
with self.assertRaises(AttributeError): |
| 90 |
import gobject |
| 91 |
|
| 92 |
gobject.anything |
| 93 |
|
| 94 |
with self.assertRaises(AttributeError): |
| 95 |
import glib |
| 96 |
|
| 97 |
glib.anything |
| 98 |
|
| 99 |
with self.assertRaises(AttributeError): |
| 100 |
import gio |
| 101 |
|
| 102 |
gio.anything |
| 103 |
|
| 104 |
with self.assertRaises(AttributeError): |
| 105 |
import gtk |
| 106 |
|
| 107 |
gtk.anything |
| 108 |
|
| 109 |
with self.assertRaises(AttributeError): |
| 110 |
import gtk.gdk |
| 111 |
|
| 112 |
gtk.gdk.anything |
| 113 |
|
| 114 |
|
| 115 |
class TestImporter(unittest.TestCase): |
| 116 |
def test_invalid_repository_module_name(self): |
| 117 |
with self.assertRaises(ImportError) as context: |
| 118 |
from gi.repository import InvalidGObjectRepositoryModuleName |
| 119 |
|
| 120 |
InvalidGObjectRepositoryModuleName |
| 121 |
|
| 122 |
exception_string = str(context.exception) |
| 123 |
|
| 124 |
self.assertTrue("InvalidGObjectRepositoryModuleName" in exception_string) |
| 125 |
self.assertTrue("introspection typelib" in exception_string) |
| 126 |
|
| 127 |
def test_require_version_warning(self): |
| 128 |
check = gi.importer._check_require_version |
| 129 |
|
| 130 |
# make sure it doesn't fail at least |
| 131 |
with check("GLib", 1): |
| 132 |
from gi.repository import GLib |
| 133 |
|
| 134 |
GLib |
| 135 |
|
| 136 |
# make sure the exception propagates |
| 137 |
with ( |
| 138 |
self.assertRaises(ImportError), |
| 139 |
check("InvalidGObjectRepositoryModuleName", 1), |
| 140 |
): |
| 141 |
from gi.repository import InvalidGObjectRepositoryModuleName |
| 142 |
|
| 143 |
InvalidGObjectRepositoryModuleName |
| 144 |
|
| 145 |
def test_require_version_versiontype(self): |
| 146 |
import gi |
| 147 |
|
| 148 |
with self.assertRaises(ValueError): |
| 149 |
gi.require_version("GLib", 2.0) |
| 150 |
|
| 151 |
with self.assertRaises(ValueError): |
| 152 |
gi.require_version("GLib", b"2.0") |
| 153 |
|
| 154 |
def test_require_versions(self): |
| 155 |
import gi |
| 156 |
|
| 157 |
gi.require_versions({"GLib": "2.0", "Gio": "2.0", "GObject": "2.0"}) |
| 158 |
from gi.repository import GLib |
| 159 |
|
| 160 |
GLib |
| 161 |
|
| 162 |
def test_get_import_stacklevel(self): |
| 163 |
gi.importer.get_import_stacklevel(import_hook=True) |
| 164 |
gi.importer.get_import_stacklevel(import_hook=False) |