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

Migrate imp to imporlib #119

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
14 changes: 12 additions & 2 deletions pyutilib/component/loader/plugin_importLoader.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
__all__ = ['ImportLoader']

from glob import glob
import imp
import importlib.util
import importlib.machinery
import re
import os
import sys
Expand All @@ -22,6 +23,15 @@
from pyutilib.component.config import ManagedSingletonPlugin
from pyutilib.component.core import implements, ExtensionPoint, IIgnorePluginWhenLoading, IPluginLoader, Plugin

def load_source(modname, filename):
loader = importlib.machinery.SourceFileLoader(modname, filename)
spec = importlib.util.spec_from_file_location(modname, filename, loader=loader)
module = importlib.util.module_from_spec(spec)
# The module is always executed and not cached in sys.modules.
# Uncomment the following line to cache the module.
# sys.modules[module.__name__] = module
loader.exec_module(module)
return module

class ImportLoader(ManagedSingletonPlugin):
"""Loader that looks for Python source files in the plugins directories,
Expand Down Expand Up @@ -53,7 +63,7 @@ def load(self, env, search_path, disable_re, name_re):
if plugin_name not in sys.modules and name_re.match(
plugin_name):
try:
module = imp.load_source(plugin_name, plugin_file)
module = load_source(plugin_name, plugin_file)
if generate_debug_messages:
env.log.debug('Loading file plugin %s from %s' % \
(plugin_name, plugin_file))
Expand Down
22 changes: 17 additions & 5 deletions pyutilib/misc/import_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
# _________________________________________________________________________

import os
import imp
import importlib
import importlib.machinery
import sys

import pyutilib.common
Expand All @@ -24,6 +25,17 @@
runpy_available = False


def load_source(modname, filename):
loader = importlib.machinery.SourceFileLoader(modname, filename)
spec = importlib.util.spec_from_file_location(modname, filename, loader=loader)
module = importlib.util.module_from_spec(spec)
# The module is always executed and not cached in sys.modules.
# Uncomment the following line to cache the module.
# sys.modules[module.__name__] = module
loader.exec_module(module)
return module


def import_file(filename, context=None, name=None, clear_cache=False):
"""
Import a Python file as a module
Expand Down Expand Up @@ -125,15 +137,15 @@ def import_file(filename, context=None, name=None, clear_cache=False):
else:
if dirname is not None:
# find_module will return the .py file (never .pyc)
fp, pathname, description = imp.find_module(modulename,
[dirname])
fp, pathname, description = importlib.find_loader(modulename,
str(dirname))
fp.close()
else:
try:
sys.path.insert(0, implied_dirname)
# find_module will return the .py file
# (never .pyc)
fp, pathname, description = imp.find_module(modulename)
fp, pathname, description = importlib.find_loader(modulename)
fp.close()
except ImportError:
raise
Expand All @@ -142,7 +154,7 @@ def import_file(filename, context=None, name=None, clear_cache=False):
try:
# Note: we are always handing load_source a .py file, but
# it will use the .pyc or .pyo file if it exists
module = imp.load_source(name, pathname)
module = load_source(name, pathname)
except:
et, e, tb = sys.exc_info()
import traceback
Expand Down