From 9e537edd148b33c87b3c8ddbaa9f68a1429b13dd Mon Sep 17 00:00:00 2001 From: AN Long Date: Sun, 26 Nov 2023 16:56:46 +0800 Subject: [PATCH 1/2] make the import hook compatible with python3.12 --- .gitignore | 2 ++ thriftpy2/hook.py | 52 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 4f133944..58f2d905 100644 --- a/.gitignore +++ b/.gitignore @@ -49,3 +49,5 @@ env/ pyvenv.cfg share/* + +venv diff --git a/thriftpy2/hook.py b/thriftpy2/hook.py index fd353869..12c85d67 100644 --- a/thriftpy2/hook.py +++ b/thriftpy2/hook.py @@ -3,25 +3,49 @@ from __future__ import absolute_import import sys +import importlib.abc +import importlib.util +import types from .parser import load_module -class ThriftImporter(object): - def __init__(self, extension="_thrift"): - self.extension = extension - def __eq__(self, other): - return self.__class__.__module__ == other.__class__.__module__ and \ - self.__class__.__name__ == other.__class__.__name__ and \ - self.extension == other.extension +# TODO: The load process does not compatible with Python standard, e.g., if the +# specified thrift file does not exists, it raises FileNotFoundError, and skiped +# the other meta finders in the sys.meta_path. +if sys.version_info >= (3, 4): + class ThriftImporter(importlib.abc.MetaPathFinder): + def __init__(self, extension="_thrift"): + self.extension = extension - def find_module(self, fullname, path=None): - if fullname.endswith(self.extension): - return self + def find_spec(self, fullname, path, target=None): + if not fullname.endswith(self.extension): + return None + return importlib.util.spec_from_loader(fullname, + ThriftLoader(fullname)) - def load_module(self, fullname): - return load_module(fullname) + + class ThriftLoader(importlib.abc.Loader): + def __init__(self, fullname): + self.fullname = fullname + + def create_module(self, spec): + return load_module(self.fullname) + + def exec_module(self, module): + ... +else: + class ThriftImporter(object): + def __init__(self, extension="_thrift"): + self.extension = extension + + def find_module(self, fullname, path=None): + if fullname.endswith(self.extension): + return self + + def load_module(self, fullname): + return load_module(fullname) _imp = ThriftImporter() @@ -29,9 +53,9 @@ def load_module(self, fullname): def install_import_hook(): global _imp - sys.meta_path[:] = [x for x in sys.meta_path if _imp != x] + [_imp] + sys.meta_path[:] = [x for x in sys.meta_path if _imp is not x] + [_imp] def remove_import_hook(): global _imp - sys.meta_path[:] = [x for x in sys.meta_path if _imp != x] + sys.meta_path[:] = [x for x in sys.meta_path if _imp is not x] From be219f01c6b7379393f0150f98cade4b12ce3777 Mon Sep 17 00:00:00 2001 From: AN Long Date: Mon, 27 Nov 2023 17:54:20 +0800 Subject: [PATCH 2/2] Update thriftpy2/hook.py --- thriftpy2/hook.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/thriftpy2/hook.py b/thriftpy2/hook.py index 12c85d67..2ae383b3 100644 --- a/thriftpy2/hook.py +++ b/thriftpy2/hook.py @@ -34,7 +34,7 @@ def create_module(self, spec): return load_module(self.fullname) def exec_module(self, module): - ... + pass else: class ThriftImporter(object): def __init__(self, extension="_thrift"):