gstreamer/subprojects/gst-python/gi/__init__.py
Edward Hervey 865b9b59a5 gst-python: Fix override loading in python >= 3.12
The `imp` module got removed in python 3.12 and the `importlib` module should be
used instead.

This is also a good excuse to switch to the new finder module from PEP 451 :
https://www.python.org/dev/peps/pep-0451/

This only requires implement the `find_spec()` method in our custom loaders

Co-authored-by: Stefan <107316-stefan6419846@users.noreply.gitlab.freedesktop.org>
Co-authored-by: Jordan Petrids <jordan@centricular.com>
Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/6050>
2024-02-04 19:15:27 +00:00

29 lines
831 B
Python

import gi
import os
import sys
import importlib.util
from importlib.machinery import PathFinder
from pathlib import Path
# Remove this dummy module the python path and
# try to import the actual gi module
sys.path.remove(str(Path(__file__).parents[1]))
del sys.modules["gi"]
import gi
class GstOverrideImport:
def find_spec(self, fullname, path, target=None):
if not fullname.startswith("gi.overrides"):
return None
finder = importlib.machinery.PathFinder()
# From find_spec the docs:
# If name is for a submodule (contains a dot), the parent module is automatically imported.
spec = finder.find_spec(
fullname,
os.environ.get('_GI_OVERRIDES_PATH', '').split(os.pathsep)
)
return spec
sys.meta_path.insert(0, GstOverrideImport())