Skip to content

Commit

Permalink
Merge pull request #230 from aisk/modern-import-hook
Browse files Browse the repository at this point in the history
make the import hook compatible with python3.12
  • Loading branch information
ethe authored Nov 28, 2023
2 parents a7b17e0 + be219f0 commit 9a2553a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@ env/

pyvenv.cfg
share/*

venv
52 changes: 38 additions & 14 deletions thriftpy2/hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,59 @@
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):
pass
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()


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]

0 comments on commit 9a2553a

Please sign in to comment.