home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_Lib_imputil.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  25.1 KB  |  710 lines

  1. #
  2. # imputil.py: import utilities
  3. #
  4.  
  5. ### docco needed here and in Docs/ ...
  6.  
  7. # note: avoid importing non-builtin modules
  8. import imp                      ### not available in JPython?
  9. import sys
  10. import strop
  11. import __builtin__
  12.  
  13. # for the DirectoryImporter
  14. import struct
  15. import marshal
  16.  
  17. __all__ = ["ImportManager","Importer","BuiltinImporter"]
  18.  
  19. _StringType = type('')
  20. _ModuleType = type(sys)         ### doesn't work in JPython...
  21.  
  22. class ImportManager:
  23.     "Manage the import process."
  24.  
  25.     def install(self, namespace=vars(__builtin__)):
  26.         "Install this ImportManager into the specified namespace."
  27.  
  28.         if isinstance(namespace, _ModuleType):
  29.             namespace = vars(namespace)
  30.  
  31.         # Note: we have no notion of "chaining"
  32.  
  33.         # Record the previous import hook, then install our own.
  34.         self.previous_importer = namespace['__import__']
  35.         self.namespace = namespace
  36.         namespace['__import__'] = self._import_hook
  37.  
  38.         ### fix this
  39.         #namespace['reload'] = self._reload_hook
  40.  
  41.     def uninstall(self):
  42.         "Restore the previous import mechanism."
  43.         self.namespace['__import__'] = self.previous_importer
  44.  
  45.     def add_suffix(self, suffix, importFunc):
  46.         assert callable(importFunc)
  47.         self.fs_imp.add_suffix(suffix, importFunc)
  48.  
  49.     ######################################################################
  50.     #
  51.     # PRIVATE METHODS
  52.     #
  53.  
  54.     clsFilesystemImporter = None
  55.  
  56.     def __init__(self, fs_imp=None):
  57.         # we're definitely going to be importing something in the future,
  58.         # so let's just load the OS-related facilities.
  59.         if not _os_stat:
  60.             _os_bootstrap()
  61.  
  62.         # This is the Importer that we use for grabbing stuff from the
  63.         # filesystem. It defines one more method (import_from_dir) for our use.
  64.         if not fs_imp:
  65.             cls = self.clsFilesystemImporter or _FilesystemImporter
  66.             fs_imp = cls()
  67.         self.fs_imp = fs_imp
  68.  
  69.         # Initialize the set of suffixes that we recognize and import.
  70.         # The default will import dynamic-load modules first, followed by
  71.         # .py files (or a .py file's cached bytecode)
  72.         for desc in imp.get_suffixes():
  73.             if desc[2] == imp.C_EXTENSION:
  74.                 self.add_suffix(desc[0],
  75.                                 DynLoadSuffixImporter(desc).import_file)
  76.         self.add_suffix('.py', py_suffix_importer)
  77.  
  78.     def _import_hook(self, fqname, globals=None, locals=None, fromlist=None):
  79.         """Python calls this hook to locate and import a module."""
  80.  
  81.         parts = strop.split(fqname, '.')
  82.  
  83.         # determine the context of this import
  84.         parent = self._determine_import_context(globals)
  85.  
  86.         # if there is a parent, then its importer should manage this import
  87.         if parent:
  88.             module = parent.__importer__._do_import(parent, parts, fromlist)
  89.             if module:
  90.                 return module
  91.  
  92.         # has the top module already been imported?
  93.         try:
  94.             top_module = sys.modules[parts[0]]
  95.         except KeyError:
  96.  
  97.             # look for the topmost module
  98.             top_module = self._import_top_module(parts[0])
  99.             if not top_module:
  100.                 # the topmost module wasn't found at all.
  101.                 raise ImportError, 'No module named ' + fqname
  102.  
  103.         # fast-path simple imports
  104.         if len(parts) == 1:
  105.             if not fromlist:
  106.                 return top_module
  107.  
  108.             if not top_module.__dict__.get('__ispkg__'):
  109.                 # __ispkg__ isn't defined (the module was not imported by us),
  110.                 # or it is zero.
  111.                 #
  112.                 # In the former case, there is no way that we could import
  113.                 # sub-modules that occur in the fromlist (but we can't raise an
  114.                 # error because it may just be names) because we don't know how
  115.                 # to deal with packages that were imported by other systems.
  116.                 #
  117.                 # In the latter case (__ispkg__ == 0), there can't be any sub-
  118.                 # modules present, so we can just return.
  119.                 #
  120.                 # In both cases, since len(parts) == 1, the top_module is also
  121.                 # the "bottom" which is the defined return when a fromlist
  122.                 # exists.
  123.                 return top_module
  124.  
  125.         importer = top_module.__dict__.get('__importer__')
  126.         if importer:
  127.             return importer._finish_import(top_module, parts[1:], fromlist)
  128.  
  129.         # If the importer does not exist, then we have to bail. A missing
  130.         # importer means that something else imported the module, and we have
  131.         # no knowledge of how to get sub-modules out of the thing.
  132.         raise ImportError, 'No module named ' + fqname
  133.  
  134.     def _determine_import_context(self, globals):
  135.         """Returns the context in which a module should be imported.
  136.  
  137.         The context could be a loaded (package) module and the imported module
  138.         will be looked for within that package. The context could also be None,
  139.         meaning there is no context -- the module should be looked for as a
  140.         "top-level" module.
  141.         """
  142.  
  143.         if not globals or not globals.get('__importer__'):
  144.             # globals does not refer to one of our modules or packages. That
  145.             # implies there is no relative import context (as far as we are
  146.             # concerned), and it should just pick it off the standard path.
  147.             return None
  148.  
  149.         # The globals refer to a module or package of ours. It will define
  150.         # the context of the new import. Get the module/package fqname.
  151.         parent_fqname = globals['__name__']
  152.  
  153.         # if a package is performing the import, then return itself (imports
  154.         # refer to pkg contents)
  155.         if globals['__ispkg__']:
  156.             parent = sys.modules[parent_fqname]
  157.             assert globals is parent.__dict__
  158.             return parent
  159.  
  160.         i = strop.rfind(parent_fqname, '.')
  161.  
  162.         # a module outside of a package has no particular import context
  163.         if i == -1:
  164.             return None
  165.  
  166.         # if a module in a package is performing the import, then return the
  167.         # package (imports refer to siblings)
  168.         parent_fqname = parent_fqname[:i]
  169.         parent = sys.modules[parent_fqname]
  170.         assert parent.__name__ == parent_fqname
  171.         return parent
  172.  
  173.     def _import_top_module(self, name):
  174.         # scan sys.path looking for a location in the filesystem that contains
  175.         # the module, or an Importer object that can import the module.
  176.         for item in sys.path:
  177.             if isinstance(item, _StringType):
  178.                 module = self.fs_imp.import_from_dir(item, name)
  179.             else:
  180.                 module = item.import_top(name)
  181.             if module:
  182.                 return module
  183.         return None
  184.  
  185.     def _reload_hook(self, module):
  186.         "Python calls this hook to reload a module."
  187.  
  188.         # reloading of a module may or may not be possible (depending on the
  189.         # importer), but at least we can validate that it's ours to reload
  190.         importer = module.__dict__.get('__importer__')
  191.         if not importer:
  192.             ### oops. now what...
  193.             pass
  194.  
  195.         # okay. it is using the imputil system, and we must delegate it, but
  196.         # we don't know what to do (yet)
  197.         ### we should blast the module dict and do another get_code(). need to
  198.         ### flesh this out and add proper docco...
  199.         raise SystemError, "reload not yet implemented"
  200.  
  201.  
  202. class Importer:
  203.     "Base class for replacing standard import functions."
  204.  
  205.     def import_top(self, name):
  206.         "Import a top-level module."
  207.         return self._import_one(None, name, name)
  208.  
  209.     ######################################################################
  210.     #
  211.     # PRIVATE METHODS
  212.     #
  213.     def _finish_import(self, top, parts, fromlist):
  214.         # if "a.b.c" was provided, then load the ".b.c" portion down from
  215.         # below the top-level module.
  216.         bottom = self._load_tail(top, parts)
  217.  
  218.         # if the form is "import a.b.c", then return "a"
  219.         if not fromlist:
  220.             # no fromlist: return the top of the import tree
  221.             return top
  222.  
  223.         # the top module was imported by self.
  224.         #
  225.         # this means that the bottom module was also imported by self (just
  226.         # now, or in the past and we fetched it from sys.modules).
  227.         #
  228.         # since we imported/handled the bottom module, this means that we can
  229.         # also handle its fromlist (and reliably use __ispkg__).
  230.  
  231.         # if the bottom node is a package, then (potentially) import some
  232.         # modules.
  233.         #
  234.         # note: if it is not a package, then "fromlist" refers to names in
  235.         #       the bottom module rather than modules.
  236.         # note: for a mix of names and modules in the fromlist, we will
  237.         #       import all modules and insert those into the namespace of
  238.         #       the package module. Python will pick up all fromlist names
  239.         #       from the bottom (package) module; some will be modules that
  240.         #       we imported and stored in the namespace, others are expected
  241.         #       to be present already.
  242.         if bottom.__ispkg__:
  243.             self._import_fromlist(bottom, fromlist)
  244.  
  245.         # if the form is "from a.b import c, d" then return "b"
  246.         return bottom
  247.  
  248.     def _import_one(self, parent, modname, fqname):
  249.         "Import a single module."
  250.  
  251.         # has the module already been imported?
  252.         try:
  253.             return sys.modules[fqname]
  254.         except KeyError:
  255.             pass
  256.  
  257.         # load the module's code, or fetch the module itself
  258.         result = self.get_code(parent, modname, fqname)
  259.         if result is None:
  260.             return None
  261.  
  262.         module = self._process_result(result, fqname)
  263.  
  264.         # insert the module into its parent
  265.         if parent:
  266.             setattr(parent, modname, module)
  267.         return module
  268.  
  269.     def _process_result(self, (ispkg, code, values), fqname):
  270.         # did get_code() return an actual module? (rather than a code object)
  271.         is_module = isinstance(code, _ModuleType)
  272.  
  273.         # use the returned module, or create a new one to exec code into
  274.         if is_module:
  275.             module = code
  276.         else:
  277.             module = imp.new_module(fqname)
  278.  
  279.         ### record packages a bit differently??
  280.         module.__importer__ = self
  281.         module.__ispkg__ = ispkg
  282.  
  283.         # insert additional values into the module (before executing the code)
  284.         module.__dict__.update(values)
  285.  
  286.         # the module is almost ready... make it visible
  287.         sys.modules[fqname] = module
  288.  
  289.         # execute the code within the module's namespace
  290.         if not is_module:
  291.             exec code in module.__dict__
  292.  
  293.         # fetch from sys.modules instead of returning module directly.
  294.         return sys.modules[fqname]
  295.  
  296.     def _load_tail(self, m, parts):
  297.         """Import the rest of the modules, down from the top-level module.
  298.  
  299.         Returns the last module in the dotted list of modules.
  300.         """
  301.         for part in parts:
  302.             fqname = "%s.%s" % (m.__name__, part)
  303.             m = self._import_one(m, part, fqname)
  304.             if not m:
  305.                 raise ImportError, "No module named " + fqname
  306.         return m
  307.  
  308.     def _import_fromlist(self, package, fromlist):
  309.         'Import any sub-modules in the "from" list.'
  310.  
  311.         # if '*' is present in the fromlist, then look for the '__all__'
  312.         # variable to find additional items (modules) to import.
  313.         if '*' in fromlist:
  314.             fromlist = list(fromlist) + \
  315.                        list(package.__dict__.get('__all__', []))
  316.  
  317.         for sub in fromlist:
  318.             # if the name is already present, then don't try to import it (it
  319.             # might not be a module!).
  320.             if sub != '*' and not hasattr(package, sub):
  321.                 subname = "%s.%s" % (package.__name__, sub)
  322.                 submod = self._import_one(package, sub, subname)
  323.                 if not submod:
  324.                     raise ImportError, "cannot import name " + subname
  325.  
  326.     def _do_import(self, parent, parts, fromlist):
  327.         """Attempt to import the module relative to parent.
  328.  
  329.         This method is used when the import context specifies that <self>
  330.         imported the parent module.
  331.         """
  332.         top_name = parts[0]
  333.         top_fqname = parent.__name__ + '.' + top_name
  334.         top_module = self._import_one(parent, top_name, top_fqname)
  335.         if not top_module:
  336.             # this importer and parent could not find the module (relatively)
  337.             return None
  338.  
  339.         return self._finish_import(top_module, parts[1:], fromlist)
  340.  
  341.     ######################################################################
  342.     #
  343.     # METHODS TO OVERRIDE
  344.     #
  345.     def get_code(self, parent, modname, fqname):
  346.         """Find and retrieve the code for the given module.
  347.  
  348.         parent specifies a parent module to define a context for importing. It
  349.         may be None, indicating no particular context for the search.
  350.  
  351.         modname specifies a single module (not dotted) within the parent.
  352.  
  353.         fqname specifies the fully-qualified module name. This is a
  354.         (potentially) dotted name from the "root" of the module namespace
  355.         down to the modname.
  356.         If there is no parent, then modname==fqname.
  357.  
  358.         This method should return None, or a 3-tuple.
  359.  
  360.         * If the module was not found, then None should be returned.
  361.  
  362.         * The first item of the 2- or 3-tuple should be the integer 0 or 1,
  363.             specifying whether the module that was found is a package or not.
  364.  
  365.         * The second item is the code object for the module (it will be
  366.             executed within the new module's namespace). This item can also
  367.             be a fully-loaded module object (e.g. loaded from a shared lib).
  368.  
  369.         * The third item is a dictionary of name/value pairs that will be
  370.             inserted into new module before the code object is executed. This
  371.             is provided in case the module's code expects certain values (such
  372.             as where the module was found). When the second item is a module
  373.             object, then these names/values will be inserted *after* the module
  374.             has been loaded/initialized.
  375.         """
  376.         raise RuntimeError, "get_code not implemented"
  377.  
  378.  
  379. ######################################################################
  380. #
  381. # Some handy stuff for the Importers
  382. #
  383.  
  384. # byte-compiled file suffix character
  385. _suffix_char = __debug__ and 'c' or 'o'
  386.  
  387. # byte-compiled file suffix
  388. _suffix = '.py' + _suffix_char
  389.  
  390. def _compile(pathname, timestamp):
  391.     """Compile (and cache) a Python source file.
  392.  
  393.     The file specified by <pathname> is compiled to a code object and
  394.     returned.
  395.  
  396.     Presuming the appropriate privileges exist, the bytecodes will be
  397.     saved back to the filesystem for future imports. The source file's
  398.     modification timestamp must be provided as a Long value.
  399.     """
  400.     codestring = open(pathname, 'r').read()
  401.     if codestring and codestring[-1] != '\n':
  402.         codestring = codestring + '\n'
  403.     code = __builtin__.compile(codestring, pathname, 'exec')
  404.  
  405.     # try to cache the compiled code
  406.     try:
  407.         f = open(pathname + _suffix_char, 'wb')
  408.     except IOError:
  409.         pass
  410.     else:
  411.         f.write('\0\0\0\0')
  412.         f.write(struct.pack('<I', timestamp))
  413.         marshal.dump(code, f)
  414.         f.flush()
  415.         f.seek(0, 0)
  416.         f.write(imp.get_magic())
  417.         f.close()
  418.  
  419.     return code
  420.  
  421. _os_stat = _os_path_join = None
  422. def _os_bootstrap():
  423.     "Set up 'os' module replacement functions for use during import bootstrap."
  424.  
  425.     names = sys.builtin_module_names
  426.  
  427.     join = None
  428.     if 'posix' in names:
  429.         sep = '/'
  430.         from posix import stat
  431.     elif 'nt' in names:
  432.         sep = '\\'
  433.         from nt import stat
  434.     elif 'dos' in names:
  435.         sep = '\\'
  436.         from dos import stat
  437.     elif 'os2' in names:
  438.         sep = '\\'
  439.         from os2 import stat
  440.     elif 'mac' in names:
  441.         from mac import stat
  442.         def join(a, b):
  443.             if a == '':
  444.                 return b
  445.             path = s
  446.             if ':' not in a:
  447.                 a = ':' + a
  448.             if a[-1:] != ':':
  449.                 a = a + ':'
  450.             return a + b
  451.     else:
  452.         raise ImportError, 'no os specific module found'
  453.  
  454.     if join is None:
  455.         def join(a, b, sep=sep):
  456.             if a == '':
  457.                 return b
  458.             lastchar = a[-1:]
  459.             if lastchar == '/' or lastchar == sep:
  460.                 return a + b
  461.             return a + sep + b
  462.  
  463.     global _os_stat
  464.     _os_stat = stat
  465.  
  466.     global _os_path_join
  467.     _os_path_join = join
  468.  
  469. def _os_path_isdir(pathname):
  470.     "Local replacement for os.path.isdir()."
  471.     try:
  472.         s = _os_stat(pathname)
  473.     except OSError:
  474.         return None
  475.     return (s[0] & 0170000) == 0040000
  476.  
  477. def _timestamp(pathname):
  478.     "Return the file modification time as a Long."
  479.     try:
  480.         s = _os_stat(pathname)
  481.     except OSError:
  482.         return None
  483.     return long(s[8])
  484.  
  485.  
  486. ######################################################################
  487. #
  488. # Emulate the import mechanism for builtin and frozen modules
  489. #
  490. class BuiltinImporter(Importer):
  491.     def get_code(self, parent, modname, fqname):
  492.         if parent:
  493.             # these modules definitely do not occur within a package context
  494.             return None
  495.  
  496.         # look for the module
  497.         if imp.is_builtin(modname):
  498.             type = imp.C_BUILTIN
  499.         elif imp.is_frozen(modname):
  500.             type = imp.PY_FROZEN
  501.         else:
  502.             # not found
  503.             return None
  504.  
  505.         # got it. now load and return it.
  506.         module = imp.load_module(modname, None, modname, ('', '', type))
  507.         return 0, module, { }
  508.  
  509.  
  510. ######################################################################
  511. #
  512. # Internal importer used for importing from the filesystem
  513. #
  514. class _FilesystemImporter(Importer):
  515.     def __init__(self):
  516.         self.suffixes = [ ]
  517.  
  518.     def add_suffix(self, suffix, importFunc):
  519.         assert callable(importFunc)
  520.         self.suffixes.append((suffix, importFunc))
  521.  
  522.     def import_from_dir(self, dir, fqname):
  523.         result = self._import_pathname(_os_path_join(dir, fqname), fqname)
  524.         if result:
  525.             return self._process_result(result, fqname)
  526.         return None
  527.  
  528.     def get_code(self, parent, modname, fqname):
  529.         # This importer is never used with an empty parent. Its existence is
  530.         # private to the ImportManager. The ImportManager uses the
  531.         # import_from_dir() method to import top-level modules/packages.
  532.         # This method is only used when we look for a module within a package.
  533.         assert parent
  534.  
  535.         return self._import_pathname(_os_path_join(parent.__pkgdir__, modname),
  536.                                      fqname)
  537.  
  538.     def _import_pathname(self, pathname, fqname):
  539.         if _os_path_isdir(pathname):
  540.             result = self._import_pathname(_os_path_join(pathname, '__init__'),
  541.                                            fqname)
  542.             if result:
  543.                 values = result[2]
  544.                 values['__pkgdir__'] = pathname
  545.                 values['__path__'] = [ pathname ]
  546.                 return 1, result[1], values
  547.             return None
  548.  
  549.         for suffix, importFunc in self.suffixes:
  550.             filename = pathname + suffix
  551.             try:
  552.                 finfo = _os_stat(filename)
  553.             except OSError:
  554.                 pass
  555.             else:
  556.                 return importFunc(filename, finfo, fqname)
  557.         return None
  558.  
  559. ######################################################################
  560. #
  561. # SUFFIX-BASED IMPORTERS
  562. #
  563.  
  564. def py_suffix_importer(filename, finfo, fqname):
  565.     file = filename[:-3] + _suffix
  566.     t_py = long(finfo[8])
  567.     t_pyc = _timestamp(file)
  568.  
  569.     code = None
  570.     if t_pyc is not None and t_pyc >= t_py:
  571.         f = open(file, 'rb')
  572.         if f.read(4) == imp.get_magic():
  573.             t = struct.unpack('<I', f.read(4))[0]
  574.             if t == t_py:
  575.                 code = marshal.load(f)
  576.         f.close()
  577.     if code is None:
  578.         file = filename
  579.         code = _compile(file, t_py)
  580.  
  581.     return 0, code, { '__file__' : file }
  582.  
  583. class DynLoadSuffixImporter:
  584.     def __init__(self, desc):
  585.         self.desc = desc
  586.  
  587.     def import_file(self, filename, finfo, fqname):
  588.         fp = open(filename, self.desc[1])
  589.         module = imp.load_module(fqname, fp, filename, self.desc)
  590.         module.__file__ = filename
  591.         return 0, module, { }
  592.  
  593.  
  594. ######################################################################
  595.  
  596. def _print_importers():
  597.     items = sys.modules.items()
  598.     items.sort()
  599.     for name, module in items:
  600.         if module:
  601.             print name, module.__dict__.get('__importer__', '-- no importer')
  602.         else:
  603.             print name, '-- non-existent module'
  604.  
  605. def _test_revamp():
  606.     ImportManager().install()
  607.     sys.path.insert(0, BuiltinImporter())
  608.  
  609. ######################################################################
  610.  
  611. #
  612. # TODO
  613. #
  614. # from Finn Bock:
  615. #   remove use of "strop" -- not available in JPython
  616. #   type(sys) is not a module in JPython. what to use instead?
  617. #   imp.C_EXTENSION is not in JPython. same for get_suffixes and new_module
  618. #
  619. #   given foo.py of:
  620. #      import sys
  621. #      sys.modules['foo'] = sys
  622. #
  623. #   ---- standard import mechanism
  624. #   >>> import foo
  625. #   >>> foo
  626. #   <module 'sys' (built-in)>
  627. #
  628. #   ---- revamped import mechanism
  629. #   >>> import imputil
  630. #   >>> imputil._test_revamp()
  631. #   >>> import foo
  632. #   >>> foo
  633. #   <module 'foo' from 'foo.py'>
  634. #
  635. #
  636. # from MAL:
  637. #   should BuiltinImporter exist in sys.path or hard-wired in ImportManager?
  638. #   need __path__ processing
  639. #   performance
  640. #   move chaining to a subclass [gjs: it's been nuked]
  641. #   avoid strop
  642. #   deinstall should be possible
  643. #   query mechanism needed: is a specific Importer installed?
  644. #   py/pyc/pyo piping hooks to filter/process these files
  645. #   wish list:
  646. #     distutils importer hooked to list of standard Internet repositories
  647. #     module->file location mapper to speed FS-based imports
  648. #     relative imports
  649. #     keep chaining so that it can play nice with other import hooks
  650. #
  651. # from Gordon:
  652. #   push MAL's mapper into sys.path[0] as a cache (hard-coded for apps)
  653. #
  654. # from Guido:
  655. #   need to change sys.* references for rexec environs
  656. #   need hook for MAL's walk-me-up import strategy, or Tim's absolute strategy
  657. #   watch out for sys.modules[...] is None
  658. #   flag to force absolute imports? (speeds _determine_import_context and
  659. #       checking for a relative module)
  660. #   insert names of archives into sys.path  (see quote below)
  661. #   note: reload does NOT blast module dict
  662. #   shift import mechanisms and policies around; provide for hooks, overrides
  663. #       (see quote below)
  664. #   add get_source stuff
  665. #   get_topcode and get_subcode
  666. #   CRLF handling in _compile
  667. #   race condition in _compile
  668. #   refactoring of os.py to deal with _os_bootstrap problem
  669. #   any special handling to do for importing a module with a SyntaxError?
  670. #       (e.g. clean up the traceback)
  671. #   implement "domain" for path-type functionality using pkg namespace
  672. #       (rather than FS-names like __path__)
  673. #   don't use the word "private"... maybe "internal"
  674. #
  675. #
  676. # Guido's comments on sys.path caching:
  677. #
  678. # We could cache this in a dictionary: the ImportManager can have a
  679. # cache dict mapping pathnames to importer objects, and a separate
  680. # method for coming up with an importer given a pathname that's not yet
  681. # in the cache.  The method should do a stat and/or look at the
  682. # extension to decide which importer class to use; you can register new
  683. # importer classes by registering a suffix or a Boolean function, plus a
  684. # class.  If you register a new importer class, the cache is zapped.
  685. # The cache is independent from sys.path (but maintained per
  686. # ImportManager instance) so that rearrangements of sys.path do the
  687. # right thing.  If a path is dropped from sys.path the corresponding
  688. # cache entry is simply no longer used.
  689. #
  690. # My/Guido's comments on factoring ImportManager and Importer:
  691. #
  692. # > However, we still have a tension occurring here:
  693. # >
  694. # > 1) implementing policy in ImportManager assists in single-point policy
  695. # >    changes for app/rexec situations
  696. # > 2) implementing policy in Importer assists in package-private policy
  697. # >    changes for normal, operating conditions
  698. # >
  699. # > I'll see if I can sort out a way to do this. Maybe the Importer class will
  700. # > implement the methods (which can be overridden to change policy) by
  701. # > delegating to ImportManager.
  702. #
  703. # Maybe also think about what kind of policies an Importer would be
  704. # likely to want to change.  I have a feeling that a lot of the code
  705. # there is actually not so much policy but a *necessity* to get things
  706. # working given the calling conventions for the __import__ hook: whether
  707. # to return the head or tail of a dotted name, or when to do the "finish
  708. # fromlist" stuff.
  709. #
  710.