From 9c14eb873b5d8029a2d221a09852ef824f3be726 Mon Sep 17 00:00:00 2001 From: Florian Apolloner Date: Mon, 11 Mar 2024 13:16:05 +0100 Subject: [PATCH] Implement Python 3.12 compatible loader. Fixes #130. --- src/qrc_importer.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/qrc_importer.py b/src/qrc_importer.py index 1a4a123..db42fbd 100644 --- a/src/qrc_importer.py +++ b/src/qrc_importer.py @@ -16,11 +16,33 @@ # import sys +from importlib import abc +from importlib.util import spec_from_loader + import pyotherside -from importlib import abc + +class PyOtherSideQtRCLoader(abc.Loader): + def __init__(self, filepath): + self.filepath = filepath + + def create_module(self, spec): + return + + def exec_module(self, module): + data = pyotherside.qrc_get_file_contents(self.filepath[len('qrc:') :]) + code = compile(data, self.filepath, 'exec') + exec(code, module.__dict__) + class PyOtherSideQtRCImporter(abc.MetaPathFinder, abc.SourceLoader): + def find_spec(self, fullname, path, target=None): + if path is None: + fname = self.get_filename(fullname) + if fname: + return spec_from_loader(fullname, PyOtherSideQtRCLoader(fname)) + return + def find_module(self, fullname, path): if path is None or all(x.startswith('qrc:') for x in path): if self.get_filename(fullname):