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

DeprecationWarning: the load_module() method is deprecated and slated for removal in Python 3.12; use exec_module() instead #2576

Merged
merged 2 commits into from
Feb 5, 2024
Merged
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
17 changes: 14 additions & 3 deletions locust/util/load_locustfile.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import importlib
import importlib.util
import inspect
import os
import sys
Expand Down Expand Up @@ -53,9 +54,19 @@ def load_locustfile(path) -> tuple[str | None, dict[str, User], list[LoadTestSha
# Add to front, then remove from original position
sys.path.insert(0, directory)
del sys.path[i + 1]

# Perform the import
source = importlib.machinery.SourceFileLoader(os.path.splitext(locustfile)[0], path)
imported = source.load_module()
module_name = os.path.splitext(locustfile)[0]
loader = importlib.machinery.SourceFileLoader(module_name, path)
spec = importlib.util.spec_from_file_location(module_name, path, loader=loader)
if spec is None:
sys.stderr.write(f"Unable to get module spec for {module_name} in {path}")
sys.exit(1)

imported = importlib.util.module_from_spec(spec)
sys.modules[imported.__name__] = imported
loader.exec_module(imported)

# Remove directory from path if we added it ourselves (just to be neat)
if added_to_path:
del sys.path[0]
Expand All @@ -67,6 +78,6 @@ def load_locustfile(path) -> tuple[str | None, dict[str, User], list[LoadTestSha
user_classes = {name: value for name, value in vars(imported).items() if is_user_class(value)}

# Find shape class, if any, return it
shape_classes = [value() for name, value in vars(imported).items() if is_shape_class(value)]
shape_classes = [value() for value in vars(imported).values() if is_shape_class(value)]

return imported.__doc__, user_classes, shape_classes
Loading