GithubHelp home page GithubHelp logo

diary's People

Contributors

oahcy avatar

Watchers

 avatar

diary's Issues

python编程之hook import

hook python import (让我们知道import发生了什么)

在import的第一个阶段,主要是完成了查找要引入模块的功能,这个查找的过程如下:

1. 检查 sys.modules (保存了之前import的类库的缓存),如果module被找到,则⾛到第二步。
2. 检查 sys.meta_path。meta_path 是一个 list,⾥面保存着一些 finder 对象,如果找到该module的话,就会返回一个finder对象。
3. 检查⼀些隐式的finder对象,不同的python实现有不同的隐式finder,但是都会有 sys.path_hooks, sys.path_importer_cache 以及sys.path。
4. 抛出 ImportError。

上面说的sys.meta_path在开始时其实是空的,我们可以自己向里面加入finder对象,如果在sys.modules中找不到,就会调用sys.meta_path中的finder对象找,用这种方法,我们可以看到程序找module的过程。

× 比如我们加入一个finder对象到meta_path:

import sys
class watcher(object):
    @classmethod
    def find_module(cls, name, path, target=None):
        print("Import -> ", name, path, target)
        return None
sys.meta_path.insert(0, watcher)
import web

然后就可以看到import web到底干了什么了:

>>> import sys
>>> class watcher(object):
...     @classmethod
...     def find_module(cls,name,path,target=None):
...             print "import -- ",name,path,target
...             return None
...
>>> sys.meta_path.insert(0,watcher)
>>> import web
import --  web None None
import --  web.__future__ ['C:\\python27\\lib\\site-packages\\web.py-0.37-py2.7.egg\\web'] None
import --  __future__ None None
import --  web.utils ['C:\\python27\\lib\\site-packages\\web.py-0.37-py2.7.egg\\web'] None
import --  web.re ['C:\\python27\\lib\\site-packages\\web.py-0.37-py2.7.egg\\web'] None
import --  web.sys ['C:\\python27\\lib\\site-packages\\web.py-0.37-py2.7.egg\\web'] None
import --  web.time ['C:\\python27\\lib\\site-packages\\web.py-0.37-py2.7.egg\\web'] None
import --  time None None
import --  web.threading ['C:\\python27\\lib\\site-packages\\web.py-0.37-py2.7.egg\\web'] None
import --  threading None None
import --  thread None None
import --  collections None None
import --  _collections None None
import --  keyword None None
import --  heapq None None
import --  itertools None None
import --  _heapq None None
import --  web.itertools ['C:\\python27\\lib\\site-packages\\web.py-0.37-py2.7.egg\\web'] None
import --  web.traceback ['C:\\python27\\lib\\site-packages\\web.py-0.37-py2.7.egg\\web'] None
import --  web.os ['C:\\python27\\lib\\site-packages\\web.py-0.37-py2.7.egg\\web'] None
import --  web.subprocess ['C:\\python27\\lib\\site-packages\\web.py-0.37-py2.7.egg\\web'] None

参考 ython 中 import 的机制与实现

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.