DevPlayer

Python Import Hook Hack

Nov 15th, 2011
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.81 KB | None | 0 0
  1.  
  2.  
  3. # 2011-Nov-15
  4.  
  5. # recordimports.py
  6.  
  7. # my Import Hook Hack in response to:
  8. # http://groups.google.com/group/comp.lang.python/browse_thread/thread/5a5d5c724f142eb5?hl=en
  9. # as an initial learning exercise
  10.  
  11. # This code needs to come before any imports you want recorded
  12. # usually just once in the initial (main) module
  13. # Of course you can excluding the if __name__ == '__main__': demo code
  14.  
  15. # barely tested:
  16. #    only tested with modules that had no errors on import
  17. #    did not need/use/expect/try  reload()
  18. #    ran with Python 2.7 on Win32
  19. # create two fake modules moda.py and modb.py and stick some imports in them
  20.  
  21. '''
  22. Exerpt from PEP 302 -- New Import Hooks
  23. ...
  24. Motivation:
  25.    - __import__ gets called even for modules that are already in
  26.      sys.modules, which is almost never what you want, unless you're
  27.      writing some sort of monitoring tool.
  28.  
  29. Note the last two words.'''
  30.  
  31.  
  32. # =======================================================================
  33. # place to save Collected imports
  34. imported = []
  35.  
  36. # save old __builtins__.__import__()
  37. __builtins__.__dict__['__old_import__'] = __builtins__.__dict__['__import__']
  38.  
  39. # match __builtins__.__import__() function signature
  40. def __myimport(name, globals={}, locals={}, fromlist=[], level=-1):
  41.     global imported
  42.  
  43.     # I don't know why this works.
  44.     __name__ = locals['__name__']
  45.     __file__ = locals['__file__']
  46.     __package__ = locals['__package__']
  47.     __doc__ = locals['__doc__']
  48.  
  49.     # call original __import__
  50.     module = __builtins__.__old_import__(name, globals, locals, fromlist, level)
  51.  
  52.     # save import module name into namespace
  53.     __builtins__.__dict__[name] = module
  54.  
  55.     tag = (name, __name__, __file__, __package__, module)
  56.  
  57.     # do not append duplicates
  58.     if tag not in imported:
  59.         imported.append( tag )
  60.  
  61.     return module
  62.  
  63. # store the new __import__ into __builtins__
  64. __builtins__.__dict__['__import__'] = __myimport
  65.  
  66. # erase unneed func name
  67. del __myimport
  68. # =======================================================================
  69.  
  70.  
  71. # demo
  72. if __name__ == '__main__':
  73.     # import some random packages/modules
  74.     import sys
  75.     import moda        # a test module that does some other imports
  76.     import modb        # a test module that does some imports
  77.     from pprint import pprint
  78.  
  79.     # imported has name, __name__, __file__, __package__
  80.     # show each import
  81.     for n, __n, __f, __p, m in imported:
  82.         print n
  83.         print '   ', __n
  84.         print '   ', __f
  85.         print '   ', __p
  86.         print
  87.     del n, __n, __f, __p, m
  88.  
  89.     print 'recordimports.py'
  90.     pprint(dir(), None, 4, 1)
  91.     print
  92.  
  93.     print 'moda.py'
  94.     pprint(dir(moda), None, 4, 1)
  95.     print
  96.  
  97.     print 'modb.py'
  98.     pprint(dir(modb), None, 4, 1)
  99.  
  100.     # print imported
  101.     print
  102.  
  103.  
Add Comment
Please, Sign In to add comment