Skip to content

Commit

Permalink
Replace use of deprecated package imp with current importlib (#189)
Browse files Browse the repository at this point in the history
* Replace use of deprecated package imp with current importlib

* Changes suggested by Fabian Ruffy

* Attempt to make black happy

* Remove confusing comments and unnecessary conversions to str

* Require at least Python 3.4 due to the importlib features used
  • Loading branch information
jafingerhut committed Jun 19, 2023
1 parent c458186 commit 771a452
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
16 changes: 11 additions & 5 deletions ptf
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import logging
import unittest
import time
import os
import imp
import importlib
import random
import signal
import fnmatch
Expand Down Expand Up @@ -104,6 +104,14 @@ config_default = {
}


def import_module(root_path, module_name):
"""Try to import a module and class directly instead of the typical
Python method. Allows for dynamic imports."""
finder = importlib.machinery.PathFinder()
module_specs = finder.find_spec(module_name, [root_path])
return module_specs.loader.load_module()


def config_setup():
"""
Set up the configuration including parsing the arguments
Expand Down Expand Up @@ -544,7 +552,7 @@ def load_test_modules(config):
if modname in sys.modules:
mod = sys.modules[modname]
else:
mod = imp.load_module(modname, *imp.find_module(modname, [root]))
mod = import_module(root, modname)
except:
logging.warning("Could not import file " + filename)
raise
Expand Down Expand Up @@ -809,9 +817,7 @@ if platform_name == "nn":

platform_mod = None
try:
platform_mod = imp.load_module(
platform_name, *imp.find_module(platform_name, [config["platform_dir"]])
)
platform_mod = import_module(config["platform_dir"], platform_name)
except:
logging.warn("Failed to import " + platform_name + " platform module")
raise
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package_dir=
packages = find:
scripts = ptf, ptf_nn/ptf_nn_agent.py
platforms = any
python_requires = >=3
python_requires = >=3.4
setup_requires =
setuptools_scm
install_requires = file: requirements.txt
Expand Down

0 comments on commit 771a452

Please sign in to comment.