Commit e48880a8 authored by Greg Stein's avatar Greg Stein

reduce fsimp coupling: provide fs_imp param to ImportManager constructor,

  add clsFilesystemImporter class attribute, alter handling of suffix list
convert suffix importers to funcs rather than instances
remove backwards compat code: Importer.install and 2-tuple get_code()
  result values
parent dd6af3ed
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
# #
# note: avoid importing non-builtin modules # note: avoid importing non-builtin modules
import imp import imp ### not available in JPython?
import sys import sys
import strop import strop
import __builtin__ import __builtin__
...@@ -40,36 +40,42 @@ class ImportManager: ...@@ -40,36 +40,42 @@ class ImportManager:
namespace = vars(namespace) namespace = vars(namespace)
### Note that we have no notion of "uninstall" or "chaining" ### Note that we have no notion of "uninstall" or "chaining"
namespace['__import__'] = self._import_hook namespace['__import__'] = self._import_hook
### fix this ### fix this
#namespace['reload'] = self._reload_hook #namespace['reload'] = self._reload_hook
def add_suffix(self, suffix, importer): def add_suffix(self, suffix, importFunc):
assert isinstance(importer, SuffixImporter) assert callable(importFunc)
self.suffixes.append((suffix, importer)) self.fs_imp.add_suffix(suffix, importFunc)
###################################################################### ######################################################################
# #
# PRIVATE METHODS # PRIVATE METHODS
# #
def __init__(self):
clsFilesystemImporter = None
def __init__(self, fs_imp=None):
# we're definitely going to be importing something in the future, # we're definitely going to be importing something in the future,
# so let's just load the OS-related facilities. # so let's just load the OS-related facilities.
if not _os_stat: if not _os_stat:
_os_bootstrap() _os_bootstrap()
# This is the Importer that we use for grabbing stuff from the
# filesystem. It defines one more method (import_from_dir) for our use.
if not fs_imp:
cls = self.clsFilesystemImporter or _FilesystemImporter
fs_imp = cls()
self.fs_imp = fs_imp
# Initialize the set of suffixes that we recognize and import. # Initialize the set of suffixes that we recognize and import.
# The default will import dynamic-load modules first, followed by # The default will import dynamic-load modules first, followed by
# .py files (or a .py file's cached bytecode) # .py files (or a .py file's cached bytecode)
self.suffixes = [ ]
for desc in imp.get_suffixes(): for desc in imp.get_suffixes():
if desc[2] == imp.C_EXTENSION: if desc[2] == imp.C_EXTENSION:
self.suffixes.append((desc[0], DynLoadSuffixImporter(desc))) self.add_suffix(desc[0], DynLoadSuffixImporter(desc).import_file)
self.suffixes.append(('.py', PySuffixImporter())) self.add_suffix('.py', py_suffix_importer)
# This is the importer that we use for grabbing stuff from the
# filesystem. It defines one more method (import_from_dir) for our use.
self.fs_imp = _FilesystemImporter(self.suffixes)
def _import_hook(self, fqname, globals=None, locals=None, fromlist=None): def _import_hook(self, fqname, globals=None, locals=None, fromlist=None):
"""Python calls this hook to locate and import a module.""" """Python calls this hook to locate and import a module."""
...@@ -197,9 +203,6 @@ class ImportManager: ...@@ -197,9 +203,6 @@ class ImportManager:
class Importer: class Importer:
"Base class for replacing standard import functions." "Base class for replacing standard import functions."
def install(self):
sys.path.insert(0, self)
def import_top(self, name): def import_top(self, name):
"Import a top-level module." "Import a top-level module."
return self._import_one(None, name, name) return self._import_one(None, name, name)
...@@ -256,10 +259,6 @@ class Importer: ...@@ -256,10 +259,6 @@ class Importer:
if result is None: if result is None:
return None return None
### backwards-compat
if len(result) == 2:
result = result + ({},)
module = self._process_result(result, fqname) module = self._process_result(result, fqname)
# insert the module into its parent # insert the module into its parent
...@@ -510,9 +509,12 @@ class BuiltinImporter(Importer): ...@@ -510,9 +509,12 @@ class BuiltinImporter(Importer):
# Internal importer used for importing from the filesystem # Internal importer used for importing from the filesystem
# #
class _FilesystemImporter(Importer): class _FilesystemImporter(Importer):
def __init__(self, suffixes): def __init__(self):
# this list is shared with the ImportManager. self.suffixes = [ ]
self.suffixes = suffixes
def add_suffix(self, suffix, importFunc):
assert callable(importFunc)
self.suffixes.append((suffix, importFunc))
def import_from_dir(self, dir, fqname): def import_from_dir(self, dir, fqname):
result = self._import_pathname(_os_path_join(dir, fqname), fqname) result = self._import_pathname(_os_path_join(dir, fqname), fqname)
...@@ -541,14 +543,14 @@ class _FilesystemImporter(Importer): ...@@ -541,14 +543,14 @@ class _FilesystemImporter(Importer):
return 1, result[1], values return 1, result[1], values
return None return None
for suffix, importer in self.suffixes: for suffix, importFunc in self.suffixes:
filename = pathname + suffix filename = pathname + suffix
try: try:
finfo = _os_stat(filename) finfo = _os_stat(filename)
except OSError: except OSError:
pass pass
else: else:
return importer.import_file(filename, finfo, fqname) return importFunc(filename, finfo, fqname)
return None return None
###################################################################### ######################################################################
...@@ -556,12 +558,7 @@ class _FilesystemImporter(Importer): ...@@ -556,12 +558,7 @@ class _FilesystemImporter(Importer):
# SUFFIX-BASED IMPORTERS # SUFFIX-BASED IMPORTERS
# #
class SuffixImporter: def py_suffix_importer(filename, finfo, fqname):
def import_file(self, filename, finfo, fqname):
raise RuntimeError
class PySuffixImporter(SuffixImporter):
def import_file(self, filename, finfo, fqname):
file = filename[:-3] + _suffix file = filename[:-3] + _suffix
t_py = long(finfo[8]) t_py = long(finfo[8])
t_pyc = _timestamp(file) t_pyc = _timestamp(file)
...@@ -580,7 +577,7 @@ class PySuffixImporter(SuffixImporter): ...@@ -580,7 +577,7 @@ class PySuffixImporter(SuffixImporter):
return 0, code, { '__file__' : file } return 0, code, { '__file__' : file }
class DynLoadSuffixImporter(SuffixImporter): class DynLoadSuffixImporter:
def __init__(self, desc): def __init__(self, desc):
self.desc = desc self.desc = desc
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment