mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2025-01-23 15:48:23 +00:00
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>
This commit is contained in:
parent
2748805bbe
commit
865b9b59a5
3 changed files with 39 additions and 54 deletions
|
@ -37,7 +37,7 @@ def load(paths=()):
|
|||
|
||||
def _load_plugins(path):
|
||||
|
||||
import imp
|
||||
import importlib
|
||||
import glob
|
||||
|
||||
files = glob.glob(os.path.join(path, "*.py"))
|
||||
|
@ -47,8 +47,16 @@ def _load_plugins(path):
|
|||
name = os.path.basename(os.path.splitext(filename)[0])
|
||||
if name == "__init__":
|
||||
continue
|
||||
fp, pathname, description = imp.find_module(name, [path])
|
||||
module = imp.load_module(name, fp, pathname, description)
|
||||
|
||||
finder = importlib.machinery.PathFinder()
|
||||
spec = finder.find_spec(
|
||||
name,
|
||||
os.environ.get('_GI_OVERRIDES_PATH', '').split(os.pathsep)
|
||||
)
|
||||
if spec is None:
|
||||
raise ModuleNotFoundError(name)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
|
||||
yield module
|
||||
|
||||
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
import gi
|
||||
import os
|
||||
import sys
|
||||
import imp
|
||||
import importlib.util
|
||||
from importlib.machinery import PathFinder
|
||||
from pathlib import Path
|
||||
|
||||
# Remove this dummy module the python path and
|
||||
|
@ -12,34 +13,17 @@ import gi
|
|||
|
||||
|
||||
class GstOverrideImport:
|
||||
def find_module(self, fullname, path=None, target=None):
|
||||
if fullname.startswith('gi.overrides'):
|
||||
fp = None
|
||||
try:
|
||||
fp, _, _ = imp.find_module(fullname.split(
|
||||
'.')[-1], os.environ.get('_GI_OVERRIDES_PATH', '').split(os.pathsep),)
|
||||
except ImportError:
|
||||
def find_spec(self, fullname, path, target=None):
|
||||
if not fullname.startswith("gi.overrides"):
|
||||
return None
|
||||
finally:
|
||||
if fp:
|
||||
fp.close()
|
||||
return self
|
||||
return None
|
||||
|
||||
def load_module(self, name):
|
||||
if name in sys.modules:
|
||||
return sys.modules[name]
|
||||
|
||||
fp, pathname, description = imp.find_module(name.split(
|
||||
'.')[-1], os.environ.get('_GI_OVERRIDES_PATH', '').split(os.pathsep),)
|
||||
|
||||
try:
|
||||
module = imp.load_module(name, fp, pathname, description)
|
||||
finally:
|
||||
if fp:
|
||||
fp.close()
|
||||
sys.modules[name] = module
|
||||
return module
|
||||
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())
|
||||
|
|
|
@ -1,29 +1,22 @@
|
|||
import os
|
||||
import sys
|
||||
import imp
|
||||
import importlib
|
||||
|
||||
class GstOverrideImport:
|
||||
def find_module(self, fullname, path=None):
|
||||
if fullname in ('gi.overrides.Gst', 'gi.overrides._gi_gst'):
|
||||
return self
|
||||
def find_spec(self, fullname, path, target=None):
|
||||
if not (fullname.startswith("gi.overrides.Gst") or fullname.startswith("gi.overrides._gi_gst")):
|
||||
return None
|
||||
|
||||
def load_module(self, name):
|
||||
if name in sys.modules:
|
||||
return sys.modules[name]
|
||||
|
||||
fp, pathname, description = imp.find_module(name.split('.')[-1], [
|
||||
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('GST_OVERRIDE_SRC_PATH'),
|
||||
os.environ.get('GST_OVERRIDE_BUILD_PATH'),
|
||||
])
|
||||
|
||||
try:
|
||||
module = imp.load_module(name, fp, pathname, description)
|
||||
finally:
|
||||
if fp:
|
||||
fp.close()
|
||||
sys.modules[name] = module
|
||||
return module
|
||||
]
|
||||
)
|
||||
return spec
|
||||
|
||||
if sys.version_info.major >= 3:
|
||||
sys.meta_path.insert(0, GstOverrideImport())
|
||||
|
|
Loading…
Reference in a new issue