+++ /dev/null
-"""\r
-Import utilities\r
-\r
-Exported classes:\r
- ImportManager Manage the import process\r
-\r
- Importer Base class for replacing standard import functions\r
- BuiltinImporter Emulate the import mechanism for builtin and frozen modules\r
-\r
- DynLoadSuffixImporter\r
-"""\r
-from warnings import warnpy3k\r
-warnpy3k("the imputil module has been removed in Python 3.0", stacklevel=2)\r
-del warnpy3k\r
-\r
-# note: avoid importing non-builtin modules\r
-import imp ### not available in Jython?\r
-import sys\r
-import __builtin__\r
-\r
-# for the DirectoryImporter\r
-import struct\r
-import marshal\r
-\r
-__all__ = ["ImportManager","Importer","BuiltinImporter"]\r
-\r
-_StringType = type('')\r
-_ModuleType = type(sys) ### doesn't work in Jython...\r
-\r
-class ImportManager:\r
- "Manage the import process."\r
-\r
- def install(self, namespace=vars(__builtin__)):\r
- "Install this ImportManager into the specified namespace."\r
-\r
- if isinstance(namespace, _ModuleType):\r
- namespace = vars(namespace)\r
-\r
- # Note: we have no notion of "chaining"\r
-\r
- # Record the previous import hook, then install our own.\r
- self.previous_importer = namespace['__import__']\r
- self.namespace = namespace\r
- namespace['__import__'] = self._import_hook\r
-\r
- ### fix this\r
- #namespace['reload'] = self._reload_hook\r
-\r
- def uninstall(self):\r
- "Restore the previous import mechanism."\r
- self.namespace['__import__'] = self.previous_importer\r
-\r
- def add_suffix(self, suffix, importFunc):\r
- assert hasattr(importFunc, '__call__')\r
- self.fs_imp.add_suffix(suffix, importFunc)\r
-\r
- ######################################################################\r
- #\r
- # PRIVATE METHODS\r
- #\r
-\r
- clsFilesystemImporter = None\r
-\r
- def __init__(self, fs_imp=None):\r
- # we're definitely going to be importing something in the future,\r
- # so let's just load the OS-related facilities.\r
- if not _os_stat:\r
- _os_bootstrap()\r
-\r
- # This is the Importer that we use for grabbing stuff from the\r
- # filesystem. It defines one more method (import_from_dir) for our use.\r
- if fs_imp is None:\r
- cls = self.clsFilesystemImporter or _FilesystemImporter\r
- fs_imp = cls()\r
- self.fs_imp = fs_imp\r
-\r
- # Initialize the set of suffixes that we recognize and import.\r
- # The default will import dynamic-load modules first, followed by\r
- # .py files (or a .py file's cached bytecode)\r
- for desc in imp.get_suffixes():\r
- if desc[2] == imp.C_EXTENSION:\r
- self.add_suffix(desc[0],\r
- DynLoadSuffixImporter(desc).import_file)\r
- self.add_suffix('.py', py_suffix_importer)\r
-\r
- def _import_hook(self, fqname, globals=None, locals=None, fromlist=None):\r
- """Python calls this hook to locate and import a module."""\r
-\r
- parts = fqname.split('.')\r
-\r
- # determine the context of this import\r
- parent = self._determine_import_context(globals)\r
-\r
- # if there is a parent, then its importer should manage this import\r
- if parent:\r
- module = parent.__importer__._do_import(parent, parts, fromlist)\r
- if module:\r
- return module\r
-\r
- # has the top module already been imported?\r
- try:\r
- top_module = sys.modules[parts[0]]\r
- except KeyError:\r
-\r
- # look for the topmost module\r
- top_module = self._import_top_module(parts[0])\r
- if not top_module:\r
- # the topmost module wasn't found at all.\r
- raise ImportError, 'No module named ' + fqname\r
-\r
- # fast-path simple imports\r
- if len(parts) == 1:\r
- if not fromlist:\r
- return top_module\r
-\r
- if not top_module.__dict__.get('__ispkg__'):\r
- # __ispkg__ isn't defined (the module was not imported by us),\r
- # or it is zero.\r
- #\r
- # In the former case, there is no way that we could import\r
- # sub-modules that occur in the fromlist (but we can't raise an\r
- # error because it may just be names) because we don't know how\r
- # to deal with packages that were imported by other systems.\r
- #\r
- # In the latter case (__ispkg__ == 0), there can't be any sub-\r
- # modules present, so we can just return.\r
- #\r
- # In both cases, since len(parts) == 1, the top_module is also\r
- # the "bottom" which is the defined return when a fromlist\r
- # exists.\r
- return top_module\r
-\r
- importer = top_module.__dict__.get('__importer__')\r
- if importer:\r
- return importer._finish_import(top_module, parts[1:], fromlist)\r
-\r
- # Grrr, some people "import os.path" or do "from os.path import ..."\r
- if len(parts) == 2 and hasattr(top_module, parts[1]):\r
- if fromlist:\r
- return getattr(top_module, parts[1])\r
- else:\r
- return top_module\r
-\r
- # If the importer does not exist, then we have to bail. A missing\r
- # importer means that something else imported the module, and we have\r
- # no knowledge of how to get sub-modules out of the thing.\r
- raise ImportError, 'No module named ' + fqname\r
-\r
- def _determine_import_context(self, globals):\r
- """Returns the context in which a module should be imported.\r
-\r
- The context could be a loaded (package) module and the imported module\r
- will be looked for within that package. The context could also be None,\r
- meaning there is no context -- the module should be looked for as a\r
- "top-level" module.\r
- """\r
-\r
- if not globals or not globals.get('__importer__'):\r
- # globals does not refer to one of our modules or packages. That\r
- # implies there is no relative import context (as far as we are\r
- # concerned), and it should just pick it off the standard path.\r
- return None\r
-\r
- # The globals refer to a module or package of ours. It will define\r
- # the context of the new import. Get the module/package fqname.\r
- parent_fqname = globals['__name__']\r
-\r
- # if a package is performing the import, then return itself (imports\r
- # refer to pkg contents)\r
- if globals['__ispkg__']:\r
- parent = sys.modules[parent_fqname]\r
- assert globals is parent.__dict__\r
- return parent\r
-\r
- i = parent_fqname.rfind('.')\r
-\r
- # a module outside of a package has no particular import context\r
- if i == -1:\r
- return None\r
-\r
- # if a module in a package is performing the import, then return the\r
- # package (imports refer to siblings)\r
- parent_fqname = parent_fqname[:i]\r
- parent = sys.modules[parent_fqname]\r
- assert parent.__name__ == parent_fqname\r
- return parent\r
-\r
- def _import_top_module(self, name):\r
- # scan sys.path looking for a location in the filesystem that contains\r
- # the module, or an Importer object that can import the module.\r
- for item in sys.path:\r
- if isinstance(item, _StringType):\r
- module = self.fs_imp.import_from_dir(item, name)\r
- else:\r
- module = item.import_top(name)\r
- if module:\r
- return module\r
- return None\r
-\r
- def _reload_hook(self, module):\r
- "Python calls this hook to reload a module."\r
-\r
- # reloading of a module may or may not be possible (depending on the\r
- # importer), but at least we can validate that it's ours to reload\r
- importer = module.__dict__.get('__importer__')\r
- if not importer:\r
- ### oops. now what...\r
- pass\r
-\r
- # okay. it is using the imputil system, and we must delegate it, but\r
- # we don't know what to do (yet)\r
- ### we should blast the module dict and do another get_code(). need to\r
- ### flesh this out and add proper docco...\r
- raise SystemError, "reload not yet implemented"\r
-\r
-\r
-class Importer:\r
- "Base class for replacing standard import functions."\r
-\r
- def import_top(self, name):\r
- "Import a top-level module."\r
- return self._import_one(None, name, name)\r
-\r
- ######################################################################\r
- #\r
- # PRIVATE METHODS\r
- #\r
- def _finish_import(self, top, parts, fromlist):\r
- # if "a.b.c" was provided, then load the ".b.c" portion down from\r
- # below the top-level module.\r
- bottom = self._load_tail(top, parts)\r
-\r
- # if the form is "import a.b.c", then return "a"\r
- if not fromlist:\r
- # no fromlist: return the top of the import tree\r
- return top\r
-\r
- # the top module was imported by self.\r
- #\r
- # this means that the bottom module was also imported by self (just\r
- # now, or in the past and we fetched it from sys.modules).\r
- #\r
- # since we imported/handled the bottom module, this means that we can\r
- # also handle its fromlist (and reliably use __ispkg__).\r
-\r
- # if the bottom node is a package, then (potentially) import some\r
- # modules.\r
- #\r
- # note: if it is not a package, then "fromlist" refers to names in\r
- # the bottom module rather than modules.\r
- # note: for a mix of names and modules in the fromlist, we will\r
- # import all modules and insert those into the namespace of\r
- # the package module. Python will pick up all fromlist names\r
- # from the bottom (package) module; some will be modules that\r
- # we imported and stored in the namespace, others are expected\r
- # to be present already.\r
- if bottom.__ispkg__:\r
- self._import_fromlist(bottom, fromlist)\r
-\r
- # if the form is "from a.b import c, d" then return "b"\r
- return bottom\r
-\r
- def _import_one(self, parent, modname, fqname):\r
- "Import a single module."\r
-\r
- # has the module already been imported?\r
- try:\r
- return sys.modules[fqname]\r
- except KeyError:\r
- pass\r
-\r
- # load the module's code, or fetch the module itself\r
- result = self.get_code(parent, modname, fqname)\r
- if result is None:\r
- return None\r
-\r
- module = self._process_result(result, fqname)\r
-\r
- # insert the module into its parent\r
- if parent:\r
- setattr(parent, modname, module)\r
- return module\r
-\r
- def _process_result(self, result, fqname):\r
- ispkg, code, values = result\r
- # did get_code() return an actual module? (rather than a code object)\r
- is_module = isinstance(code, _ModuleType)\r
-\r
- # use the returned module, or create a new one to exec code into\r
- if is_module:\r
- module = code\r
- else:\r
- module = imp.new_module(fqname)\r
-\r
- ### record packages a bit differently??\r
- module.__importer__ = self\r
- module.__ispkg__ = ispkg\r
-\r
- # insert additional values into the module (before executing the code)\r
- module.__dict__.update(values)\r
-\r
- # the module is almost ready... make it visible\r
- sys.modules[fqname] = module\r
-\r
- # execute the code within the module's namespace\r
- if not is_module:\r
- try:\r
- exec code in module.__dict__\r
- except:\r
- if fqname in sys.modules:\r
- del sys.modules[fqname]\r
- raise\r
-\r
- # fetch from sys.modules instead of returning module directly.\r
- # also make module's __name__ agree with fqname, in case\r
- # the "exec code in module.__dict__" played games on us.\r
- module = sys.modules[fqname]\r
- module.__name__ = fqname\r
- return module\r
-\r
- def _load_tail(self, m, parts):\r
- """Import the rest of the modules, down from the top-level module.\r
-\r
- Returns the last module in the dotted list of modules.\r
- """\r
- for part in parts:\r
- fqname = "%s.%s" % (m.__name__, part)\r
- m = self._import_one(m, part, fqname)\r
- if not m:\r
- raise ImportError, "No module named " + fqname\r
- return m\r
-\r
- def _import_fromlist(self, package, fromlist):\r
- 'Import any sub-modules in the "from" list.'\r
-\r
- # if '*' is present in the fromlist, then look for the '__all__'\r
- # variable to find additional items (modules) to import.\r
- if '*' in fromlist:\r
- fromlist = list(fromlist) + \\r
- list(package.__dict__.get('__all__', []))\r
-\r
- for sub in fromlist:\r
- # if the name is already present, then don't try to import it (it\r
- # might not be a module!).\r
- if sub != '*' and not hasattr(package, sub):\r
- subname = "%s.%s" % (package.__name__, sub)\r
- submod = self._import_one(package, sub, subname)\r
- if not submod:\r
- raise ImportError, "cannot import name " + subname\r
-\r
- def _do_import(self, parent, parts, fromlist):\r
- """Attempt to import the module relative to parent.\r
-\r
- This method is used when the import context specifies that <self>\r
- imported the parent module.\r
- """\r
- top_name = parts[0]\r
- top_fqname = parent.__name__ + '.' + top_name\r
- top_module = self._import_one(parent, top_name, top_fqname)\r
- if not top_module:\r
- # this importer and parent could not find the module (relatively)\r
- return None\r
-\r
- return self._finish_import(top_module, parts[1:], fromlist)\r
-\r
- ######################################################################\r
- #\r
- # METHODS TO OVERRIDE\r
- #\r
- def get_code(self, parent, modname, fqname):\r
- """Find and retrieve the code for the given module.\r
-\r
- parent specifies a parent module to define a context for importing. It\r
- may be None, indicating no particular context for the search.\r
-\r
- modname specifies a single module (not dotted) within the parent.\r
-\r
- fqname specifies the fully-qualified module name. This is a\r
- (potentially) dotted name from the "root" of the module namespace\r
- down to the modname.\r
- If there is no parent, then modname==fqname.\r
-\r
- This method should return None, or a 3-tuple.\r
-\r
- * If the module was not found, then None should be returned.\r
-\r
- * The first item of the 2- or 3-tuple should be the integer 0 or 1,\r
- specifying whether the module that was found is a package or not.\r
-\r
- * The second item is the code object for the module (it will be\r
- executed within the new module's namespace). This item can also\r
- be a fully-loaded module object (e.g. loaded from a shared lib).\r
-\r
- * The third item is a dictionary of name/value pairs that will be\r
- inserted into new module before the code object is executed. This\r
- is provided in case the module's code expects certain values (such\r
- as where the module was found). When the second item is a module\r
- object, then these names/values will be inserted *after* the module\r
- has been loaded/initialized.\r
- """\r
- raise RuntimeError, "get_code not implemented"\r
-\r
-\r
-######################################################################\r
-#\r
-# Some handy stuff for the Importers\r
-#\r
-\r
-# byte-compiled file suffix character\r
-_suffix_char = __debug__ and 'c' or 'o'\r
-\r
-# byte-compiled file suffix\r
-_suffix = '.py' + _suffix_char\r
-\r
-def _compile(pathname, timestamp):\r
- """Compile (and cache) a Python source file.\r
-\r
- The file specified by <pathname> is compiled to a code object and\r
- returned.\r
-\r
- Presuming the appropriate privileges exist, the bytecodes will be\r
- saved back to the filesystem for future imports. The source file's\r
- modification timestamp must be provided as a Long value.\r
- """\r
- codestring = open(pathname, 'rU').read()\r
- if codestring and codestring[-1] != '\n':\r
- codestring = codestring + '\n'\r
- code = __builtin__.compile(codestring, pathname, 'exec')\r
-\r
- # try to cache the compiled code\r
- try:\r
- f = open(pathname + _suffix_char, 'wb')\r
- except IOError:\r
- pass\r
- else:\r
- f.write('\0\0\0\0')\r
- f.write(struct.pack('<I', timestamp))\r
- marshal.dump(code, f)\r
- f.flush()\r
- f.seek(0, 0)\r
- f.write(imp.get_magic())\r
- f.close()\r
-\r
- return code\r
-\r
-_os_stat = _os_path_join = None\r
-def _os_bootstrap():\r
- "Set up 'os' module replacement functions for use during import bootstrap."\r
-\r
- names = sys.builtin_module_names\r
-\r
- join = None\r
- if 'posix' in names:\r
- sep = '/'\r
- from posix import stat\r
- elif 'nt' in names:\r
- sep = '\\'\r
- from nt import stat\r
- elif 'dos' in names:\r
- sep = '\\'\r
- from dos import stat\r
- elif 'os2' in names:\r
- sep = '\\'\r
- from os2 import stat\r
- else:\r
- raise ImportError, 'no os specific module found'\r
-\r
- if join is None:\r
- def join(a, b, sep=sep):\r
- if a == '':\r
- return b\r
- lastchar = a[-1:]\r
- if lastchar == '/' or lastchar == sep:\r
- return a + b\r
- return a + sep + b\r
-\r
- global _os_stat\r
- _os_stat = stat\r
-\r
- global _os_path_join\r
- _os_path_join = join\r
-\r
-def _os_path_isdir(pathname):\r
- "Local replacement for os.path.isdir()."\r
- try:\r
- s = _os_stat(pathname)\r
- except OSError:\r
- return None\r
- return (s.st_mode & 0170000) == 0040000\r
-\r
-def _timestamp(pathname):\r
- "Return the file modification time as a Long."\r
- try:\r
- s = _os_stat(pathname)\r
- except OSError:\r
- return None\r
- return long(s.st_mtime)\r
-\r
-\r
-######################################################################\r
-#\r
-# Emulate the import mechanism for builtin and frozen modules\r
-#\r
-class BuiltinImporter(Importer):\r
- def get_code(self, parent, modname, fqname):\r
- if parent:\r
- # these modules definitely do not occur within a package context\r
- return None\r
-\r
- # look for the module\r
- if imp.is_builtin(modname):\r
- type = imp.C_BUILTIN\r
- elif imp.is_frozen(modname):\r
- type = imp.PY_FROZEN\r
- else:\r
- # not found\r
- return None\r
-\r
- # got it. now load and return it.\r
- module = imp.load_module(modname, None, modname, ('', '', type))\r
- return 0, module, { }\r
-\r
-\r
-######################################################################\r
-#\r
-# Internal importer used for importing from the filesystem\r
-#\r
-class _FilesystemImporter(Importer):\r
- def __init__(self):\r
- self.suffixes = [ ]\r
-\r
- def add_suffix(self, suffix, importFunc):\r
- assert hasattr(importFunc, '__call__')\r
- self.suffixes.append((suffix, importFunc))\r
-\r
- def import_from_dir(self, dir, fqname):\r
- result = self._import_pathname(_os_path_join(dir, fqname), fqname)\r
- if result:\r
- return self._process_result(result, fqname)\r
- return None\r
-\r
- def get_code(self, parent, modname, fqname):\r
- # This importer is never used with an empty parent. Its existence is\r
- # private to the ImportManager. The ImportManager uses the\r
- # import_from_dir() method to import top-level modules/packages.\r
- # This method is only used when we look for a module within a package.\r
- assert parent\r
-\r
- for submodule_path in parent.__path__:\r
- code = self._import_pathname(_os_path_join(submodule_path, modname), fqname)\r
- if code is not None:\r
- return code\r
- return self._import_pathname(_os_path_join(parent.__pkgdir__, modname),\r
- fqname)\r
-\r
- def _import_pathname(self, pathname, fqname):\r
- if _os_path_isdir(pathname):\r
- result = self._import_pathname(_os_path_join(pathname, '__init__'),\r
- fqname)\r
- if result:\r
- values = result[2]\r
- values['__pkgdir__'] = pathname\r
- values['__path__'] = [ pathname ]\r
- return 1, result[1], values\r
- return None\r
-\r
- for suffix, importFunc in self.suffixes:\r
- filename = pathname + suffix\r
- try:\r
- finfo = _os_stat(filename)\r
- except OSError:\r
- pass\r
- else:\r
- return importFunc(filename, finfo, fqname)\r
- return None\r
-\r
-######################################################################\r
-#\r
-# SUFFIX-BASED IMPORTERS\r
-#\r
-\r
-def py_suffix_importer(filename, finfo, fqname):\r
- file = filename[:-3] + _suffix\r
- t_py = long(finfo[8])\r
- t_pyc = _timestamp(file)\r
-\r
- code = None\r
- if t_pyc is not None and t_pyc >= t_py:\r
- f = open(file, 'rb')\r
- if f.read(4) == imp.get_magic():\r
- t = struct.unpack('<I', f.read(4))[0]\r
- if t == t_py:\r
- code = marshal.load(f)\r
- f.close()\r
- if code is None:\r
- file = filename\r
- code = _compile(file, t_py)\r
-\r
- return 0, code, { '__file__' : file }\r
-\r
-class DynLoadSuffixImporter:\r
- def __init__(self, desc):\r
- self.desc = desc\r
-\r
- def import_file(self, filename, finfo, fqname):\r
- fp = open(filename, self.desc[1])\r
- module = imp.load_module(fqname, fp, filename, self.desc)\r
- module.__file__ = filename\r
- return 0, module, { }\r
-\r
-\r
-######################################################################\r
-\r
-def _print_importers():\r
- items = sys.modules.items()\r
- items.sort()\r
- for name, module in items:\r
- if module:\r
- print name, module.__dict__.get('__importer__', '-- no importer')\r
- else:\r
- print name, '-- non-existent module'\r
-\r
-def _test_revamp():\r
- ImportManager().install()\r
- sys.path.insert(0, BuiltinImporter())\r
-\r
-######################################################################\r
-\r
-#\r
-# TODO\r
-#\r
-# from Finn Bock:\r
-# type(sys) is not a module in Jython. what to use instead?\r
-# imp.C_EXTENSION is not in Jython. same for get_suffixes and new_module\r
-#\r
-# given foo.py of:\r
-# import sys\r
-# sys.modules['foo'] = sys\r
-#\r
-# ---- standard import mechanism\r
-# >>> import foo\r
-# >>> foo\r
-# <module 'sys' (built-in)>\r
-#\r
-# ---- revamped import mechanism\r
-# >>> import imputil\r
-# >>> imputil._test_revamp()\r
-# >>> import foo\r
-# >>> foo\r
-# <module 'foo' from 'foo.py'>\r
-#\r
-#\r
-# from MAL:\r
-# should BuiltinImporter exist in sys.path or hard-wired in ImportManager?\r
-# need __path__ processing\r
-# performance\r
-# move chaining to a subclass [gjs: it's been nuked]\r
-# deinstall should be possible\r
-# query mechanism needed: is a specific Importer installed?\r
-# py/pyc/pyo piping hooks to filter/process these files\r
-# wish list:\r
-# distutils importer hooked to list of standard Internet repositories\r
-# module->file location mapper to speed FS-based imports\r
-# relative imports\r
-# keep chaining so that it can play nice with other import hooks\r
-#\r
-# from Gordon:\r
-# push MAL's mapper into sys.path[0] as a cache (hard-coded for apps)\r
-#\r
-# from Guido:\r
-# need to change sys.* references for rexec environs\r
-# need hook for MAL's walk-me-up import strategy, or Tim's absolute strategy\r
-# watch out for sys.modules[...] is None\r
-# flag to force absolute imports? (speeds _determine_import_context and\r
-# checking for a relative module)\r
-# insert names of archives into sys.path (see quote below)\r
-# note: reload does NOT blast module dict\r
-# shift import mechanisms and policies around; provide for hooks, overrides\r
-# (see quote below)\r
-# add get_source stuff\r
-# get_topcode and get_subcode\r
-# CRLF handling in _compile\r
-# race condition in _compile\r
-# refactoring of os.py to deal with _os_bootstrap problem\r
-# any special handling to do for importing a module with a SyntaxError?\r
-# (e.g. clean up the traceback)\r
-# implement "domain" for path-type functionality using pkg namespace\r
-# (rather than FS-names like __path__)\r
-# don't use the word "private"... maybe "internal"\r
-#\r
-#\r
-# Guido's comments on sys.path caching:\r
-#\r
-# We could cache this in a dictionary: the ImportManager can have a\r
-# cache dict mapping pathnames to importer objects, and a separate\r
-# method for coming up with an importer given a pathname that's not yet\r
-# in the cache. The method should do a stat and/or look at the\r
-# extension to decide which importer class to use; you can register new\r
-# importer classes by registering a suffix or a Boolean function, plus a\r
-# class. If you register a new importer class, the cache is zapped.\r
-# The cache is independent from sys.path (but maintained per\r
-# ImportManager instance) so that rearrangements of sys.path do the\r
-# right thing. If a path is dropped from sys.path the corresponding\r
-# cache entry is simply no longer used.\r
-#\r
-# My/Guido's comments on factoring ImportManager and Importer:\r
-#\r
-# > However, we still have a tension occurring here:\r
-# >\r
-# > 1) implementing policy in ImportManager assists in single-point policy\r
-# > changes for app/rexec situations\r
-# > 2) implementing policy in Importer assists in package-private policy\r
-# > changes for normal, operating conditions\r
-# >\r
-# > I'll see if I can sort out a way to do this. Maybe the Importer class will\r
-# > implement the methods (which can be overridden to change policy) by\r
-# > delegating to ImportManager.\r
-#\r
-# Maybe also think about what kind of policies an Importer would be\r
-# likely to want to change. I have a feeling that a lot of the code\r
-# there is actually not so much policy but a *necessity* to get things\r
-# working given the calling conventions for the __import__ hook: whether\r
-# to return the head or tail of a dotted name, or when to do the "finish\r
-# fromlist" stuff.\r
-#\r