Use importlib to take place of imp module
The imp module is deprecated[1] since version 3.4, use importlib to instead [1]: https://docs.python.org/3/library/imp.html Change-Id: Ifb44be3b0a0c35dd99d0230e9c371dfcb97f6fdd
This commit is contained in:
@@ -21,7 +21,7 @@ Command-line interface to the OpenStack Trove API.
|
||||
import argparse
|
||||
import getpass
|
||||
import glob
|
||||
import imp
|
||||
import importlib
|
||||
import itertools
|
||||
import logging
|
||||
import os
|
||||
@@ -299,6 +299,14 @@ class OpenStackTroveShell(object):
|
||||
|
||||
yield name, module
|
||||
|
||||
def _load_module(self, name, path):
|
||||
module_spec = importlib.spec_from_file_location(
|
||||
name, path
|
||||
)
|
||||
module = importlib.module_from_spec(module_spec)
|
||||
module_spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
def _discover_via_contrib_path(self, version):
|
||||
module_path = os.path.dirname(os.path.abspath(__file__))
|
||||
version_str = "v%s" % version.replace('.', '_')
|
||||
@@ -312,7 +320,7 @@ class OpenStackTroveShell(object):
|
||||
if name == "__init__":
|
||||
continue
|
||||
|
||||
module = imp.load_source(name, ext_path)
|
||||
module = self._load_module(name, ext_path)
|
||||
yield name, module
|
||||
|
||||
def _discover_via_entry_points(self):
|
||||
|
@@ -13,8 +13,8 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import imp
|
||||
import inspect
|
||||
import types
|
||||
from unittest import mock
|
||||
|
||||
import pkg_resources
|
||||
@@ -31,7 +31,7 @@ class DiscoverTest(testtools.TestCase):
|
||||
if group == 'troveclient.extension':
|
||||
fake_ep = mock.Mock()
|
||||
fake_ep.name = 'foo'
|
||||
fake_ep.module = imp.new_module('foo')
|
||||
fake_ep.module = types.ModuleType('foo')
|
||||
fake_ep.load.return_value = fake_ep.module
|
||||
return [fake_ep]
|
||||
|
||||
@@ -48,13 +48,13 @@ class DiscoverTest(testtools.TestCase):
|
||||
def test_discover_extensions(self):
|
||||
|
||||
def mock_discover_via_python_path(self):
|
||||
yield 'foo', imp.new_module('foo')
|
||||
yield 'foo', types.ModuleType('foo')
|
||||
|
||||
def mock_discover_via_contrib_path(self, version):
|
||||
yield 'bar', imp.new_module('bar')
|
||||
yield 'bar', types.ModuleType('bar')
|
||||
|
||||
def mock_discover_via_entry_points(self):
|
||||
yield 'baz', imp.new_module('baz')
|
||||
yield 'baz', types.ModuleType('baz')
|
||||
|
||||
@mock.patch.object(troveclient.shell.OpenStackTroveShell,
|
||||
'_discover_via_python_path',
|
||||
|
Reference in New Issue
Block a user