def find_module(self, fullname, path=None):
     if "." in fullname:
         # Only deal with top level packages
         return None
     if fullname in module_db:
         solution = module_db[fullname]
         if not solution:
             # Previously seen module that can't be cached
             print("Uncacheable", fullname)
             return None
         print("Cached solution for", fullname, ":", solution)
         fh = open(solution[1], solution[2][1]) if solution[0] else None
         return pkgutil.ImpLoader(fullname, fh, solution[1], solution[2])
     print("New module encountered:", fullname)
     try:
         solution = imp.find_module(fullname)
     except:
         module_db[fullname] = None
         print("Uncacheable")
         return None
     module_db[fullname] = (solution[0] is not None,) + solution[1:]
     return pkgutil.ImpLoader(fullname, *solution)
Beispiel #2
0
        def find_in_directory(self, module, fullname, directory):
            if directory == '': directory = '.'

            if directory not in self.import_path:
                # If directory has not been seen before gather information about it
                self.examine_path(directory)

            if directory in self.loader_path:
                # Path element has a special handler. Ask for module.
                self.debug("checking " + fullname + " with " +
                           str(self.loader_path[directory]))
                module_loader = self.loader_path[directory].find_module(module)
                if module_loader:
                    self.debug("module found.")
                    return module_loader

            if self.import_path[directory]:
                # Path element is a regular directory. Try finding the module in here.
                if self.import_path[directory].get(module):
                    # Match found.
                    if imp.PKG_DIRECTORY in self.import_path[directory][
                            module]:
                        full_pathname = os.path.join(directory, module)
                        if os.path.isdir(full_pathname) and os.path.exists(
                                os.path.join(full_pathname, '__init__.py')):
                            solution = (None, full_pathname,
                                        ('', '', imp.PKG_DIRECTORY))
                            self.debug("predicted solution: " + str(solution))
                            return pkgutil.ImpLoader(fullname, *solution)
                    for suffix in imp.get_suffixes():
                        if suffix[0] in self.import_path[directory][module]:
                            filename = os.path.join(directory,
                                                    module + suffix[0])
                            fh = open(filename, suffix[1])
                            solution = (fh, filename, suffix)
                            self.debug("predicted solution: " + str(solution))
                            return pkgutil.ImpLoader(fullname, *solution)
Beispiel #3
0
 def find_module_patched(self, fullname, path=None):
     # Note: we ignore 'path' argument since it is only used via meta_path
     subname = fullname.split(".")[-1]
     if subname != fullname and self.path is None:
         return None
     if self.path is None:
         path = None
     else:
         # original: path = [os.path.realpath(self.path)]
         path = [self.path]
     try:
         file, filename, etc = pkgutil.imp.find_module(subname, path)
     except ImportError:
         return None
     return pkgutil.ImpLoader(fullname, file, filename, etc)
Beispiel #4
0
    def _pl_find_on_path(self, fullname, path=None):
        subname = fullname.split(".")[-1]
        if self.path is None and subname != fullname:
            return None

        path = os.path.realpath(self.path)
        for (fp, ispkg) in FLS:
            for loader in self._loader_handlers:
                composed_path = fp % (('%s' + SEP + '%s') % (path, subname), loader.suffix)
                if os.path.isdir(composed_path):
                    raise IOError("Invalid: Directory name ends in recognized suffix")
                if os.path.isfile(composed_path):
                    return PolyLoader(fullname, composed_path, ispkg)

        # Fall back onto Python's own methods.
        try:
            file, filename, etc = imp.find_module(subname, [path])
        except ImportError as e:
            return None
        return pkgutil.ImpLoader(fullname, file, filename, etc)
Beispiel #5
0
        def find_module(self, fullname, path=None):
            if '.' in fullname:
                # Caching deals only with top level packages
                rdot = fullname.rindex('.')
                head = fullname[:rdot]  # 'logging'
                tail = fullname[rdot + 1:]  # 'os'
                #self.debug("ignoring call for " + fullname + " (" + str(path) + ")")
                self.debug("trying to predict location of " + fullname +
                           " within " + str(path))
                for directory in path:
                    solution = self.find_in_directory(tail, fullname,
                                                      directory)
                    if solution:
                        return solution
                self.debug("no such luck")
                raise ImportError('No module named ' + fullname)
                return None

            self.debug("trying to predict location of " + fullname)
            for directory in sys.path:
                solution = self.find_in_directory(fullname, fullname,
                                                  directory)
                if solution:
                    return solution

            # No match found in paths. Check if this is a builtin module
            if imp.is_builtin(fullname):
                self.debug("identified " + fullname + " as builtin")
                solution = (None, fullname, ('', '', imp.C_BUILTIN))
                return pkgutil.ImpLoader(fullname, *solution)

            # TODO: Frozen?

            # Still no match. This might not exist
            self.debug("Import " + fullname + " failed: not found")
            raise ImportError('No module named ' + fullname)
 def test_loader_deprecated(self):
     with self.check_deprecated():
         pkgutil.ImpLoader("", "", "", "")