Skip to content

Commit

Permalink
Merge branch 'imp-fix'
Browse files Browse the repository at this point in the history
  • Loading branch information
al-025 committed Feb 14, 2024
2 parents 6f17dce + ca8ee3b commit 1838412
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions s2protocol/versions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@

import os
import re
import imp
import importlib
import sys


def _import_protocol(base_path, protocol_module_name):
"""
Import a module from a base path, used to import protocol modules.
This implementation is derived from the __import__ example here:
https://docs.python.org/2/library/imp.html
This implementation is derived from the import_module example here:
https://docs.python.org/3/library/importlib.html#approximating-importlib-import-module
"""

# Try to return the module if it's been loaded already
Expand All @@ -19,16 +19,21 @@ def _import_protocol(base_path, protocol_module_name):
except KeyError:
pass

# If any of the following calls raises an exception,
# there's a problem we can't handle -- let the caller handle it.
#
fp, pathname, description = imp.find_module(protocol_module_name, [base_path])
try:
return imp.load_module(protocol_module_name, fp, pathname, description)
finally:
# Since we may exit via an exception, close fp explicitly.
if fp:
fp.close()
# Otherwise try to load the module
spec = importlib.util.spec_from_file_location(
protocol_module_name,
os.path.join(base_path, protocol_module_name+'.py')
)
module = importlib.util.module_from_spec(spec)

# If the module does not exist, raise an exception and let the
# caller handle it
if( not os.path.exists(spec.origin) ):
raise ImportError("No module named '{}'".format(protocol_module_name))

sys.modules[protocol_module_name] = module
spec.loader.exec_module(module)
return module


def list_all(base_path=None):
Expand Down

0 comments on commit 1838412

Please sign in to comment.