DevPlayer

Python Import Hook Hack

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