def test_require_version(self): self.assertTrue(require_version("Gtk", "3.0") is None) self.assertRaises(ValueError, require_version, "Gtk", "4.0") self.assertRaises(ValueError, require_version, "Gtk", "2.0") self.assertEqual(get_required_version("Gtk"), "3.0") self.assertTrue(get_required_version("...") is None) self.assertRaises(ValueError, require_version, "...", "3.0")
def _load(self): version = gi.get_required_version(self._namespace) self._introspection_module = IntrospectionModule(self._namespace, version) overrides_modules = __import__('gi.overrides', fromlist=[self._namespace]) self._overrides_module = getattr(overrides_modules, self._namespace, None) self.__path__ = repository.get_typelib_path(self._namespace)
def _load(self): version = gi.get_required_version(self._namespace) self._introspection_module = IntrospectionModule( self._namespace, version) overrides_modules = __import__('gi.overrides', fromlist=[self._namespace]) self._overrides_module = getattr(overrides_modules, self._namespace, None) self.__path__ = repository.get_typelib_path(self._namespace)
def _load(self): version = gi.get_required_version(self._namespace) self._introspection_module = IntrospectionModule(self._namespace, version) overrides_modules = __import__('gi.overrides', fromlist=[self._namespace]) self._overrides_module = getattr(overrides_modules, self._namespace, None) self.__path__ = repository.get_typelib_path(self._namespace) if _have_py3: # get_typelib_path() delivers bytes, not a string self.__path__ = self.__path__.decode('UTF-8')
def get_introspection_module(namespace): """ :Returns: An object directly wrapping the gi module without overrides. """ if namespace in _introspection_modules: return _introspection_modules[namespace] version = gi.get_required_version(namespace) module = IntrospectionModule(namespace, version) _introspection_modules[namespace] = module return module
def _check_require_version(namespace, stacklevel): """A context manager which tries to give helpful warnings about missing gi.require_version() which could potentially break code if only an older version than expected is installed or a new version gets introduced. :: with _check_require_version("Gtk", stacklevel): load_namespace_and_overrides() """ global _active_imports, _implicit_required # This keeps track of the recursion level so we only check for # explicitly imported namespaces and not the ones imported in overrides _active_imports.append(namespace) try: yield except: raise else: # Keep track of all dependency versions forced due to this import, so # we don't warn for them in the future. This mirrors the import # behavior where importing will get an older version if a previous # import depended on it. for dependency in _get_all_dependencies(namespace): ns, version = dependency.split("-", 1) _implicit_required[ns] = version finally: _active_imports.remove(namespace) # Warn in case: # * this namespace was explicitly imported # * the version wasn't forced using require_version() # * the version wasn't forced implicitly by a previous import # * this namespace isn't part of glib (we have bigger problems if # versions change there) is_explicit_import = not _active_imports version_required = gi.get_required_version(namespace) is not None version_implicit = namespace in _implicit_required is_in_glib = namespace in ("GLib", "GObject", "Gio") if is_explicit_import and not version_required and not version_implicit and not is_in_glib: version = repository.get_version(namespace) warnings.warn( "%(namespace)s was imported without specifying a version first. " "Use gi.require_version('%(namespace)s', '%(version)s') before " "import to ensure that the right version gets loaded." % {"namespace": namespace, "version": version}, ImportWarning, stacklevel=stacklevel, )
def _load(self): version = gi.get_required_version(self._namespace) self._introspection_module = IntrospectionModule( self._namespace, version) overrides_modules = __import__('gi.overrides', fromlist=[self._namespace]) self._overrides_module = getattr(overrides_modules, self._namespace, None) self.__path__ = repository.get_typelib_path(self._namespace) if _have_py3: # get_typelib_path() delivers bytes, not a string self.__path__ = self.__path__.decode('UTF-8')
def get_matplotlib_gtk_backend(): import gi required = gi.get_required_version("Gtk") if required == "4.0": versions = [4] elif required == "3.0": versions = [3] elif os.environ.get("_GTK_API"): # Private undocumented API. versions = [int(os.environ["_GTK_API"])] else: versions = [4, 3] for version in versions: # Matplotlib converts require_version ValueErrors into ImportErrors. try: mod = importlib.import_module( f"matplotlib.backends.backend_gtk{version}") return mod, getattr(mod, f"_BackendGTK{version}") except ImportError: pass raise ImportError("Failed to import any Matplotlib GTK backend")
def _check_require_version(namespace, stacklevel): """A context manager which tries to give helpful warnings about missing gi.require_version() which could potentially break code if only an older version than expected is installed or a new version gets introduced. :: with _check_require_version("Gtk", stacklevel): load_namespace_and_overrides() """ was_loaded = repository.is_registered(namespace) yield if was_loaded: # it was loaded before by another import which depended on this # namespace or by C code like libpeas return if namespace in ("GLib", "GObject", "Gio"): # part of glib (we have bigger problems if versions change there) return if gi.get_required_version(namespace) is not None: # the version was forced using require_version() return version = repository.get_version(namespace) warnings.warn( "%(namespace)s was imported without specifying a version first. " "Use gi.require_version('%(namespace)s', '%(version)s') before " "import to ensure that the right version gets loaded." % { "namespace": namespace, "version": version }, PyGIWarning, stacklevel=stacklevel)
def _check_require_version(namespace, stacklevel): """A context manager which tries to give helpful warnings about missing gi.require_version() which could potentially break code if only an older version than expected is installed or a new version gets introduced. :: with _check_require_version("Gtk", stacklevel): load_namespace_and_overrides() """ was_loaded = repository.is_registered(namespace) yield if was_loaded: # it was loaded before by another import which depended on this # namespace or by C code like libpeas return if namespace in ("GLib", "GObject", "Gio"): # part of glib (we have bigger problems if versions change there) return if gi.get_required_version(namespace) is not None: # the version was forced using require_version() return version = repository.get_version(namespace) warnings.warn( "%(namespace)s was imported without specifying a version first. " "Use gi.require_version('%(namespace)s', '%(version)s') before " "import to ensure that the right version gets loaded." % {"namespace": namespace, "version": version}, PyGIWarning, stacklevel=stacklevel, )