Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > simple import hook

Reply
Thread Tools

simple import hook

 
 
Andrea Crotti
Guest
Posts: n/a
 
      11-10-2011
So I would really like to accomplish the following:
run a program normally and keep track of all the imports that were
actually done.

I studied the PEP 302, but I'm still a bit confused about how to do it.

I thought that instead of implementing everything I could just record
the request
and then delegate to the "imp" module, so I did this:

class MyLoader(object):
"""
Loader object
"""

def __init__(self):
self.loaded = set()

def find_module(self, module_name, package=None):
print("requesting %s" % module_name)
self.loaded.add(module_name)
return self

def load_module(self, fullname):
#XXX: the find_module is actually doing nothing, since
# everything is delegated to the "imp" module
fp, pathname, stuff = imp.find_module(fullname)
imp.load_module(fullname, fp, pathname, stuff)

myl = MyLoader()
sys.meta_path.append(myl)
try:
import random
import os
print(random.random())



Which doesn't work, and very strangely it doesn't even look deterministic!
Sometimes it stops at first import sometimes it's able to do a few of them.
How can that be?

And how could I do solve my problem?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
multiversion flag and auto requiring import hook Andrea Crotti Python 0 01-19-2012 01:12 PM
import hook Jeremy Sanders Python 4 06-18-2006 07:54 PM
Python exception hook simple example needed fowlertrainer@anonym.hu Python 6 07-11-2005 02:50 PM
import hook, overwrite import? Torsten Mohr Python 2 01-27-2005 01:14 PM
Reference implementation of an import hook Noam Raphael Python 0 06-21-2004 09:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57