Skip to content

Commit

Permalink
Replace uses of deprecated imp with importlib (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlocke committed Jul 19, 2023
1 parent 82ee5e3 commit af56116
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
24 changes: 19 additions & 5 deletions stone/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from __future__ import absolute_import, division, print_function, unicode_literals

import codecs
import imp # pylint: disable=deprecated-module
import importlib
import io
import json
import logging
Expand Down Expand Up @@ -137,10 +137,8 @@
help='If set, backends will not see any routes for the specified namespaces.',
)


def main():
"""The entry point for the program."""

if '--' in sys.argv:
cli_args = sys.argv[1:sys.argv.index('--')]
backend_args = sys.argv[sys.argv.index('--') + 1:]
Expand Down Expand Up @@ -169,7 +167,8 @@ def main():
# The module should should contain an api variable that references a
# :class:`stone.api.Api` object.
try:
api = imp.load_source('api', args.api[0]).api # pylint: disable=redefined-outer-name
api_module = _load_module('api', args.api[0])
api = api_module.api # pylint: disable=redefined-outer-name
except ImportError as e:
print('error: Could not import API description due to:',
e, file=sys.stderr)
Expand Down Expand Up @@ -342,7 +341,7 @@ def main():
if new_python_path not in sys.path:
sys.path.append(new_python_path)
try:
backend_module = imp.load_source('user_backend', args.backend)
backend_module = _load_module('user_backend', args.backend)
except Exception:
print("error: Importing backend '%s' module raised an exception:" %
args.backend, file=sys.stderr)
Expand All @@ -368,6 +367,21 @@ def main():
# easier to do debugging.
return api

def _load_module(name, path):
file_name = os.path.basename(path)
module_name = file_name.replace('.', '_')

if sys.version_info[0] == 3 and sys.version_info[1] >= 5:
module_specs = importlib.util.spec_from_file_location(module_name, path)
module = importlib.util.module_from_spec(module_specs)
module_specs.loader.exec_module(module)
else:
loader = importlib.machinery.SourceFileLoader(module_name, path)
module = loader.load_module() # pylint: disable=deprecated-method,no-value-for-parameter

sys.modules[name] = module

This comment has been minimized.

Copy link
@devPalacio

devPalacio Jul 20, 2023

Contributor

this needs to be sys.modules[module_name] = module


return module

if __name__ == '__main__':
# Assign api variable for easy debugging from a Python console
Expand Down
2 changes: 1 addition & 1 deletion stone/ir/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from collections import OrderedDict
# See <https://github.com/PyCQA/pylint/issues/73>
from distutils.version import StrictVersion
from distutils.version import StrictVersion # pylint: disable=deprecated-module
import six

from .data_types import (
Expand Down

0 comments on commit af56116

Please sign in to comment.