⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 palm_imputil.py

📁 minimal python variant for small footprint apps like embedded apps
💻 PY
📖 第 1 页 / 共 2 页
字号:
        # use the returned module, or create a new one to exec code into        if is_module:            module = code        else:            module = imp.new_module(fqname)        ### record packages a bit differently??        module.__importer__ = self        module.__ispkg__ = ispkg        # insert additional values into the module (before executing the code)        module.__dict__.update(values)        # the module is almost ready... make it visible        sys.modules[fqname] = module        # execute the code within the module's namespace        if not is_module:            exec code in module.__dict__        # fetch from sys.modules instead of returning module directly.        return sys.modules[fqname]    def _load_tail(self, m, parts):        """Import the rest of the modules, down from the top-level module.        Returns the last module in the dotted list of modules.        """        for part in parts:            fqname = "%s.%s" % (m.__name__, part)            m = self._import_one(m, part, fqname)            if not m:                raise ImportError, "No module named " + fqname        return m    def _import_fromlist(self, package, fromlist):        'Import any sub-modules in the "from" list.'        # if '*' is present in the fromlist, then look for the '__all__'        # variable to find additional items (modules) to import.        if '*' in fromlist:            fromlist = list(fromlist) + \                       list(package.__dict__.get('__all__', []))        for sub in fromlist:            # if the name is already present, then don't try to import it (it            # might not be a module!).            if sub != '*' and not hasattr(package, sub):                subname = "%s.%s" % (package.__name__, sub)                submod = self._import_one(package, sub, subname)                if not submod:                    raise ImportError, "cannot import name " + subname    def _do_import(self, parent, parts, fromlist):        """Attempt to import the module relative to parent.        This method is used when the import context specifies that <self>        imported the parent module.        """        top_name = parts[0]        top_fqname = parent.__name__ + '.' + top_name        top_module = self._import_one(parent, top_name, top_fqname)        if not top_module:            # this importer and parent could not find the module (relatively)            return None        return self._finish_import(top_module, parts[1:], fromlist)    ######################################################################    #    # METHODS TO OVERRIDE    #    def get_code(self, parent, modname, fqname):        """Find and retrieve the code for the given module.        parent specifies a parent module to define a context for importing. It        may be None, indicating no particular context for the search.        modname specifies a single module (not dotted) within the parent.        fqname specifies the fully-qualified module name. This is a        (potentially) dotted name from the "root" of the module namespace        down to the modname.        If there is no parent, then modname==fqname.        This method should return None, or a 3-tuple.        * If the module was not found, then None should be returned.        * The first item of the 2- or 3-tuple should be the integer 0 or 1,            specifying whether the module that was found is a package or not.        * The second item is the code object for the module (it will be            executed within the new module's namespace). This item can also            be a fully-loaded module object (e.g. loaded from a shared lib).        * The third item is a dictionary of name/value pairs that will be            inserted into new module before the code object is executed. This            is provided in case the module's code expects certain values (such            as where the module was found). When the second item is a module            object, then these names/values will be inserted *after* the module            has been loaded/initialized.        """        raise RuntimeError, "get_code not implemented"######################################################################## Emulate the import mechanism for builtin and frozen modules#class BuiltinImporter(Importer):    def get_code(self, parent, modname, fqname):        if parent:            # these modules definitely do not occur within a package context            return None        # look for the module        if imp.is_builtin(modname):            type = imp.C_BUILTIN        elif imp.is_frozen(modname):            type = imp.PY_FROZEN        else:            # not found            return None        # got it. now load and return it.        module = imp.load_module(modname, None, modname, ('', '', type))        return 0, module, { }######################################################################def _print_importers():    items = sys.modules.items()    items.sort()    for name, module in items:        if module:            print name, module.__dict__.get('__importer__', '-- no importer')        else:            print name, '-- non-existent module'def _test_revamp():    ImportManager().install()    sys.path.insert(0, BuiltinImporter())######################################################################## TODO## from Finn Bock:#   remove use of "strop" -- not available in JPython#   type(sys) is not a module in JPython. what to use instead?#   imp.C_EXTENSION is not in JPython. same for get_suffixes and new_module##   given foo.py of:#      import sys#      sys.modules['foo'] = sys##   ---- standard import mechanism#   >>> import foo#   >>> foo#   <module 'sys' (built-in)>##   ---- revamped import mechanism#   >>> import imputil#   >>> imputil._test_revamp()#   >>> import foo#   >>> foo#   <module 'foo' from 'foo.py'>### from MAL:#   should BuiltinImporter exist in sys.path or hard-wired in ImportManager?#   need __path__ processing#   performance#   move chaining to a subclass [gjs: it's been nuked]#   avoid strop#   deinstall should be possible#   query mechanism needed: is a specific Importer installed?#   py/pyc/pyo piping hooks to filter/process these files#   wish list:#     distutils importer hooked to list of standard Internet repositories#     module->file location mapper to speed FS-based imports#     relative imports#     keep chaining so that it can play nice with other import hooks## from Gordon:#   push MAL's mapper into sys.path[0] as a cache (hard-coded for apps)## from Guido:#   need to change sys.* references for rexec environs#   need hook for MAL's walk-me-up import strategy, or Tim's absolute strategy#   watch out for sys.modules[...] is None#   flag to force absolute imports? (speeds _determine_import_context and#       checking for a relative module)#   insert names of archives into sys.path  (see quote below)#   note: reload does NOT blast module dict#   shift import mechanisms and policies around; provide for hooks, overrides#       (see quote below)#   add get_source stuff#   get_topcode and get_subcode#   CRLF handling in _compile#   race condition in _compile#   refactoring of os.py to deal with _os_bootstrap problem#   any special handling to do for importing a module with a SyntaxError?#       (e.g. clean up the traceback)#   implement "domain" for path-type functionality using pkg namespace#       (rather than FS-names like __path__)#   don't use the word "private"... maybe "internal"### Guido's comments on sys.path caching:## We could cache this in a dictionary: the ImportManager can have a# cache dict mapping pathnames to importer objects, and a separate# method for coming up with an importer given a pathname that's not yet# in the cache.  The method should do a stat and/or look at the# extension to decide which importer class to use; you can register new# importer classes by registering a suffix or a Boolean function, plus a# class.  If you register a new importer class, the cache is zapped.# The cache is independent from sys.path (but maintained per# ImportManager instance) so that rearrangements of sys.path do the# right thing.  If a path is dropped from sys.path the corresponding# cache entry is simply no longer used.## My/Guido's comments on factoring ImportManager and Importer:## > However, we still have a tension occurring here:# ># > 1) implementing policy in ImportManager assists in single-point policy# >    changes for app/rexec situations# > 2) implementing policy in Importer assists in package-private policy# >    changes for normal, operating conditions# ># > I'll see if I can sort out a way to do this. Maybe the Importer class will# > implement the methods (which can be overridden to change policy) by# > delegating to ImportManager.## Maybe also think about what kind of policies an Importer would be# likely to want to change.  I have a feeling that a lot of the code# there is actually not so much policy but a *necessity* to get things# working given the calling conventions for the __import__ hook: whether# to return the head or tail of a dotted name, or when to do the "finish# fromlist" stuff.#

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -