Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hooks: Use load_hook to support python >= 3.12 | < 3.5 #746

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion trackma/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,18 @@ def start(self):
if os.path.isdir(hooks_dir):
import pkgutil

try:
from importlib.util import module_from_spec

def load_hook(loader, name):
spec = loader.find_spec(name)
module = module_from_spec(spec)
spec.loader.exec_module(module)
return module
except ImportError:
def load_hook(loader, name):
return loader.find_module(name).load_module(name)
Comment on lines +315 to +317
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trackma currently requires Python 3.8, so we don't need this fallback code for <3.5 (which is ancient).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm stuck in implementing this here... loader.find_module().load_module() allowed importing sibling packages, but newer one doesn't... so I'm stuck on this... Is there any way to do this without resorting to sys.path modification?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guess I'll have to take a look at testing this myself. No promises when I'll get to that, though.


self.msg.info("Importing user hooks...")
for loader, name, ispkg in pkgutil.iter_modules([hooks_dir]):
# List all the hook files in the hooks folder, import them
Expand All @@ -312,7 +324,8 @@ def start(self):
# for later calls.
try:
self.msg.debug("Importing hook {}...".format(name))
module = loader.find_module(name).load_module(name)
module = load_hook(loader, name)

if hasattr(module, 'init'):
module.init(self)
self.hooks_available.append(module)
Expand Down